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:
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. You might want to define it differently using plt.hist([1,2,3], bins=3)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.