通过调用 get_lines()
函数可以很容易地检索折线图中的所有线条。我似乎找不到条形图的等效函数,即检索 AxesSubplot
中的所有 Rectangle 实例。有什么建议吗?
最佳答案
如果您想要所有条形图,只需捕获绘图方法的输出即可。它是一个包含条的列表:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = np.arange(5)
y = np.random.rand(5)
bars = ax.bar(x, y, color='grey')
bars[3].set_color('g')
如果您确实想要轴中的所有 Rectangle 对象,但这些可以不仅仅是条形,请使用:
bars = [rect for rect in ax.get_children() if isinstance(rect, mpl.patches.Rectangle)]
关于python - 如何获取 matplotlib 条形图中的所有条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22688915/