Issue
I have a question with line graph processing in python using pandas. For example, I have an excel file with the corresponding GDP:
Year | GDP
2020 | 1000
2019 | 900
2018 | 800
......
1980 | 100
Here is my python code:
import pandas as pd
df = pd.read_csv('gdp.csv',encoding='unicode_escape',index_col=0)
df.plot()
It shows a graph, but the x-axis shows the years as 1980, 1985, 1990, 1995....2020.
Currently, I want it to print out all the years contained in the excel file ( 1980, 1981, 1982, 1983....). Can anyone just help me? Thank you.
Solution
This could work for you:
ax = df.plot()
xticks = ax.get_xticks()
ax.set_xticks(range(xticks[0], xticks[-1]+1), labels=range(1980, 2021));
fig = ax.get_figure()
fig.autofmt_xdate()
fig.set_size_inches((16,9))
Note, that too many ticks can easily become unreadable, so I used auto formatting to rotate it and increased the figure size.
Answered By - Carlos Horn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.