Tikiyetti
Tikiyetti

Reputation: 485

How to edit the axis labels of my plot in pandas

I have a pretty simple histogram plot right of (year,month on the x-axis) dates now using those code:

#!/usr/bin/python
import pandas as pd
import matplotlib.pyplot as plt

file = 'dates.txt'

df = pd.read_csv("dates.txt", header=None)
df["dates"] = pd.to_datetime(df[0])
grouped = df["dates"].groupby([df["dates"].dt.year, df["dates"].dt.month]).count().plot(kind="bar")
plt.show()

Which looks like this: enter image description here

I'm looking to replace the numerical representation of the months with the corresponding month abbreviation. For example, (2015, 7) --> (2015, Jul). So I tried the following code apply(lambda x: calendar.month_abbr[x]) in my script below:

#!/usr/bin/python
import pandas as pd
import matplotlib.pyplot as plt
import calendar

file = 'dates.txt'

df = pd.read_csv("dates.txt", header=None)
df["dates"] = pd.to_datetime(df[0])
#print(df["dates"])
#df.groupby(df["dates"].dt.month).count().plot(kind="bar")
graph = df["dates"].groupby([df["dates"].dt.year, df["dates"].dt.month.apply(lambda x: calendar.month_abbr[x])]).count().plot(kind="bar", y="Calls")
graph.set_ylabel("Call Volume")
graph.set_xlabel("Date")
plt.show()

But that gave me some unexpected output. The months converted but now they're out of order.

enter image description here

Upvotes: 0

Views: 58

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You can try groupby the month periods and manually relabel the ticks:

new_df = df["dates"].groupby(df.dates.dt.to_period('M')).count()

# new label
labels = new_df.index.strftime("%Y-%b")

# plot and re-label:
fig, ax = plt.subplots(figsize=(16,9))
new_df.plot(kind="bar", ax=ax)
ax.set_xticklabels(labels)
plt.show()

Output:

enter image description here

Upvotes: 1

Related Questions