1

I am trying to create line chart using pandas data frame and matplotlib. I am using following code to create line chart.

import pandas as pd
import matplotlib.pyplot as plt

data = {
    'Quarter': ['Q1-2018', 'Q2-2018', 'Q3-2018', 'Q4-2018', 'Q1-2019'],
    'Data': [256339, 265555, 274880, 211128, 0]
}
dataset2 = pd.DataFrame(data=data)
ax3 = dataset2[['Quarter', 'Data']].plot.line(x='Quarter', y='Data', 
legend=False)
ax3.margins(x=0.1)
plt.show()

Which produces following result enter image description here

As you can see, start and end of line is starting and ending at edge of the plot. What I am trying to achieve is to have some space at the start and end of line chart like below. enter image description here

I tried setting x margin by using ax3.margins(x=0.1) but it does not do any thing.

How do I add some space to start and end of chart so that line does not stick to edges?

6
  • Can you create a minimal reproducible example? Commented Jan 30, 2019 at 9:09
  • Hi DavidG, please check the edit. Commented Jan 30, 2019 at 9:20
  • 1
    I cannot reproduce this. Running your code, I get this picture. There must be something in your code that you are not showing or you have a matplotlibrc file... Commented Jan 30, 2019 at 12:22
  • @ThomasKühn It's not OP's fault. They use a newer pandas version than you. Both versions have problems with categorical plots though, just on different levels. Commented Feb 6, 2019 at 19:15
  • @ImportanceOfBeingErnest I see. Then it's good to be aware of that. If you happen to know at which version the change has occurred, maybe it would be good to include that in your answer? Commented Feb 6, 2019 at 19:22

1 Answer 1

2

In pandas 0.23 you would get the correct plot with margins as desired, yet without labels. This "bug" seems to have been fixed in pandas 0.24, at the expense of another undesired behaviour.

That is, pandas fixes the limits of categorical plots and sets the ticklabels to the positions that would look correct if limits are not changed. While you could in theory unfix the limits (ax.set_xlim(None, None)) and let the axes autoscale (ax.autoscale()), the result will be a incorrectly labelled plot.
I doubt there is any reasoning behind this, it's rather an oversight in the pandas source. This pandas issue best describes the problem, which then boils down to this 5 year old issue.

In any case, for categorical plots, consider using matplotlib directly. It's categorical feature is pretty stable by now and easy to use:

import pandas as pd
import matplotlib.pyplot as plt

data = {
    'Quarter': ['Q1-2018', 'Q2-2018', 'Q3-2018', 'Q4-2018', 'Q1-2019'],
    'Data': [1,3,2,4,1]
}
df = pd.DataFrame(data=data)

plt.plot("Quarter", "Data", data=df)

plt.show()

enter image description here

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

1 Comment

Thank you for detailed answer. I end up using ax.set_xlim() function to set the offset. But as you suggested this will produce incorrectly labelled plot, I will be using matplotlib directly instead of Pandas built-in functionality.

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.