30

I have some code:

#!/usr/bin/env python

import matplotlib
matplotlib.use("Agg")       
import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [1.2,1.9,3.1,4.2,4.8]

plt.plot(x,y)
plt.xlabel('OX')
plt.ylabel('OY')
plt.savefig('figure1.png')
plt.close()

And it gives me that figure:

my figure

As you can see, the "step" on axis X is 0.5 but I would like to set it to 1. How to make it?

When I use plt.xticks(1), it gives me errors:

Traceback (most recent call last):   File "overflow.py", line 13, in <module>
    plt.xticks(1)   File "/usr/lib/pymodules/python2.6/matplotlib/pyplot.py", line 998, in xticks
    locs = ax.set_xticks(args[0])   File "/usr/lib/pymodules/python2.6/matplotlib/axes.py", line 2064, in set_xticks
    return self.xaxis.set_ticks(ticks, minor=minor)   File "/usr/lib/pymodules/python2.6/matplotlib/axis.py", line 1150, in set_ticks
    if len(ticks) > 1: TypeError: object of type 'int' has no len()

I use Python 2.6.6 on Ubuntu 10.10.

2 Answers 2

48
plt.xticks([1, 2, 3, 4, 5])

xticks documentation.

Five x-ticks.

0
0

You can set x-ticks with every other x-tick.

import matplotlib.pyplot as plt

x = [1,2,3,4,5]
y = [1.2,1.9,3.1,4.2,4.8]

plt.plot(x, y)
plt.xticks(plt.xticks()[0][1::2]);

Another method is to set a tick on each integer position using MultipleLocator(1).

ax = plt.subplot()
ax.plot(x, y)
ax.xaxis.set_major_locator(plt.MultipleLocator(1))

img

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.