【发布时间】:2016-08-31 04:42:57
【问题描述】:
我正在使用 Pandas 0.18。我有一个这样的数据框:
code proportion percent_highcost total_quantity
A81 0.7 76 1002
A81 0.0 73 1400
我正在绘制这样的散点图:
colours = np.where(df['proportion'] > 0, 'r', 'b')
df.plot.scatter(y='percent_highcost', x='total_quantity', c=colours)
这很好用,但我不知道如何添加图例来指示两种颜色的含义。
我试过 plt.legend(['Non-dispensing', 'dispensing'], loc=1) 但这会产生奇怪的结果 - 我猜是因为只有一个系列:
谁能给点建议?
【问题讨论】:
-
我建议使用
fig.colorbar(sc),其中sc是艺术家scatter。您可能必须使用ax.scatter而不是df.plot才能轻松访问该艺术家。
标签: python pandas matplotlib