fjurt
fjurt

Reputation: 793

Using year-week column as x-axis for bar chart in matplotlib

I try to plot a bar plot with matplotlib to show the change of an aggregated number on a weekly basis over time (my dataset has roughly 1000 weeks). Normally, when I plot such time-series, I have daily data and I convert the dates with pd.to_datetime(). But in the weekly case, don't know how to specify the format in order to get an accurate x-axis labels with the years and not also with the weeks which overlay on each other.

This would be a simple data set example:

    YearWeek    Output
0   2022-01     7.3
1   2022-02     5.3
2   2022-03     7.2
3   2022-04     4.8
4   2022-05     5.8
5   2022-06     9.2
6   2022-07     5.3
7   2022-08     5.3
8   2022-09     7.5
9   2022-10     9.2
10  2022-11     5.4
11  2022-12     4.8

enter image description here

In the case of more weeks, the x-axis is not readable and the labels lie on each other. I try to only get yearly labels.

This would be the plot for the real data set I am using: enter image description here

For reproducibility:

import pandas as pd
import matplotlib.pyplot as plt

df1 = pd.DataFrame({
    'YearWeek':['2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06', '2022-07', '2022-08', '2022-09', '2022-10', '2022-11', '2022-12'],
    'Output':[7.3, 5.3, 7.2, 4.8, 5.8, 9.2, 5.3, 5.3, 7.5, 9.2, 5.4, 4.8]}) 

plt.bar(df1['YearWeek'], df1['Output'])

Thanks a lot!

Upvotes: 0

Views: 2103

Answers (2)

Krishnakanth Allika
Krishnakanth Allika

Reputation: 790

Edit1: Added plt.xticks(df1['YearWeek'],df1['YearWeek'].str[:4]) to show only the year instead of year-week on x-axis.

Edit2: x-axis will show each year only once.

You can rotate x-axis ticks by 90 degrees or 45 degrees using plt.xticks(rotation=90).

For example:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df1 = pd.DataFrame({
    'YearWeek':['2021-01', '2021-02', '2021-03', '2021-04', '2021-05', '2021-06', '2021-07', '2021-08', '2021-09', '2021-10', '2021-11', '2021-12', '2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06', '2022-07', '2022-08', '2022-09', '2022-10', '2022-11', '2022-12'],
    'Output':[6.3, 5.3, 7.2, 4.8, 8.8, 9.2, 5.3, 5.3, 4.5, 9.2, 5.4, 4.8, 7.3, 5.3, 8.2, 4.8, 5.8, 9.2, 5.3,2.3, 7.5, 9.2, 5.4, 9.8]}) 

df1['Year'] = df1['YearWeek'].str[:4]

plt.bar(df1['YearWeek'], df1['Output'])
# plt.xticks(rotation=90)
plt.xticks(df1['YearWeek'],df1['Year'])
plt.xticks(np.arange(6, len(df1), 12))
plt.show()

enter image description here

Output of Edit2:
enter image description here

Upvotes: 1

eshirvana
eshirvana

Reputation: 24603

one solution could be to rotate them :

...
plt.xticks(rotation=45)
plt.show()

enter image description here

Upvotes: 1

Related Questions