matplotlib stackplot中未对齐的bin

时间:2016-02-16 21:15:09

标签: python matplotlib

我正在尝试制作一个堆栈图,其中垃圾箱似乎没有正确对齐数据。我绘制的是当你从中心径向向外移动时球体中某物的比例。在该图的最右边部分,我可以看到错误。浅蓝色应该是一个宽度的垂直列。相反,深蓝似乎倾向于浅蓝色部分。

我认为问题在于数据间隔不均匀。例如:在半径为300的情况下,我可能会有一定的比例值。然后在半径330我可能有另一个,然后下一个在400。 enter image description here

我曾经认为stackplot可以照顾到这一点,但似乎没有。我有办法理顺这些数据列吗?

源代码:

def phaseProp(rad,phase):

#phaseLabel = np.array(['coe','en','fs','olv','maj','perov','ppv','ring','wad','per','wust','st'])
#print phaseLabel
rad = rad/1000.
phase = phase/100.
print phase[:,:]
#print phase[:,0]
fig, ax = plt.subplots(figsize = (15,10))
ax.stackplot(rad[:],phase[:,0],phase[:,1],phase[:,2],phase[:,3],phase[:,4],   \
               phase[:,5],phase[:,6],phase[:,7],phase[:,8],                   \
               phase[:,9] ,phase[:,10],phase[:,11],phase[:,12],                           \
               colors = ['gainsboro','gold','lightsage','darkorange','tomato','indianred',\
                       'darksage','sage','palevioletred','darkgrey','dodgerblue' ,'mediumblue' ,'darkblue' ])        

plt.legend([mpatches.Patch(color='gainsboro'),  
            mpatches.Patch(color='gold'), 
            mpatches.Patch(color='lightsage'),
            mpatches.Patch(color='darkorange'),
            mpatches.Patch(color='tomato'),
            mpatches.Patch(color='indianred'),
            mpatches.Patch(color='darksage'),
            mpatches.Patch(color='sage'),
            mpatches.Patch(color='palevioletred'),
            mpatches.Patch(color='darkgrey'),
            mpatches.Patch(color='dodgerblue'),
            mpatches.Patch(color='mediumblue'),
            mpatches.Patch(color='darkblue')],

            ['coe','opx','ol','gt','pv','ppv','rw','wad','fp','st','h2o','iceIh','iceVII'],\
            loc='upper center', bbox_to_anchor=(0.5, 1.127),fancybox=True, shadow=True, ncol=5,fontsize='20')

plt.ylabel(r'Phase Proportion',fontsize = 34)
plt.xlabel(r'Radius (km)',fontsize = 34)
plt.tick_params(axis='both', which='both', labelsize=32)

plt.xlim(rad[noc+1],rad[nr])
plt.ylim(0,1.0)
#ax.stackplot(rad,phase)
#plt.gca().invert_xaxis()
plt.show()

1 个答案:

答案 0 :(得分:0)

我已经看过你的问题了,我认为问题在于H20线的最后两点是(7100,0)和(7150,1)因此它只是向上倾斜看到。

然而,添加一个额外的点以给出方形边缘非常简单:

rad_amended = np.hstack((rad,rad[-1])) #extend the array by 1
rad_amended[-2] = rad[-2] +1 #alter the penultimate value
phase_amended = np.vstack((phase,phase[-1])) #extend the Y values
nr+=1 #extend the range of the x-axis
phaseProp(rad_amended,phase_amended)

这个原则可以扩展为完整的数据集,并为每个区域提供方形边,但我认为你对图的其余部分感到满意​​吗?