Home > Enterprise >  How to order the tick labels on a discrete axis (0 indexed like a bar plot)
How to order the tick labels on a discrete axis (0 indexed like a bar plot)

Time:11-28

I have a dataframe with this data and want to plot it with a bar graph with x-axis labels being months

import pandas as pd

data = {'Birthday': ['1900-01-31', '1900-02-28', '1900-03-31', '1900-04-30', '1900-05-31', '1900-06-30', '1900-07-31', '1900-08-31', '1900-09-30', '1900-10-31', '1900-11-30', '1900-12-31'],
        'Players': [32, 25, 27, 19, 27, 18, 18, 21, 23, 21, 26, 23]}
df = pd.DataFrame(data)

  Birthday Players
1900-01-31      32
1900-02-28      25
1900-03-31      27
1900-04-30      19
1900-05-31      27
1900-06-30      18
1900-07-31      18
1900-08-31      21
1900-09-30      23
1900-10-31      21
1900-11-30      26
1900-12-31      23

This is what I have

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

fig = plt.figure(figsize=(12, 7))
locator = mdates.MonthLocator()
fmt = mdates.DateFormatter('%b')
X = plt.gca().xaxis
X.set_major_locator(locator)
X.set_major_formatter(fmt)
plt.bar(month_df.index, month_df.Players, color = 'maroon', width=10)

but the result is this with the label starting from Feb instead of Jan

enter image description here

CodePudding user response:

I'm not familiar with matplotlib.dates but because you are using pandas there are simple ways doing what you need using pandas.

Here is my code:

import pandas as pd
import calendar
from matplotlib import pyplot as plt

# read data
month_df = pd.read_csv('data.csv', delim_whitespace=True)

# convert column to datetime
month_df["Birthday"] = pd.to_datetime(month_df["Birthday"], format="%Y-%m-%d")

# group by month and plot a bar plot
month_df.groupby(month_df["Birthday"].dt.month).sum().plot(kind="bar", color = "maroon")

# set plot properties
plt.ylabel("Count")
plt.xlabel("Birthday Month")
plt.xticks(ticks = range(0,12) ,labels = calendar.month_name[1:])

# show plot
plt.show()

Output:

output image

CodePudding user response:

Typically, matplotlib.bar does not do a very good job with datetimes for various reasons. It's easy to manually set your x tick locations and labels as below. This a fixed x datetime tick labels in matplotlib bar chart

CodePudding user response:

  • Bar plot x-axis tick locations are 0 indexed, not datetimes
  • This solution applies to any plot with a discrete axis (e.g. bar, hist, heat, etc).
  • Similar to this enter image description here

    • If there are many repeated months, where the data must be aggregated, then combine the data using pandas.DataFrame.groupby and aggregate some function like .mean() or .sum()
    dfg = df.groupby('month').Players.sum()
    
    ax = dfg.plot(kind='bar', figsize=(12, 7), rot=0, legend=False)
    
  • Related