0

Sometimes when plotting very small deviations in matplotlib, say small deviations from 1, matplotlib will automatically make ticks labeled around 0, and then write "+1" up in the left corner. Usually it will even do something worse, and subtract something like 0.999 from the ticks instead of 1. For example, the following code

import pylab as pl
import numpy as np

x=np.linspace(-10,10,100)
pl.plot(x,1+1e-4*np.sin(x))
pl.savefig('test.pdf')
pl.show()

produces the following plot. enter image description here

How do I turn off/control this feature?

What I ultimately want is for the ticks to mark only 1, so I added the command

pl.gca().set_yticks([1])

which gives the following ridiculous plot enter image description here instead of showing just the 1 in the middle of the left axis. How to fix this?

(apologizes for a bad title, didn't know what to write)

Jonathan Lindgren
  • 1,192
  • 3
  • 14
  • 31
  • I marked your question as duplicate, not to dismiss it but merely to highlight the redundancy. The earlier question has got good and thorough answers you may find valuable. – snake_charmer Oct 11 '15 at 15:01

1 Answers1

0

As previously discussed here, you could fetch the label locations from yticks add the offset you want and "manually" add its value in the top left corner.

import pylab as pl
import numpy as np

offset = 1

x=np.linspace(-10,10,100)

pl.plot(x,1+1e-4*np.sin(x))

locs,labels = yticks()
yticks(locs, map(lambda y: "%g" % y, locs - offset ) )
text(0.0, 1.01, "+%g" % offset, fontsize=10, transform = gca().transAxes)

pl.show()

enter image description here

Community
  • 1
  • 1
snake_charmer
  • 2,845
  • 4
  • 26
  • 39