【发布时间】:2015-06-20 19:24:30
【问题描述】:
我使用 Pandas 创建了一个条形图,其中显示了某些国家/地区的数量如何变化,并根据每个国家/地区的大陆设置了条形颜色。我使用以下代码绘制图表。代码基于this question的第二次回复:
s = pd.Series(
listOfQuantities,
listOfCountiesNames
)
''' Assign color to each country based on the continent '''
colormapping = {'AF':'k','AS':'r','EU':'g','OC':'r','NA':'b','SA':'y'}
colorstring = ""
for country in listOfCountiesNames:
continent = countryToContinent[country]
colorstring += colormapping[continent]
pd.Series.plot(
s,
kind='bar',
color=colorstring,
grid=False,
)
我想创建一个图例,就像我在附图中显示的那样(图例不是由 python 生成的,我是手动添加的)。是否可以使用 pandas 绘制此类自定义图例,或者我可以使用其他图形库实现类似的功能?我也很感激为此类数据提供替代绘图类型的建议。
【问题讨论】:
标签: python pandas matplotlib plot