[英]Matplotlib Tick lines and labels of different color
I am trying to plot a 2d image using a specific colormap in matplotlib and I need to change the color of the ticks to white.我正在尝试使用 matplotlib 中的特定颜色图绘制二维图像,我需要将刻度线的颜色更改为白色。 But when I do so, the labels or tick numbers change of color too and they become invisible on a white background.但是当我这样做时,标签或刻度数字也会改变颜色,并且它们在白色背景上变得不可见。
How can I change only the color of the tick lines and not the color of their label or associated number???如何仅更改刻度线的颜色,而不更改其标签或相关数字的颜色???
Example例子
import matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()
ax=fig.add_subplot(111)
pc=ax.pcolormesh(xf,yf,F, vmin=F.min(), vmax=F.max(), cmap=colormap)
fig.colorbar(pc,orientation='horizontal',label=r'Intensity',format='%1.1f', ticks=np.arange(-0.1,0.6,0.1), pad=0.1)
ax.set_xlabel("RA offset [arcsec]")
ax.set_ylabel("DEC offset [arcsec]")
ax.set_xlim(3.0,-3.0)
ax.set_ylim(-3.0,3.0)
########## ticks
### x axis
major_ticks = np.arange(-2.0, 3.0, 2.0)
minor_ticks = np.arange(-3.0, 4.0, 1.0)
ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)
after making your Axes
and setting the appropriate tick locations, you can access the ticks with ax.get_xticklines()
and ax.get_yticklines()
.制作Axes
并设置适当的刻度位置后,您可以使用ax.get_xticklines()
和ax.get_yticklines()
访问刻度。 If you iterate over all the xticks
or yticks
, you can change their colours using set_color()
如果您遍历所有xticks
或yticks
,则可以使用set_color()
更改它们的颜色
For minor ticks, you can use ax.xaxis.get_minorticklines()
.对于次要刻度,您可以使用ax.xaxis.get_minorticklines()
。 Note that you could also use ax.xaxis.get_majorticklines()
in place of ax.get_xticklines()
if you prefer.请注意,如果您愿意,也可以使用ax.xaxis.get_majorticklines()
代替ax.get_xticklines()
。
for tick in ax.get_xticklines():
tick.set_color('red')
for minortick in ax.xaxis.get_minorticklines():
minortick.set_color('r')
for tick in ax.get_yticklines():
tick.set_color('green')
Although the question is answered, I think that it is simpler to use问题虽然回答了,但我觉得用起来更简单
ax.tick_params(axis='y', which='both', color='w')
to change the colour of the ticks of the y axis without changing the labels colour (the argument which
chooses major
, minor
or both
).改变y轴的刻度线的颜色而不改变标签的颜色(该参数which
选择major
, minor
或both
)。
To change the colour of the ticks and labels the command would be要更改刻度和标签的颜色,命令将是
ax.tick_params(axis='y', which='both', colors='w')
By simpler, I mean that it uses fewer lines to achieve the same result.简单来说,我的意思是它使用更少的行来实现相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.