【问题标题】:How to set legend outside three-dimensional axes in matplotlib?如何在matplotlib中设置三维轴之外的图例?
【发布时间】:2019-06-18 16:10:35
【问题描述】:

我正在使用 matplotlib 中的基本 3D 散点图。一个简单的代码示例如下:

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt
import numpy as np

XX = np.array([[1,6,7,3,2],[8,11,7,5,12],[2,7,1,30,12],[15,3,17,2,1]])

fig = plt.figure()
ax = plt.axes(projection='3d')

xs = XX[indx, 0]
ys = XX[indx, 1]
zs = XX[indx, 2]


for i in range(11):
    ax.scatter3D(xs, ys, zs, c='b', marker='o', label='item'+str(i), s=100.5, alpha=1)

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.title('Number of Components = 3')
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fontsize=7)
plt.tight_layout()
plt.show()

以下是我的输出图:

在我的输出图中,图例与 Z 轴的标签重叠。我想将图例放置在三维轴框之外,最好在绘图的右侧(即 ZLabel 的右侧)这样它不会与任何东西重叠强>。我该怎么做?

【问题讨论】:

    标签: python python-2.7 matplotlib


    【解决方案1】:

    将图例置于情节之外的一般策略见How to put the legend out of the plot

    在这里,您可能希望在右侧留出更多空间,并将图例向右移动得更远

    fig.tight_layout()
    fig.subplots_adjust(right=0.8)
    ax.legend(loc='center left', bbox_to_anchor=(1.07, 0.5), fontsize=7)
    

    【讨论】:

      猜你喜欢
      • 2012-09-06
      • 1970-01-01
      • 2015-07-31
      • 1970-01-01
      • 2021-03-17
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多