在 Matplotlib 中更新单条 [英] Update Single Bar in Matplotlib
问题描述
我正在使用matplotlib绘制条形图,根据我的代码中的条件,我需要更改条形之一的颜色.是否可以在不绘制新条形图的情况下更改图中单个条形的颜色,因为那样会增加复杂性?
相关:
I am plotting a bar graph using matplotlib and according to a condition in my code I need to change the color of one of the bar. Is it possible to change the color of a single bar in the plot without plotting a new bar graph, because that would increase the complexity ?
Related: how to change the color of a single bar if condition is True matplotlib
matplotlib.pyplot.bar
returns a matplotlib.container.BarContainer
, in which the individual bars are stored as matplotlib.patches.Rectangle
objects. Given that
The container can be treated as a tuple of the patches themselves. Additionally, you can access these and further parameters by the attributes
You can extract the patch for the specific bar or bars you want and change its color. An example:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 5)
bars = plt.bar(x, x)
bars[2].set_color('orange')
plt.show()
这篇关于在 Matplotlib 中更新单条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!