gpt4 book ai didi

python - 在 Matplotlib 中更新单条

转载 作者:行者123 更新时间:2023-12-03 23:49:46 25 4
gpt4 key购买 nike

我正在使用 matplotlib 绘制条形图,并且根据代码中的条件,我需要更改其中一个条形的颜色。是否可以在不绘制新条形图的情况下更改图中单个条形的颜色,因为这会增加复杂性?

相关:how to change the color of a single bar if condition is True matplotlib

最佳答案

matplotlib.pyplot.bar 返回 matplotlib.container.BarContainer ,其中单个条形存储为 matplotlib.patches.Rectangle 对象。鉴于

The container can be treated as a tuple of the patches themselves. Additionally, you can access these and further parameters by the attributes



您可以为您想要的特定条或条提取补丁并更改其颜色。一个例子:

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()

enter image description here

关于python - 在 Matplotlib 中更新单条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59347557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com