简体   繁体   English

如何在matplotlib中的三维轴外设置图例?

[英]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.

相关问题 如何将三维数组的最后两个轴上的下三角形设为零? - How do I set to zero lower triangles along the last two axes of a three-dimensional array? Matplotlib具有固定的轴大小和轴外的图例 - Matplotlib with fixed axes size and legend outside of axes 如何动态创建三维数组 - How to dynamically create a three-dimensional array 如何使用matplotlib在同一平面上绘制多个三维曲面图 - How to plot multiple three-dimensional surface plots with matplotlib in the same plane 如何 position 一个 matplotlib 补丁超出轴范围(以便它可以在标题旁边,或图例,或图上的任何地方) - How to position a matplotlib patch outside of the axes range (so that it could be next to the title, or legend, or anywhere on the figure) 三维数组的Python列表理解 - Python list comprehension for three-dimensional array 将三维ndarray重塑为二维 - Reshaping a three-dimensional ndarray into two dimensions 如何从二维数据中获得三维直方图? - How to get a three-dimensional histogram from two-dimensional data? 如何在轴外使用 matplotlib blit artist? - How to matplotlib blit artist outside axes? 二维和三维张量的乘法 - Multiplication of two-dimensional and three-dimensional tensors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM