对于一个项目,我正在制作一些3D散点图,其中有三个相应的投影。我用不同的颜色来表示第四个参数。首先,我用某种颜色绘制数据,然后用其他颜色的数据叠加绘制,这样最后顺序就可以看到我想要的一切:
一开始这工作得很好,但是当我试图用稍微不同的数据做同样的事情时,颜色就变得混乱了。投影中显示的颜色是正确的,但其中一些在3D图中缺失,因此它们不再匹配:
当我以一种有趣的方式旋转3D图时,颜色被恢复,我可以看到它们,因为它们应该是:
然而,我不想要一个以有趣的方式旋转的3D图,因为轴被弄乱了,不可能像那样正确地阅读它。
我在这里找到了一个解决问题的方法:plotting 3d scatter in matplotlib .它基本上是说我应该更换我的斧头。scatter(X,Y)with ax.plot(X,Y,'o').当我这样做的颜色是显示的方式,他们应该是,但情节是混乱和丑陋的这种方式。基本上我只想用散点图来做这件事。
有人知道怎么解决这个问题吗?
下面是我的代码的最小示例,只有两种颜色:
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()
1条答案
按热度按时间deikduxw1#
不要使用
ax.plot(x,y, 'o')
,而是使用ax.plot(x,y,'.')
或ax.plot(x,y,'*'
。'o'
指定要使用的marker,而“o”标记是一个大的实心圆,这就是为什么你的图看起来很难看。