Search code examples
matplotlibsubplotlogarithmyaxis

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


I am plotting a function in matplotlib using subplots (I have to use subplots because of other reasons) and like to set the y-scale to logarithmic while having the y-ticks in the color red.

I used this code:

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

The tickcolor is black although I specificly set it to red. However, when I choose a linear scale, the tickcolor is red:

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

Also, when a manually select the y-axis range, the first tick is red:

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

It has something to do with the logarithmic scale but I don't know how to solve it. Can someone help me with this?


Solution

  • The ticks you see in the first graph are minor ticks, and ax.tick_params applies to major ticks by default.

    You can specify which ticks ax.tick_params applies to using the which= argument:

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