简体   繁体   English

绘制后更改直方图的颜色

[英]Change color of histogram after it was plotted

How can I change the color of a histogram after I draw it? 绘制后如何改变直方图的颜色? (using hist) (使用历史记录)

z = hist([1,2,3])
z.set_color(???) < -- Something like this

also how can I check what color is the histogram 还有我怎么检查直方图是什么颜色

z = hist([1,2,3])
color = z.get_color(???) < -- also Something like this

Thank you. 谢谢。

Such functions exist. 存在这样的功能。 You just need to store the patches returned by hist and access the facecolor of each of them: 您只需要存储hist返回的patches并访问每个patchesfacecolor

import matplotlib.pyplot as plt
n, bins, patches = plt.hist([1,2,3])
for p in patches:
    print p.get_facecolor()
    p.set_facecolor((1.0, 0.0, 0.0, 1.0))

Output: 输出:

(0.0, 0.5, 0.0, 1.0)
(0.0, 0.5, 0.0, 1.0)
(0.0, 0.5, 0.0, 1.0)
(0.0, 0.5, 0.0, 1.0)
(0.0, 0.5, 0.0, 1.0)
(0.0, 0.5, 0.0, 1.0)
(0.0, 0.5, 0.0, 1.0)
(0.0, 0.5, 0.0, 1.0)
(0.0, 0.5, 0.0, 1.0)
(0.0, 0.5, 0.0, 1.0)

Note that you get one patch per bin. 请注意,每个仓位只有一个补丁。 By default hist plots 10 bins. 默认情况下, hist绘制10个bin。 You might want to define it differently using plt.hist([1,2,3], bins=3) . 您可能想使用plt.hist([1,2,3], bins=3)不同的定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM