2016-06-21 80 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

非常感谢。这非常有帮助。 –