2015-05-14 130 views
27

對於一個項目,我製作了一些三維散點圖,其中包含三個相應的投影。我用不同的顏色來表示第四個參數。首先,我用一種特定的顏色繪製數據,然後我overplot與其他數據以不同的顏色,從而使最終的順序是這樣的,我所看到的一切,因爲我想:Matplotlib中丟失的顏色三維散點圖

This is what I want!

在開始這工作得很好,但是當我嘗試用稍微不同的數據做同樣的事情時,顏色會變得混亂。在投影顯示的顏色是正確的,但其中一些在3D圖缺少這樣他們就不會再匹配:當我在一個有趣的方式旋轉3D圖

Colors messed up :(

中,顏色恢復,我可以看到他們,因爲他們被認爲是:

Funny rotation

不過,我不希望在一個有趣的方式旋轉的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() 
+0

你能分享代碼嗎?你能指定使用的數據結構,還是表示你正在使用的數據的形狀?更重要的是,你使用的兩個數據集有多不同(一些數據集中的某些特徵與另一個數據集相比有什麼不同?) - 這將解釋旋轉圖表時紅色的外觀......但它仍然很奇怪)? – kasparg

+0

我收錄了我的代碼。數據集只是numpy數組,第二個數據集應該是第一個數據集的一個子集,可以這麼說,它只包含也出現在第一個數據集中的點,但它們只是更少。除此之外沒有任何變化。我認爲這是一個錯誤,我沒有辦法改變它顯示我的數據的順序。畢竟,我現在只是用'陰謀'來解決問題,而不是'散佈'。 – Nikki

+0

我要看的第一件事是['zorder'](http://matplotlib.org/examples/pylab_examples/zorder_demo.html)。 – kwinkunks

回答

1

而不是使用ax.plot(x,y, 'o')嘗試ax.plot(x,y,'.')ax.plot(x,y,'*''o'指定marker使用,而'o'標記是一個大的實心圓,這就是爲什麼你的情節看起來很醜。

相關問題