繁体   English   中英

使用Matplotlib中的scatter()在3D散点图中添加图例

[英]Add a legend in a 3D scatterplot with scatter() in Matplotlib

我想在同一个绘图中创建一个包含不同数据集的3D散点图,并使用其标签创建一个图例。 我面临的问题是我无法正确添加图例,我得到一个带有空标签的图,如下图所示:

http://tinypic.com/view.php?pic=4jnm83&s=5#.Uqd-05GP-gQ

更具体地说,我得到错误:

/usr/lib/pymodules/python2.7/matplotlib/legend.py:610: UserWarning: Legend does not support <mpl_toolkits.mplot3d.art3d.Patch3DCollection object at 0x3bf46d0>
Use proxy artist instead."

请在下面找到我迄今为止尝试过的示例演示:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import random
import csv
from os import listdir
from os.path import isfile, join

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

handles = []
colors = ['blue', 'red']

X1 = range(0,10)
Y1 = range(0,10)
Z1 = range(0,10)

random.shuffle(X1)
random.shuffle(Y1)
random.shuffle(Z1)

scatter1 = ax.scatter(X1, Y1, Z1, c = colors[0], marker = 'o')

random.shuffle(X1)
random.shuffle(Y1)
random.shuffle(Z1)

scatter2 = ax.scatter(X1, Y1, Z1, c = colors[1], marker = 'v')

ax.set_xlabel('X', fontsize = 10)
ax.set_ylabel('Y', fontsize = 10)
ax.set_zlabel('Z', fontsize = 10)

ax.legend([scatter1, scatter2], ['label1', 'label2'])

plt.show()

我见过其他大致相似的例子,但没有一个使用scatter()图。 除了工作解决方案,有人可以解释我做错了什么?

scatter1_proxy = matplotlib.lines.Line2D([0],[0], linestyle="none", c=colors[0], marker = 'o')
scatter2_proxy = matplotlib.lines.Line2D([0],[0], linestyle="none", c=colors[1], marker = 'v')
ax.legend([scatter1_proxy, scatter2_proxy], ['label1', 'label2'], numpoints = 1)

问题是图例功能不支持3D散点返回的类型。 所以你必须创建一个具有相同特征的“虚拟图”并将它们放在图例中。

numpoints = 1只能获得图例中的一个点
linestyle =“none”因此图例中没有绘制线条

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM