如何使用matplotlib在ylable上绘制多种颜色?

时间:2016-05-04 16:22:12

标签: matplotlib

我有想要在子图上绘制的数据列表。然后我想用不同的颜色来标记ylable。请参阅下面的简单代码示例: -

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('yellow red blue')
plt.show()

这会产生以下图像: - example figure

在结果图像中,ylable被命名为“黄色红色蓝色”,全部为黑色。但我希望这个标签的颜色如下: -

'黄色',黄色, 红色的“红色” 和'蓝色'蓝色。

是否可以使用matplotlib?

1 个答案:

答案 0 :(得分:1)

没有。您无法使用单个文本对象执行此操作。您可以手动添加三个不同的标签,即:

import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
ax = plt.gca()
ax.text(-0.1, 0.4, 'yellow', color='yellow', rotation=90, transform=ax.transAxes)
ax.text(-0.1, 0.5, 'red', color='red', rotation=90, transform=ax.transAxes)
ax.text(-0.1, 0.6, 'blue', color='blue', rotation=90, transform=ax.transAxes)
plt.show()

enter image description here