如何获取 Matplotlib 条形图中的所有条形图?
pythonserver side programmingprogramming更新于 2025/8/7 14:04:17
要获取 Matplotlib 图表中的所有条形图,我们可以使用 bar() 方法并返回条形图。−
步骤
- 设置图形大小并调整子图之间和周围的填充。
- 创建一个图形和一组子图。
- 使用 subplots() 方法创建 x 和 y 数据点。
- 制作条形图并将其存储在 bars 变量中。
- 设置特定一组条形图的表面颜色。
- 要显示图形,请使用 show() 方法。
示例
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
x = np.arange(7)
y = np.random.rand(7)
bars = ax.bar(x, y, color='red')
bars[0].set_color('yellow')
bars[4].set_color('blue')
plt.show()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

