1

My x axis labels are

0.00 0.01 0.02 0.03 

etc. How can I format it as:

0 0.01 0.02 0.03

well I tried those:

plt.xlim([0,0.03])

plt.xticks(np.arange(0, 0.03, 0.01))

neither does work. I think this should be fixed, 0.00 is meaningless.

Emmet B
  • 5,341
  • 6
  • 34
  • 47
  • 1
    I think you'd find *a lot* of people who disagree with `0.00` being meaningless. It's certainly not a bug that should be fixed. – jedwards Mar 19 '15 at 08:21
  • @jedwards sure, I respect that point of view. But I would doubt any mathematician would defend 0.00. Also MATLAB and gnuplot put it as 0 always. – Emmet B Mar 19 '15 at 08:25

3 Answers3

5

You could use a custom formatter for your x-axes that prints integers as such:

from matplotlib.ticker import FuncFormatter

def my_formatter(x, pos):
    if x.is_integer():
        return str(int(x))
    else:
        return str(x)

formatter = FuncFormatter(my_formatter)

fig, ax = plt.subplots()
ax.xaxis.set_major_formatter(formatter)
plt.show()
halex
  • 16,253
  • 5
  • 58
  • 67
3

Have you used plt.yticks([0, 0.01, 0.02, 0.03], ['0', '0.01', '0.0.2', '0.03']) ?

mohan3d
  • 225
  • 1
  • 3
  • 9
2

To change a specific tick, you could either use a custom formatter, or something like the following:

import matplotlib.pyplot as plt

plt.plot([1,3,2])

plt.draw()      # Note, this line is important
ax = plt.gca()  # and you don't need this line if you already have an axis handle somewhere
labels = [l.get_text() for l in ax.get_xticklabels()]
labels[0] = '0'
ax.set_xticklabels(labels)

plt.show()
jedwards
  • 29,432
  • 3
  • 65
  • 92