在matplotlib中分别设置xticklabel的颜色 [英] Set color for xticklabels individually in matplotlib

查看:1468
本文介绍了在matplotlib中分别设置xticklabel的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,如何给标签 a, b, c提供单独的颜色(例如, a为绿色, b为蓝色, c为红色)?

 导入numpy为np 
导入matplotlib.pyplot为plt
图,ax = plt.subplots()
p = plt.boxplot(np.random.normal(size =(10,3)))
ax.set_xticklabels(list( abc))
plt.show()

解决方案

代码:

 导入numpy为np 
导入matplotlib.pyplot为plt
图,ax = plt.subplots()
p = plt.boxplot(np.random .normal(size =(10,3)))
ax.set_xticklabels(list( abc))

[t.set_color(i)for(i,t)in
zip([['red','green','blue'],ax.xaxis.get_ticklabels())]

plt.show()

给我:




How can I give the labels "a", "b", "c" individual colors (e.g. "a" in green, "b" in blue, "c" in red) in the example below?

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
p = plt.boxplot(np.random.normal(size=(10,3)))
ax.set_xticklabels(list("abc"))
plt.show()

解决方案

The code:

import numpy as np
import matplotlib.pyplot as plt
    fig, ax = plt.subplots()
    p = plt.boxplot(np.random.normal(size=(10,3)))
    ax.set_xticklabels(list("abc"))

[t.set_color(i) for (i,t) in
 zip(['red','green','blue'],ax.xaxis.get_ticklabels())]

plt.show()

Gives me:

这篇关于在matplotlib中分别设置xticklabel的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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