在matplotlib中绘制3d散点图 [英] plotting 3d scatter in matplotlib

查看:74
本文介绍了在matplotlib中绘制3d散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个scipy/numpy的Nx3矩阵集合,我想对其进行3维分散,其中X和Y轴由矩阵的第一列和第二列的值,高度确定每个条形图的第一个是矩阵中的第三列,条形数由N确定.

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.

每个矩阵代表一个不同的数据组,我希望每个矩阵都以不同的颜色绘制,然后为整个图形设置图例.

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.

我有以下代码:

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])

问题是ax.scatter以透明的方式绘制事物,我希望将其删除.另外,我想设置xticks,yticks和zticks-我该怎么做?

The issue is that ax.scatter plots things with a transparency and I'd like that remove. Also, I'd like to set the xticks and yticks and zticks -- how can I do that?

最后,即使我为每个分散调用都调用了label =",图例调用也不会出现.如何显示图例?

Finally, the legend call does not appear, even though I am calling label="" for each scatter call. How can I get the legend to appear?

非常感谢您的帮助.

推荐答案

尝试将"ax.scatter"替换为"ax.plot",并可能用"o"参数替换类似的圆圈.这样可以修复透明度和图例.

Try replacing 'ax.scatter' with ax.plot', possibly with the 'o' parameter to get similar circles. 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.

要设置刻度,需要在此处中描述的mplot3d于2010年8月更新.一个>.我从 Sourceforge 中获得了最新的mplot3d.我不确定Matplotlib 1.0.1是否包含此最新更新,因为我仍在使用Matplotlib 1.0.0运行Python 2.6.

Setting the ticks requires the August 2010 update to mplot3d as described here. I got the latest mplot3d from Sourceforge. 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.

一个快速而肮脏的图例虚假图,同时保持从散点图获得的3d透明效果:

A quick and dirty dummy plot for the legends while keeping the 3d transparency effect you get from scatter:

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

这篇关于在matplotlib中绘制3d散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆