Reputation: 485
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()
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.
Upvotes: 0
Views: 58
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:
Upvotes: 1