Pandas/matplotlib 条形图,颜色由列定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26793165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas/matplotlib bar chart with colors defined by column
提问by user1617979
I am trying to make a bar chart in python, and color bars by a data frame column, this is exceedingly easy in R ggplot2, so I really do who not understand why it is so hard in matplotlib/pandas, I would like to understand how to do it and hopefully the logic as I assume it can't be hard after all
我正在尝试在 python 中制作条形图,并通过数据框列制作颜色条,这在 R ggplot2 中非常容易,所以我真的不明白为什么它在 matplotlib/pandas 中如此困难,我想了解如何做到这一点,并希望我认为的逻辑毕竟不难
Here is an example of what I want. I did in ggplot - all I want is to define the color using attributes in the data frame, the attribute can be the color string i.e. 'k','r' .. or a feature of the data say male/female, etc.

这是我想要的一个例子。我在 ggplot 中做了 - 我想要的只是使用数据框中的属性定义颜色,属性可以是颜色字符串,即 'k','r' .. 或数据的一个特征,比如男性/女性等。

this is an example of code I tried that works to generate the bars
这是我尝试生成条形的代码示例
import matplotlib.pyplot as plt
import pandas as pd
data = pd.DataFrame({'name' : ['a','b','c'],'value' : [1,2,3],
'c' : ['b','r','b']})
data.plot('name','value',kind='bar')
but I want the color defined by column 'c'
但我想要由“c”列定义的颜色
but when I add color='c' then it does not work
但是当我添加 color='c' 然后它不起作用
data.plot('name','value',color='c',kind='bar',color='c')
I tried using plot.scatter as well and it did not work either
我也尝试使用 plot.scatter 并且它也不起作用
---- Update
- - 更新
I did some more work and this works more or less, I have yet to figure out how to properly align the labels, I still do to like why I have to have a numeric x axis, when it is really categorical (again ggplot2 deals with this on its own) - but ar least this can be done in code
我做了更多的工作,这或多或少地起作用,我还没有弄清楚如何正确对齐标签,我仍然喜欢为什么我必须有一个数字 x 轴,当它真的是绝对分类的(再次 ggplot2 处理这本身) - 但至少这可以在代码中完成
fig, ax = plt.subplots()
ax.bar(range(len(data)),data['value'],width=.5,color=data['c'])
ax.set_xticks(range(len(data)))
ax.set_xticklabels( (data['name'] ))
Thanks as always
一如既往的感谢
采纳答案by user308827
Try this:
尝试这个:
data.plot('name','value',color=['r', 'g', 'b'],kind='bar')
You can give any combination of colors as a list to the color argument. If there are more bars than number of colors, the colors will just recycle.
您可以将任何颜色组合作为 color 参数的列表。如果条数多于颜色数,则颜色将循环使用。


I can also strongly recommend the excellent brewer2mpl library. It provides aesthetically pleasing color choices. The code looks like this:
我还可以强烈推荐优秀的 brewer2mpl 库。它提供了美观的颜色选择。代码如下所示:
import brewer2mpl
bmap = brewer2mpl.get_map('Set2','qualitative',3,reverse=True)
colors = bmap.mpl_colors
data.plot('name','value',color=colors,kind='bar')
resulting in:
导致:


You can get brewer2mpl from here: https://pypi.python.org/pypi/brewer2mpl/1.4
您可以从这里获取 brewer2mpl:https://pypi.python.org/pypi/brewer2mpl/1.4

