[英]How to set legend outside three-dimensional axes in matplotlib?
I am working with a basic 3D scatterplot in matplotlib. 我正在使用matplotlib中的基本3D散点图。 A simple example of the code is the following: 该代码的一个简单示例如下:
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()
Following is my output plot: 以下是我的输出图:
In my output plot, the legend overlaps with the label for Z-axis. 在我的输出图中,图例与Z轴的标签重叠。 I want to place the legend outside of the three dimensional axis box, preferably to the right of the plot (ie to the right of ZLabel) such that it doesn't overlap with anything . 我想将图例放置在三维轴框的外部, 最好放置在图的右侧(即ZLabel的右侧) ,以使其不与任何东西重叠 。 How can I do that? 我怎样才能做到这一点?
The general strategy to place the legend out of the plot is shown in How to put the legend out of the plot 如何将图例移出情节的一般策略显示在如何将图例移出情节中
Here you may want to leave some more space to the right and move the legend even further to the right 在这里,您可能需要在右侧留出更多空间,并进一步将图例移至右侧
fig.tight_layout()
fig.subplots_adjust(right=0.8)
ax.legend(loc='center left', bbox_to_anchor=(1.07, 0.5), fontsize=7)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.