如何在 Python 中用图案和颜色对条形图进行编码?

How to code bar charts with patterns along with colours in Python?

以下代码用四种颜色打印条形图:

import matplotlib.pyplot as plt

barlist=plt.bar([1,2,3,4], [1,2,3,4])
barlist[0].set_color('r')
barlist[1].set_color('g')
barlist[2].set_color('y')
plt.show()

问题是如果用黑白印刷,它们看起来会很像。因此,我的意图是制作一个类似这样的图表:

没有 看起来像上面那样(请原谅草率的插图),但想法是让每个条形在灰度下看起来都不一样。

有什么方法可以在 Python 中实现吗?

您可以在编辑填充参数的条形内部设置不同的图案:

import matplotlib.pyplot as plt

barlist = plt.bar([1,2,3,4], [1,2,3,4], color=['r','b','y','g'])

barlist[0].set_hatch('//')
barlist[1].set_hatch('.')
barlist[2].set_hatch('*')
barlist[3].set_hatch('o')
plt.show()