分组条形图中特定于样式的颜色

时间:2017-02-02 16:10:52

标签: python matplotlib

我有一个包含多个组/系列的条形图。每组都有自己的颜色。为每个组指定确切的颜色很简单,但有哪些“标准”方法可以找到适合图表样式(http://matplotlib.org/users/style_sheets.html)的颜色(颜色图,调色板,循环,等等......)?例如,"dark_background"样式与不同颜色的样式匹配得更好。如果我没有为每个组指定颜色,那么条形颜色基于样式,但所有组都是相同的,这是我不想要的。

更新:要查看其行为,请尝试调整示例http://matplotlib.org/examples/api/barchart_demo.html 删除显式栏颜色并设置样式。

import numpy as np
import matplotlib.pyplot as plt

plt.style.use('dark_background')  # SETTING STYLE!!!

N = 5
men_means = (20, 35, 30, 35, 27)
men_std = (2, 3, 4, 1, 2)

ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind, men_means, width, yerr=men_std)  # NO BAR COLOR!!!

women_means = (25, 32, 34, 20, 25)
women_std = (3, 5, 2, 3, 3)
rects2 = ax.bar(ind + width, women_means, width, yerr=women_std)  # NO BAR COLOR !!!

# add some text for labels, title and axes ticks
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind + width / 2)
ax.set_xticklabels(('G1', 'G2', 'G3', 'G4', 'G5'))

ax.legend((rects1[0], rects2[0]), ('Men', 'Women'))


def autolabel(rects):
    """
    Attach a text label above each bar displaying its height
    """
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.05*height,
                '%d' % int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

0 个答案:

没有答案