Skip to content Skip to sidebar Skip to footer

Set Axis Limits In Matplotlib Graph

I have a five period of data with their probabilities and I would like to simulate it with 20000 times with the Poisson distribution, I want the x-axis start from 0-1, I tried but

Solution 1:

You'll want to use matplotlib's set_xlim method for the axes. Have a look at some of their examples, this one for example, to understand better what you can do with the module.

For your code, you'll need to add:

ax = plt.gca()
ax.set_xlim(0,1)

As for some optimizations, I see that you're adding the same module just under different aliases (e.g., you have numpy and its alias np imported, which are also imported by pylab). It tells me you don't have a lot of experience with the language yet. As you continue to learn, you'll eventually reduce all those imports to just a few, e.g.

import numpy as np
import matplotlib.pyplotas plt

and you'll call the correct functions belonging to these namespaces (e.g. plt.show(), rather than pylab.show - pylab is no more than a thin veil over a package of numpy and matplotlib.

There are a few more optimizations you could make to your code (e.g. vectorizing the loop), but given your current level, I think this would make it too complex. Besides, the loop does make it more explicit what you're doing.

Maybe just one tip: in python, when you want to update a variable that is numeric (int, float, ...), you could just do:

inventory += production - order

It saves you from typing inventory again and thus less chances of errors in the future if you want to make changes to your code.

Post a Comment for "Set Axis Limits In Matplotlib Graph"