Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib图表轴上每个标签的不同颜色?_Python_Colors_Matplotlib_Axis Labels - Fatal编程技术网

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.设置大小(大小)
,其中
大小
随最后一个周期内的每个
勾选而变化。(参见答案中的新示例)。