使用特定的值改变条形图中每个条的颜色 [英] vary the color of each bar in bargraph using particular value

查看:124
本文介绍了使用特定的值改变条形图中每个条的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似以下的条形图: http://matplotlib.org/examples/api/barchart_demo.html

I have a bar-graph like the following: http://matplotlib.org/examples/api/barchart_demo.html

在这种情况下,我们假设每组G1-G5代表每组男性参加某项考试而每组女性参加同一考试的平均分数.

In that case, let's assume each of the groups G1- G5 represent the average score that men in each group got on some exam and women in each group got on the same exam.

现在让我们说每个组还有其他功能(平均喜好度(1-5之间浮动)).

Now let's say I have some other feature associated with each group (avg. likability (float between 1-5)).

Ex: Avg Likability of men in G1 - 1.33
                   Avg Likability of women in G1 - 4.6
                   Avg Likability of men in G2- 5.0
                   .... etc...

让我们假设1-不讨人喜欢,5-非常讨人喜欢

Lets assume 1 - not likable and 5 - very likable

我想知道如何通过更改颜色示例的阴影将这种讨人喜欢的功能整合到每个栏中: 由于在上面的示例中,第1组的男性具有1.33,因此其图形的阴影将比G2的男性浅一些,由于G2的男性具有5.0的宜人性,因此其色条将是图形中最暗的红色阴影,并且对女人来说也是一样.

I want to know how I can incorporate this feature of likability into each bar by changing the shade of the color example: since men of group 1 in the above example have 1.33, their graph would be shaded a lighter shade of red than the men of G2, since men of G2 have 5.0 likability, their bar would be the darkest shade of red in the graph, and the same thing for the women.

我希望我已经说清楚了.如果有人可以将我指向matplotlib中的资源可以实现这一目标,我将不胜感激,因为我对该软件包非常陌生.

I hope I have made myself clear. I would really appreciate if someone could point me to a resource in matplotlib that could achieve this, as I am very new to this package.

谢谢.

推荐答案

bar将颜色列表作为参数

bar takes a list of colors as an argument (docs). Simply pass in the colors you want.

import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize
from numpy.random import rand

fig, ax = plt.subplots(1, 1)
# get a color map
my_cmap = cm.get_cmap('jet')
# get normalize function (takes data in range [vmin, vmax] -> [0, 1])
my_norm = Normalize(vmin=0, vmax=5)
# some boring fake data
my_data = 5*rand(5)
ax.bar(range(5), rand(5), color=my_cmap(my_norm(my_data)))

plt.show()

这篇关于使用特定的值改变条形图中每个条的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆