如何让棒图自动循环不同的颜色? [英] How do I make bar plots automatically cycle across different colors?

查看:369
本文介绍了如何让棒图自动循环不同的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

matplotlib 中,线图自动显示颜色。这两个线图将有不同的颜色。

In matplotlib, line plots color cycle automatically. These two line plots would have different colors.

axes.plot(x1, y)
axes.plot(x2, y)

但是,条形图不是。

axes.bar(x1, y)
axes.bar(x2, y)

如何让棒图自动循环预定义的颜色?

How do I make bar plots cycle automatically across a predefined set of colors?

推荐答案




可选:
要完全控制数字的样式现有mplstyle作为模板:
https: //github.com/matplotlib/matplotlib/tree/master/lib/matplotlib/mpl-data/stylelib

调整参数:axes.prop_cycle:循环器('color',[....])

adjust the parameter : axes.prop_cycle: cycler('color', [....])

载入你的风格:

from matplotlib import style
style.use ('PATH TO YOUR MPL STYLE')






您可以以任何方式循环浏览您的或默认的颜色周期:


You can cycle through your or the default style color cycle almost any way you want:

#!/usr/bin/python 
import matplotlib.pyplot as plt

#data
x=[1,2,4]
y=[11,12,8]
prop_iter = iter(plt.rcParams['axes.prop_cycle'])

for i in range(0,len(x)):
  plt.bar(x[i],y[i],color=next(prop_iter)['color'])

plt.show()

plt.rcParams ['axes.prop_cycle']抓取所有循环,因此您需要使用键['color']选择正确的循环仪。

plt.rcParams['axes.prop_cycle'] grabs all cycles so you need to select the correct cycler using the key ['color'].

您可以删除迭代器创建并使用列表解析和zip来创建一个衬垫:

You can drop the iterator creation and use list comprehension and zip to create one liners:

#!/usr/bin/python 
import matplotlib.pyplot as plt

x=[1,2,4]
y=[11,12,8]
prop = plt.rcParams['axes.prop_cycle']
[plt.bar(param[0],param[1],color=param[2]['color']) for param in zip(x,y,prop)]
plt.show()

在此输入图片描述

这篇关于如何让棒图自动循环不同的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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