0

I would like to plot some data with a fractional logscale, such that the y axis has the ticks at 10^(-0.1), 10^(-0.2), 10^(-0.3), etc. The problem is that when I plot my data, there are only ticks at 10^0 and 10^-1, which leaves the slope of the line too slight to see.

Is is possible to set a fractional logscale this way?

Thanks

Eddy
  • 6,661
  • 21
  • 58
  • 71
  • Related questions that may solve your problem: http://stackoverflow.com/questions/6682784/how-to-reduce-number-of-ticks-with-matplotlib and http://stackoverflow.com/questions/12608788/changing-the-tick-frequency-on-x-or-y-axis-in-matplotlib –  Jun 26 '14 at 11:29
  • Thanks, I had a look but still couldn't figure out to do it. I tried this: `ax.YAxis.set_ticks([10**0,10**(-0.1),10**(-0.2),10**(-0.3)])` but that didn't work. I'm new to matplotlib – Eddy Jun 26 '14 at 11:48
  • Did you try any of the other suggestions at the linked pages? Also, it should be `ax.yaxis`; `ax.YAxis` doesn't even exist (though perhaps it did in an old matplotlib version). –  Jun 26 '14 at 11:58
  • Or you may simply want to scale the y-axis: `ax.set_ylim(1e-3, 1)`. –  Jun 26 '14 at 12:05
  • How do you use `ax.set_ylim`? It says `invalid syntax` when I try to use it. – Eddy Jun 27 '14 at 11:28
  • This is what I've tried: `import matplotlib.pyplot as plt; fig, ax = plt.subplots(); plt.plot(X,Y); plt.yscale('log'); ax.set_yticks([10**0,10**(-0.1),10**(-0.2),10**(-0.3)])`. But that just adds extra ticks. I need to rescale the plot – Eddy Jun 27 '14 at 11:31

1 Answers1

1

It sounds like you want tick labels, not the tick marks in particular. In most figures, the minor tick marks are already there where you want them.

The following may then work, though I would think there's an easier way. Note that I'm applying labels to the minor tick marks only: the (two) major tick marks already have a label. Unfortunately, the fonts of the two types of tick marks are not the same; I think that's a result of the LaTeX equation usage.

import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
X = np.logspace(0, 3)
Y = X**-0.2
plt.plot(X,Y)
plt.yscale('log')
yticks = np.linspace(-0.1, -0.9, 9)
ax.set_yticks(10**yticks, minor=True)
ax.set_ylim(0.1, 1)
ax.set_yticklabels(['$10^{{{:.1f}}}$'.format(ytick) for ytick in yticks], minor=True)
plt.show()

which results in:

enter image description here

For the issue of the different label fonts, you can manually change the major tick labels:

ax.set_yticks([1, 0.1])
ax.set_yticklabels(['$10^0$', '$10^{-1}$'])

(and probably the same for the x-axis).

  • Thanks, I'll try this for now. If no one else can think of an automatic way to do it, then I'll mark this as accepted. – Eddy Jun 27 '14 at 14:33
  • By the way, it should be 9 ticks, not 10, in `yticks = np.linspace(-0.1, -0.9, 9)`. I couldn't edit your submission because at least 6 characters must be changed for a valid edit. – Eddy Jun 27 '14 at 14:34
  • `10^-0.5` appears twice – Eddy Jun 29 '14 at 13:09
  • Whoops, you're right. Looks like matplotlib rounds those ticks, so with the ten ticks across the interval at [-0.1, -0.1888, -0.2777, -0.3666, -0.4555, -0.5444, ..., -0.9], those got rounded to [-0.1, -0.2, -0.3, -0.4, -0.5, -0.5, .... -0.9]. Corrected now. –  Jun 30 '14 at 08:48