2016-06-21 132 views
1

我嘗試使用第三個變量來定義顏色的彩色散點圖。使用以下代碼很簡單:當我使用第三個變量來定義顏色的蟒蛇散點顏色圖時,沒有顏色

plt.scatter(mH, mA, s=1, c=mHc) 
plt.colorbar() 
plt.show() 

但是我沒有太多選擇來修改圖的框架。我想下面的代碼,使豐富多彩的散點圖,同時我嘗試優化的情節框架:

import numpy as np 
import math 
from matplotlib import rcParams 
import matplotlib.pyplot as plt 
from matplotlib.ticker import AutoMinorLocator 

fig, ax = plt.subplots() 

cax = ax.scatter(mH,mA,s=0.5,c=mHc) ### mH, mA, mHC are the dataset 
fig.colorbar(cax) 
minor_locator1 = AutoMinorLocator(6) 
minor_locator2 = AutoMinorLocator(6) 
ax.xaxis.set_minor_locator(minor_locator1) 
ax.yaxis.set_minor_locator(minor_locator2) 
ax.tick_params('both', length=10, width=2, which='major') 
ax.tick_params('both', length=5, width=2, which='minor') 
ax.set_xlabel(r'$m_H$') 
ax.set_ylabel(r'$m_A$') 
ax.set_xticks([300,600,900,1200,1500]) 
ax.set_yticks([300,600,900,1200,1500]) 

plt.savefig('mH_mA.png',bbox_inches='tight') 
plt.show() 

但我得到的情節是黑白。看起來問題在於標記大小參數,但我不知道如何糾正它。我想要有更小的標記大小。任何人都可以提供一些想法來解決這個問題。謝謝。 enter image description here

回答

3

size=0.5非常小 - 可能你看到的只是標記輪廓。我建議你增加大小了一下,也許是通edgecolors="none"關閉標記邊緣行程:

import numpy as np 
from matplotlib import pyplot as plt 

n = 10000 
x, y = np.random.randn(2, n) 
z = -(x**2 + y**2)**0.5 

fig, ax = plt.subplots(1, 1) 
ax.scatter(x, y, s=5, c=z, cmap="jet", edgecolors="none") 

enter image description here

您可能還需要與製造使用alpha=點半透明試驗參數:

ax.scatter(x, y, s=20, c=z, alpha=0.1, cmap="jet", edgecolors="none") 

enter image description here

它可以是很難得到當你有這麼多的重疊點時,散點圖看起來不錯。我會受到誘惑,展現您的數據爲2D直方圖或等高線圖來代替,或者甚至是一個散點圖和等高線圖的組合:

density, xe, ye = np.histogram2d(x, y, bins=20, normed=True) 
ax.hold(True) 
ax.scatter(x, y, s=5, c=z, cmap="jet", edgecolors="none") 
ax.contour(0.5*(xe[:-1] + xe[1:]), 0.5*(ye[:-1] + ye[1:]), density, 
      colors='k') 

enter image description here

+0

非常感謝。這非常有幫助。 –