-1

So, I have obtained a frequency plot of the 'sample_date' column: https://stackoverflow.com/a/68420301/15934571.
I want only monthly ticks displayed. The problem is that the dates on the x-axis after applying MonthLocator and DateFormatter turned into irrelevant dates (like 01-01-1970, 01-02-1970 etc.). I don't have any idea why or how to fix it. Will be grateful for any help!

Here is my code:

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('dataset.csv')
data['sample_date'] = pd.to_datetime(data['sample_date'])

ax = data['sample_date'].value_counts().sort_index().plot(kind='bar') 

ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))

Here is the sample of the bigger dataset:

,sequence_name,sample_date,epi_week,epi_date,lineage
1,England/MILK-1647769/2021,2021-06-07,76,2021-06-06,C.37
2,England/MILK-156082C/2021,2021-05-06,71,2021-05-02,C.37
3,England/CAMC-149B04F/2021,2021-03-30,66,2021-03-28,C.37
4,England/CAMC-13962F4/2021,2021-03-04,62,2021-02-28,C.37
5,England/CAMC-13238EB/2021,2021-02-23,61,2021-02-21,C.37
0,England/PHEC-L304L78C/2021,2021-05-12,72,2021-05-09,B.1.617.3
1,England/MILK-15607D4/2021,2021-05-06,71,2021-05-02,B.1.617.3
2,England/MILK-156C77E/2021,2021-05-05,71,2021-05-02,B.1.617.3
4,England/PHEC-K305K062/2021,2021-04-25,70,2021-04-25,B.1.617.3
5,England/PHEC-K305K080/2021,2021-04-25,70,2021-04-25,B.1.617.3
6,England/ALDP-153351C/2021,2021-04-23,69,2021-04-18,B.1.617.3
7,England/PHEC-30C13B/2021,2021-04-22,69,2021-04-18,B.1.617.3
8,England/PHEC-30AFE8/2021,2021-04-22,69,2021-04-18,B.1.617.3
9,England/PHEC-30A935/2021,2021-04-21,69,2021-04-18,B.1.617.3
10,England/ALDP-152BC6D/2021,2021-04-21,69,2021-04-18,B.1.617.3
11,England/ALDP-15192D9/2021,2021-04-17,68,2021-04-11,B.1.617.3
12,England/ALDP-1511E0A/2021,2021-04-15,68,2021-04-11,B.1.617.3
13,England/PHEC-306896/2021,2021-04-12,68,2021-04-11,B.1.617.3
14,England/PORT-2DFB70/2021,2021-04-06,67,2021-04-04,B.1.617.3

Ameerah
  • 23
  • 5

1 Answers1

0

Setting Datetime X Labels for Bar Chart in Matplotlib

I'm not sure why the DateFormatter is not working, it could be something to do with how pandas is setting the x_ticklabels. If you look at the __ call __ method in the class, it simply returns a num2date -> strftime and returns a string of the datetime. Perhaps somebody else can comment about why this isn't working, I am curious to know. Regardless, you can easily set the labels yourself.

I see that you're interested in only locating/labeling months in inervals of 1 month. The issue here is that in a bar plot, the x_axis is not a continuous and dependent variable. The x_axis labels the bars. Since it's not a continuous variable it's not really easy to say where the ticks should be located. You may need to do this manually or use a different chart type; such as a scatter plot or histogram.

Here is an example of manually labeling your xaxis.

enter image description here

data = pd.read_csv('stack_overflow.csv')
data['sample_date'] = pd.to_datetime(data['sample_date'])

df = data['sample_date'].value_counts().sort_index()
ax = df.plot(kind='bar') 

ax.set_xticks(np.arange(len(df)) - 0.75) #fixes offset made by rotation of labels
ax.set_xticklabels(df.index.strftime('%d-%m-%Y'), rotation=45)

#style plot
[ax.spines[s].set_visible(False) for s in ['top', 'right']]
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
ax.tick_params(axis='both', bottom=False, left=False)
ax.grid(axis='y', dashes=(8,3), color='gray', alpha=0.4)
Coup
  • 675
  • 4
  • 6