-1

I am trying to make an area plot. However the x axis of the graph shows 1,2,3 rather than the year. How can I change this? I saw some related questions here, however the code there is a bit too complicated for me. My code is:

import matplotlib as mpl
import matplotlib.pyplot as plt
areaplot=r'data.xlsx'
df_areaplot = pd.read_excel(areaplot)
df_areaplot.plot(kind='area')
plt.title('SDG Spending Trend')
plt.ylabel('Amount Spent')
plt.xlabel('Years')
plt.show()

areaplot

Data

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Tachi
  • 13
  • 3
  • 1
    do you have a column with the year in your dataframe? if so, you can do something like: `df_areaplot.plot(kind='area', x='Year', y='Amount Spent'`) (obviously editing those labels with the names of your columns) – tmdavison May 25 '21 at 14:41
  • I think I need to index the column? If I input x='2015' then it shows an error. Basically I do not want to have SDG on my area plot(right now its a blue line on top of the x axis) and want to have years rather than 1,2,3. I think that means skipping 1 row and 1 column. I have added a picture called 'data' which shows what I am working with. – Tachi May 25 '21 at 14:54
  • 1
    Set the column with the years as the index before plotting. `df.set_index('SDG', inplace=True)` – Trenton McKinney May 25 '21 at 15:00
  • Also `df_areaplot.plot(kind='area', x='SDG')` will work as in the duplicate, without setting the index. – Trenton McKinney May 25 '21 at 15:25

1 Answers1

0

As @tmdavison points out in the comment, you can use column names to determine axes, even with specified lists of columns, i.e.:

df_areaplot.plot(kind='area',
  x='BDG',
  y=['Citizen Initiatives',
     'Education and Employability',
     'Enviromental Sustainability',
     'Woman Empowerment'])
user213305
  • 402
  • 2
  • 10