具有独立条形颜色的条形图#

这是一个示例,展示了如何使用barcolorlabel参数来控制条形颜色和图例条目。请注意,以下划线开头的标签不会显示在图例中。

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

fruits = ['apple', 'blueberry', 'cherry', 'orange']
counts = [40, 100, 30, 55]
bar_labels = ['red', 'blue', '_red', 'orange']
bar_colors = ['tab:red', 'tab:blue', 'tab:red', 'tab:orange']

ax.bar(fruits, counts, label=bar_labels, color=bar_colors)

ax.set_ylabel('fruit supply')
ax.set_title('Fruit supply by kind and color')
ax.legend(title='Fruit color')

plt.show()
Fruit supply by kind and color

标签: 样式:颜色 绘图样式:条形图 级别:初学者

由 Sphinx-Gallery 生成的画廊