【发布时间】:2017-08-18 12:13:11
【问题描述】:
我一直在寻找有关如何根据“第三”列的值在散点图上设置点的解决方案。我找不到任何现成的解决方案,因此我构建了自己的解决方案,我想分享它(也许有人会使用它):) 如果这里不是发帖的地方,那我很抱歉,请删除它。
假设有一个数据框 'scatterData' 如下:
lad2014_name Male Female Result
0 ABERDEEN CITY 95154 97421 -21.78
1 ABERDEENSHIRE 101875 105141 -13.10
2 ADUR 24047 26574 -16.16
3 ALLERDALE 38346 40192 -44.56
.
.
.
499 AMBER VALLEY 48720 51502 -3.56
我想在散点图上绘制男性和女性,但是我还想通过更改标记的颜色来显示“结果”是阴性还是阳性。所以我这样做了:
def resultColour(z):
colour = '#e31a1c'
if z > 0:
colour = '#1f78b4'
return colour
#Plotting the scatter plot
plt.figure(figsize=(12,10))
for index, row in scatterData.iterrows():
x = row.Male
z = row.Result
y = row.Female
t = resultColour(z)
plt.scatter(x, y, c=t,s=85)
plt.xlabel('X axis lable',fontsize=15)
plt.ylabel('Y axis lable',fontsize=15)
plt.title('Plot title',fontsize=18)
plt.plot()
它产生如下散布
【问题讨论】:
标签: python pandas matplotlib colors scatter