0

Using matplotlib, I want to eliminate the yticks and ytick labels from a loglog plot. It works for a regular plot, but not for loglog.

The following code

from matplotlib import pyplot as plt
fig, ax = plt.subplots()
ax.plot([2,4],[8,10])
ax.set_xticks([])
ax.set_yticks([])

gives a desirable result.

ax.plot allows removal of both xticks and yticks

But if instead I want loglog scaling, it fails.

from matplotlib import pyplot as plt
fig, ax = plt.subplots()
ax.loglog([2,4],[8,10])
ax.set_xticks([])
ax.set_yticks([])

for ax.loglog, ax.set_xticks([]) and does not work

I have tried multiple different fixes, and nothing seems to work. I have a more complicated example where

from matplotlib import pyplot as plt
import numpy as np
xvals=np.logspace(-1,1,20)
yvals1=np.array([0.03950564, 0.03953078, 0.03957078, 0.03962743, 0.03970915,
       0.03981691, 0.03994918, 0.04010086, 0.04026356, 0.04042849,
       0.04058817, 0.04073742, 0.04087351, 0.04099616, 0.04110702,
       0.04120875, 0.04130422, 0.04139636, 0.04148823, 0.04158317])
yvals2=np.array([0.0223383 , 0.02247799, 0.02265306, 0.02287054, 0.02314318,
       0.0234758 , 0.02387278, 0.02434108, 0.02489343, 0.02555092,
       0.02634133, 0.02729466, 0.02843651, 0.02978005, 0.03131751,
       0.03301349, 0.03480207, 0.03658879, 0.03825972, 0.03970347])
fig, axs = plt.subplots(1,3 , figsize = (15,5))
axs[0].loglog(xvals,yvals1,c = 'green')
axs[0].loglog(xvals,yvals2,c = 'tab:blue')
axs[0].set_ylim(0.02,0.07)
axs[0].set_xlim(0.1,10)
axs[0].set_xticks([])
axs[0].set_yticks([])
axs[1].loglog(xvals,yvals1,c = 'green')
axs[1].loglog(xvals,yvals2,c = 'tab:blue')
axs[1].set_ylim(0.02,0.07)
axs[1].set_xlim(0.1,10)
axs[1].set_xticks([])
axs[1].set_yticks([])
axs[2].loglog(xvals,yvals1,c = 'green')
axs[2].loglog(xvals,yvals2,c = 'tab:blue')
axs[2].set_ylim(0.02,0.07)
axs[2].set_xlim(0.1,10)
axs[2].set_xticks([])
axs[2].set_yticks([])
plt.subplots_adjust(wspace=0.5)

ax.loglog can remove xticks but not yticks

My third example is closer to the actual code I am trying to run (I have changed the x and y values).

I have tried many things, including changing order of function calls, using the set_yticklabels() function, setting labels=None, setting major=False and minor=False etc.

What I am expecting when calling

ax.set_yticks([])

is to see tick marks , but no tick labels. I want to eliminate the numbers on the side of the plots.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158

1 Answers1

0

You need to turn off the major x|y ticks AND the minor x|y ticks as well

enter image description here

import matplotlib.pyplot as plt
fig, ax = plt.subplots(layout='constrained')
ax.loglog((1,10000), (1,10000))
ax.set_xticks([1, 100, 10000]) ; ax.set_xticks([], minor=1)
ax.set_yticks([]) ; ax.set_yticks([], minor=1)
plt.show()
gboffi
  • 22,939
  • 8
  • 54
  • 85