BBedit
BBedit

Reputation: 8057

How to show more numbers on the X-Axis of a graph?

(disclaimer: I've been learning python/programming for less than a week)

This is specifically for Python. I'm using the scipy libraries: Numpy and MatPlotLib.

As you can see, the X-Axis is showing only every 50 units. I would like it to display numbers for every 10 units.

How could I change this?

The output of my code: Python Graph

My code to generate the graph:

x1 = []
y1 = []
x2 = []
y2 = []
x3 = []
y3 = []

def ShowMyGraph():
    plot1, = pl.plot(x1,y1,'r.')
    plot2, = pl.plot(x2,y2,'b_')
    plot3, = pl.plot(x3,y3,'-')

    pl.title('Random Encounter Pattern')
    pl.xlabel('StepID')
    pl.ylabel('Danger Value')

    pl.xlim(0.0,200.0)
    pl.ylim(0.0,8000)

    pl.legend([plot1,plot2], ('Steps', 'Battles'), 'best', numpoints=1)
    pl.show()

def PlotSteps():
    x1.append(stepid)
    y1.append(danger)
    x2.append(stepid)
    y2.append(dlimit)
    x3.append(stepid)
    y3.append(dlimit)

def dosomething():
     PlotSteps()

ShowMyGraph()

Upvotes: 0

Views: 5731

Answers (1)

matt_r
matt_r

Reputation: 41

pyplot.xticks() is what you want.

To get the x axis to work in the way you're looking for, something like this should do the trick:

from numpy import arange

...

def ShowMyGraph():

...

    pl.xticks(arange(201, step=50))  # 201 since you want 200 displayed in the graph.
    pl.show()

Upvotes: 2

Related Questions