8

I'm trying to set margins so all my points are visible when plotting with matplotlib but its doesn't seem to correctly add them. Below is my code and output.

I'm using IPython with the %matplotlib magic command.

Is there something that I'm doing obviously wrong?

import matplotlib.pyplot as plt
import pandas as pd


d = pd.DataFrame(pd.Series(range(10))*2)
a = d.plot(style = "o-")
a.set_axis_bgcolor('g') 
a.margins(.05)

Matplotlib Output

1
  • 2
    a.autoscale(tight=False) Commented Jan 16, 2016 at 21:45

1 Answer 1

8

see the following documentation for set_ylim and set_xlim http://matplotlib.org/api/axes_api.html?highlight=set_xlim

d = pd.DataFrame(pd.Series(range(10))*2)
a = d.plot(style = "o-")
a.set_axis_bgcolor('g')
a.set_ylim([-1,19])
a.set_xlim([-1,11])
a.margins(.05)

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

How can I do this without setting x and y lims? For instance I would like to do this using categorical variables
Add this: a.autoscale(tight=False)
Kevin, That's what I was missing. Thank you!
@Kevin The solution in your comment is actually better than the one that you give in the answer. Maybe it would be worth adding there for completeness ...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.