简体   繁体   English

在matplotlib中绘制三维散点图

[英]plotting 3d scatter in matplotlib

I have a collection of Nx3 matrices in scipy/numpy and I'd like to make a 3 dimensional scatter of it, where the X and Y axes are determined by the values of first and second columns of the matrix, the height of each bar is the third column in the matrix, and the number of bars is determined by N. 我有一个scipy / numpy的Nx3矩阵集合,我想对它进行三维散射,其中X和Y轴由矩阵的第一列和第二列的值确定,每个条的高度是矩阵中的第三列,条数由N确定。

Each matrix represents a different data group and I want each to be plotted with a different color, and then set a legend for the entire figure. 每个矩阵代表一个不同的数据组,我希望每个矩阵都用不同的颜色绘制,然后为整个图形设置图例。

I have the following code: 我有以下代码:

fig = pylab.figure()
s = plt.subplot(1, 1, 1)
colors = ['k', "#B3C95A", 'b', '#63B8FF', 'g', "#FF3300",
          'r', 'k']
ax = Axes3D(fig)
plots = []
index = 0

for data, curr_color in zip(datasets, colors):
    p = ax.scatter(log2(data[:, 0]), log2(data[:, 1]),
                   log2(data[:, 2]), c=curr_color, label=my_labels[index])

    s.legend()
    index += 1

    plots.append(p)

    ax.set_zlim3d([-1, 9])
    ax.set_ylim3d([-1, 9])
    ax.set_xlim3d([-1, 9])

The issue is that ax.scatter plots things with a transparency and I'd like that remove. 问题是ax.scatter用透明度绘制东西,我想删除它。 Also, I'd like to set the xticks and yticks and zticks -- how can I do that? 另外,我想设置xticks和yticks以及zticks - 我该怎么做?

Finally, the legend call does not appear, even though I am calling label="" for each scatter call. 最后,即使我为每个分散调用调用label =“”,也不会出现图例调用。 How can I get the legend to appear? 如何才能显示传奇?

thanks very much for your help. 非常感谢您的帮助。

Try replacing 'ax.scatter' with ax.plot', possibly with the 'o' parameter to get similar circles. 尝试用ax.plot'替换'ax.scatter',可能使用'o'参数来获得类似的圆圈。 This fixes the transparency and the legend. 这可以修复透明度和图例。

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

mpl.rcParams['legend.fontsize'] = 10

fig = plt.figure(1)
fig.clf()
ax = Axes3D(fig)
datasets = random((8,100,3))*512
my_labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

colors = ['k', "#B3C95A", 'b', '#63B8FF', 'g', "#FF3300",
          'r', 'k']
index = 0
for data, curr_color in zip(datasets, colors):
    ax.plot(np.log2(data[:, 0]), np.log2(data[:, 1]), 
                   np.log2(data[:, 2]), 'o', c=curr_color, label=my_labels[index])
    index += 1

ax.set_zlim3d([-1, 9])
ax.set_ylim3d([-1, 9])
ax.set_xlim3d([-1, 9])

ax.set_xticks(range(0,11))
ax.set_yticks([1,2,8])
ax.set_zticks(np.arange(0,9,.5))

ax.legend(loc = 'upper left')

plt.draw()

plt.show()

I added a few lines and tweaks to get some sample data and get the rest of your demo working. 我添加了几行和调整来获取一些示例数据并使其余的演示工作。 I assume you'll be able to get it to work. 我假设你能够让它发挥作用。

Setting the ticks requires the August 2010 update to mplot3d as described here . 设置蜱要求2010年8月更新的描述mplot3d 这里 I got the latest mplot3d from Sourceforge . 我从Sourceforge获得了最新的mplot3d。 I'm not quite sure if Matplotlib 1.0.1 contains this latest update as I'm still running Python 2.6 with Matplotlib 1.0.0. 我不太确定Matplotlib 1.0.1是否包含这个最新更新,因为我仍在使用Matplotlib 1.0.0运行Python 2.6。

Edit 编辑

A quick and dirty dummy plot for the legends while keeping the 3d transparency effect you get from scatter: 一个快速而肮脏的虚拟情节图,同时保持从散射中获得的3d透明效果:

index = 0
for data, curr_color in zip(datasets, colors):
    ax.scatter(np.log2(data[:, 0]), np.log2(data[:, 1]), 
                   np.log2(data[:, 2]), 'o', c=curr_color, label=my_labels[index])
    ax.plot([], [], 'o', c = curr_color, label=my_labels[index])                    
    index += 1

AFAIK, legends for 3d scatter artists are not directly supported. AFAIK,不直接支持3D散点图艺术家的传说。 See here: http://matplotlib.sourceforge.net/users/legend_guide.html#plotting-guide-legend 请参见: http//matplotlib.sourceforge.net/users/legend_guide.html#plotting-guide-legend

However, you can use a hack/work-around with a 'proxy artist', using something like: 但是,您可以使用“代理艺术家”进行黑客攻击/解决方案,使用以下内容:

p = Rectangle((0, 0), 1, 1, fc="r")
axis.legend([p], ["Red Rectangle"])

So the proxy artist won't be added to the axis, but you can use it to create a legend. 因此代理艺术家不会添加到轴,但您可以使用它来创建图例。

Setting the kwarg depthshade=False fixed it for me: 设置kwarg depthshade = False为我修复它:

ax.scatter(np.log2(data[:, 0]), np.log2(data[:, 1]), 
               np.log2(data[:, 2]), 'o', c=curr_color,  label=my_labels[index], depthshade=False)

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

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