对于一个项目,我制作了一些三维散点图,其中包含三个相应的投影。我用不同的颜色来表示第四个参数。首先,我用一种特定的颜色绘制数据,然后我overplot与其他数据以不同的颜色,从而使最终的顺序是这样的,我所看到的一切,因为我想:Matplotlib中丢失的颜色三维散点图
在开始这工作得很好,但是当我尝试用稍微不同的数据做同样的事情时,颜色会变得混乱。在投影显示的颜色是正确的,但其中一些在3D图缺少这样他们就不会再匹配:当我在一个有趣的方式旋转3D图
中,颜色恢复,我可以看到他们,因为他们被认为是:
不过,我不希望在一个有趣的方式旋转的3D绘图,因为轴搞的一团糟,它的不可能像这样正确地阅读它。
我在这里发现了一个解决问题的办法: plotting 3d scatter in matplotlib。 它基本上说我应该用ax.plot(X,Y,'o')替换我的ax.scatter(X,Y)。当我这样做时,颜色以他们应该的方式显示出来,但情节更加混乱和丑陋。 基本上我只是想能够用散点图来做到这一点。
有谁知道如何解决这个问题?
这里是我的代码最小例如,对于只有两种颜色:
from mpl_toolkits.mplot3d import art3d
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import gridspec
art3d.zalpha = lambda *args:args[0]
numcols = 20
percentage = 50
def load(Td, pc):
T = np.load(str(pc) + 'pctTemperaturesTd=' + str(Td) + '.npy')
D = np.load(str(pc) + 'pctDensitiesTd=' + str(Td) + '.npy')
CD = np.load(str(pc) + 'pctColDensitiesTd=' + str(Td) + '.npy')
return T, D, CD
def colors(ax):
colors = np.zeros((numcols, 4))
cm = plt.get_cmap('gist_rainbow')
ax.set_color_cycle([cm(1.*i/numcols) for i in range(numcols)])
for i in range(numcols):
color = cm(1.*i/numcols)
colors[i,:] = color
return colors
# LOAD DATA
T10, D10, CD10 = load(10, percentage)
T200, D200, CD200 = load(200, percentage)
# 3D PLOT
fig = plt.figure(1)
gs = gridspec.GridSpec(4, 4)
ax = fig.add_subplot(gs[:-1,:-1], projection='3d')
colours = colors(ax)
ax.plot(T200/100., np.log10(D200), np.log10(CD200), '*', markersize=10,color=colours[10], mec = colours[10], label='Td = 200', alpha=1)
ax.plot(T10/100., np.log10(D10), np.log10(CD10), '*', markersize=10,color=colours[0], mec = colours[0], label='Td = 10', alpha=1)
ax.set_xlabel('\nTg/100', fontsize='x-large')
ax.set_ylabel('\nlog(nH)', fontsize='x-large')
ax.set_zlabel('\nlog(colDen)', fontsize='x-large')
ax.set_xlim(0,5)
#ax.set_zlim(0,)
ax.set_ylim(2,6)
# PROJECTIONS
# Tg, nH
ax2 = fig.add_subplot(gs[3,0])
ax2.scatter(T200/100., np.log10(D200), marker='*', s=10, color=colours[10], label='Td = 200', alpha=1, edgecolor=colours[10])
ax2.scatter(T10/100., np.log10(D10), marker='*', s=10, color=colours[0], label='Td = 10', alpha=1, edgecolor=colours[0])
ax2.set_xlabel('Tg/100')
ax2.set_ylabel('log(nH)')
ax2.set_xlim(0,6)
# Tg, colDen
ax3 = fig.add_subplot(gs[3,1])
ax3.scatter(T200/100., np.log10(CD200), marker='*', s=10, color=colours[10], label='Td = 200', alpha=1, edgecolor=colours[10])
ax3.scatter(T10/100., np.log10(CD10), marker='*', s=10, color=colours[0], label='Td = 10', alpha=1, edgecolor=colours[0])
ax3.set_xlabel('Tg/100')
ax3.set_ylabel('log(colDen)')
ax3.set_xlim(0,6)
# nH, colDen
ax4 = fig.add_subplot(gs[3,2])
ax4.scatter(np.log10(D200), np.log10(CD200), marker='*', s=10, color=colours[10], label='Td = 200', alpha=1, edgecolor=colours[10])
ax4.scatter(np.log10(D10), np.log10(CD10), marker='*', s=10, color=colours[0], label='Td = 10', alpha=1, edgecolor=colours[0])
ax4.set_xlabel('log(nH)')
ax4.set_ylabel('log(colDen)')
# LEGEND
legend = fig.add_subplot(gs[:,3])
text = ['Td = 10', 'Td = 20', 'Td = 30', 'Td = 40', 'Td = 50', 'Td = 60', 'Td = 70', 'Td = 80', 'Td = 90', 'Td = 100', 'Td = 110', 'Td = 120', 'Td = 130', 'Td = 140', 'Td = 150', 'Td = 160', 'Td = 170', 'Td = 180', 'Td = 190', 'Td = 200']
array = np.arange(0,2,0.1)
for i in range(len(array)):
legend.scatter(0, i, marker='*', s=100, c=colours[numcols-i-1], edgecolor=colours[numcols-i-1])
legend.text(0.3, i-0.25, text[numcols-i-1])
legend.set_xlim(-0.5, 2.5)
legend.set_ylim(0-1, i+1)
legend.axes.get_xaxis().set_visible(False)
legend.axes.get_yaxis().set_visible(False)
gs.tight_layout(fig)
plt.show()
你能分享代码吗?你能指定使用的数据结构,还是表示你正在使用的数据的形状?更重要的是,你使用的两个数据集有多不同(一些数据集中的某些特征与另一个数据集相比有什么不同?) - 这将解释旋转图表时红色的外观......但它仍然很奇怪)? – kasparg
我收录了我的代码。数据集只是numpy数组,第二个数据集应该是第一个数据集的一个子集,可以这么说,它只包含也出现在第一个数据集中的点,但它们只是更少。除此之外没有任何变化。我认为这是一个错误,我没有办法改变它显示我的数据的顺序。毕竟,我现在只是用'阴谋'来解决问题,而不是'散布'。 – Nikki
我要看的第一件事是['zorder'](http://matplotlib.org/examples/pylab_examples/zorder_demo.html)。 – kwinkunks