刻度标签中的 Matplotlib 多种颜色 [英] Matplotlib multiple colours in tick labels

查看:58
本文介绍了刻度标签中的 Matplotlib 多种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在标签内使用不同颜色的刻度标签

Is it possible to have an tick labels formatted with different colours within the label

例如使用这样的标签:

labels = ['apple - 1 : 7', 'orange - 5 : 10']

例如,数字1&5 出现蓝色和 7 &10 出现红色?

Such that the numbers 1 & 5 appear blue and 7 & 10 appear red?

推荐答案

如果您使用 matplotlib 的面向对象界面来绘制数据,您可以使用 get_xticklabels

If you are using the object-oriented interface of matplotlib to plot your data, you can use the get_xticklabels and get_yticklabels to access the labels of each axis and then change the color of the ones you want.

我误解了最初的问题.请参阅下文以获得更合适的答案.

EDIT : I misunderstood the original question. See below for a more appropriate answer.

一种可能性是使用文本实例删除原始标签并创建伪标签.这样,您可以在内部创建具有不同颜色的文本.它不是简单的方法(您将不得不编写大量代码,尤其是如果您要对多个标签进行多色处理),但是下面是您可以执行的示例.

One of the possibility is to remove the original labels and create pseudo-labels using a text instance. This way, you can create text with different color inside. It is not straightforwad (you will have to write a lot of code, especially if you have a lot of labels you want to multi-colorize), but below is an example of what you can do.

想法是使用 matplotlib.offsetbox.TextArea 方法以所需的颜色创建每个标签的不同部分,然后使用 matplotlib.offsetbox.HPacker 方法(我通过这篇文章).

The idea is to create the different parts of each label in the color you want using the matplotlib.offsetbox.TextArea method, and then merge them using the matplotlib.offsetbox.HPacker method (I discovered the HPacker method via this post).

import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, HPacker

fig = plt.subplots(1)

ax.bar([0, 1], [20, 35], 0.35, color='0.5', yerr=[2, 3], ecolor='k')
ax.set_xlim([-0.2, 1.7])
ax.set_xticks([]) # empty xticklabels

# apple label
abox1 = TextArea("apple - ", textprops=dict(color="k", size=15))
abox2 = TextArea("1 ", textprops=dict(color="b", size=15))
abox3 = TextArea(": ", textprops=dict(color="k", size=15))
abox4 = TextArea("7 ", textprops=dict(color="r", size=15))

applebox = HPacker(children=[abox1, abox2, abox3, abox4],
                  align="center", pad=0, sep=5)

# orange label
obox1 = TextArea("orange - ", textprops=dict(color="k", size=15))
obox2 = TextArea("5 ", textprops=dict(color="b", size=15))
obox3 = TextArea(": ", textprops=dict(color="k", size=15))
obox4 = TextArea("10 ", textprops=dict(color="r", size=15))

orangebox = HPacker(children=[obox1, obox2, obox3, obox4],
                    align="center", pad=0, sep=5)

anchored_applebox = AnchoredOffsetbox(loc=3, child=applebox, pad=0., frameon=False,
                                      bbox_to_anchor=(0.1, -0.07),
                                      bbox_transform=ax.transAxes, borderpad=0.)

anchored_orangebox = AnchoredOffsetbox(loc=3, child=orangebox, pad=0., frameon=False,
                                       bbox_to_anchor=(0.6, -0.07),
                                       bbox_transform=ax.transAxes, borderpad=0.)

ax.add_artist(anchored_applebox)
ax.add_artist(anchored_orangebox)

plt.show()

哪个给:

这篇关于刻度标签中的 Matplotlib 多种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆