Python matplotlib图表轴上每个标签的不同颜色?
轴上的某些标签可能有不同的颜色吗Python matplotlib图表轴上每个标签的不同颜色?,python,colors,matplotlib,axis-labels,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'], col
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()
导入matplotlib.pyplot作为plt
图=plt.图()
ax1=图add_子批次(111)
ax1.set_yticks([0,1,2])
ax1.设置标签(['red','red','blue',color='blue'))
#我想做什么
ax1.设置标签(['red','red','blue',colors=['red','red','blue'])您可以使用以下方法访问勾选对象的所有属性:
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()
最后一个循环也可用于更改其他属性,例如标签的大小:
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
最后一个示例的输出类似于:
是否也可以独立更改每个元素的大小?@LuisJose您当然可以:勾选.label1.设置大小(大小)
,其中大小
随最后一个周期内的每个勾选而变化。(参见答案中的新示例)。