Rab*_*ski 3 python colors matplotlib axis-labels
是否可以为轴上的某些标签设置不同的颜色?
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_yticks([0,1,2])
ax1.set_yticklabels(['red','red', 'blue'], color='blue')
#What I would like to do
ax1.set_yticklabels(['red','red', 'blue'], colors=['red','red','blue']) <-- doesn't work
plt.show()
Run Code Online (Sandbox Code Playgroud)
有没有办法实现我想要的?

您可以Tick使用以下方法访问对象的所有属性:
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.set_yticks([0,1,2])
ax1.set_yticklabels(['red','red', 'blue'], color='blue')
colors=['red','red','blue']
for color,tick in zip(colors,ax1.yaxis.get_major_ticks()):
tick.label1.set_color(color) #set the color property
plt.show()
Run Code Online (Sandbox Code Playgroud)
最后一个循环也可用于更改其他属性,例如,标签的大小:
colors=['red','red','blue']
sizes=[10,20,30]
for color,size,tick in zip(colors,sizes,ax1.yaxis.get_major_ticks()):
tick.label1.set_color(color) #set the color
tick.label1.set_size(size) #set the size
Run Code Online (Sandbox Code Playgroud)
最后一个例子的输出类似于:
| 归档时间: |
|
| 查看次数: |
1653 次 |
| 最近记录: |