matplotlib 子图中的对数 y 轴刻度颜色不起作用

Logarithmic y-axis tick color in matplotlib subplots doesn't work

我正在使用子图在 matplotlib 中绘制一个函数(由于其他原因我必须使用子图)并且喜欢将 y 尺度设置为对数,同时让 y 刻度为红色。

我使用了这个代码:

import matplotlib.pyplot as plt

x_data = np.arange(500.0, 900.0, 1.0)
def func(x):
    return a*(x/500.0)**(-b)

fig, ax = plt.subplots()
ax.plot(x_data, 10*func(x_data))
ax.set_yscale('log')
ax.tick_params('y', colors='r')
plt.show()

image using code above

tickcolor 是黑色的,尽管我特意将它设置为红色。 但是,当我选择线性刻度时,tickcolor 是红色的:

import matplotlib.pyplot as plt

x_data = np.arange(500.0, 900.0, 1.0)
def func(x):
    return a*(x/500.0)**(-b)

fig, ax = plt.subplots()
ax.plot(x_data, 10*func(x_data))
ax.set_yscale('linear')
ax.tick_params('y', colors='r')
plt.show()

image using code above

此外,当手动 select y 轴范围时,第一个刻度为红色:

import matplotlib.pyplot as plt

x_data = np.arange(500.0, 900.0, 1.0)
def func(x):
    return a*(x/500.0)**(-b)

fig, ax = plt.subplots()
ax.plot(x_data, 10*func(x_data))
ax.set_yscale('linear')
ax.tick_params('y', colors='r')
plt.show()

image using code above

跟对数刻度有关,不知道怎么解决。有人可以帮我解决这个问题吗?

您在第一张图中看到的报价是 次要 报价,默认情况下 ax.tick_params 适用于主要报价。

您可以使用 which= 参数指定 ax.tick_params 适用于哪些报价:

ax.tick_params('y', which="both", colors='r')