0

So I have this dataframe: img

And I want to create a graph with the xlabels corresponding with the months the records are in. I've tried:

df['date'] = pd.to_datetime(df['date'])
df.set_index('date')
l = sns.lineplot(x=df.index,y='Appliances',data=df, sort=False, color='grey')
plt.xticks(rotation=45)
# Set the locator
locator = mdates.MonthLocator()  # every month
# Specify the format - %b gives us Jan, Feb...
fmt = mdates.DateFormatter('%b')
X = plt.gca().xaxis
X.set_major_locator(locator)
X.set_major_formatter(fmt)
plt.show()

which gives me: enter image description here

As you can see, the xlabels are not showing.. I'm not sure why. Thanks!

Daichi
  • 309
  • 1
  • 2
  • 10
  • Possible duplicate of this [post](https://stackoverflow.com/questions/30133280/pandas-bar-plot-changes-date-format) – Grazina Sep 13 '19 at 10:25
  • What happens if you just use 10 lines of the dataframe? – Spinor8 Sep 13 '19 at 12:04
  • 2
    A [mcve] would allow to find out why this doesn't work. Also see [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – ImportanceOfBeingErnest Sep 13 '19 at 12:13
  • 1
    `df.set_index('date')` this returns a new dataframe without changing `df`. The dataframe is still indexed by integer so your date formatter has no effect. Change it to either `df = df.set_index('date')` or `df.set_index('date', inplace=True)` – Code Different Sep 13 '19 at 13:05

1 Answers1

0

Thanks for the comments, I figured out the problem was that i didnt set the new dataframe to have a changed index:

df = df.set_index('date')
Daichi
  • 309
  • 1
  • 2
  • 10