'Place string to X-Axis on Matplotlib

I have a question for Matplotlib. I am quite new to Matplotlib and Python.

So I have a series of Dataframe being read from MySQL in the form of:

Date
28.03.2022
28.03.2022
...

I am trying to plot the date on the x-axis of my graph using matplotlib.

I have tried to write it as a string to convert it

date_string=str(df1['Date'])
date_string=date_string.replace('.','/')
date_string=date_string.replace('\n',' ')
days = date2num(datetime.datetime.strptime(date_string, '%d/%m/%Y'))

ValueError: time data '0 28/03/2022 2 28/03/2022 4 28/03/2022 6 28/03/2022 8 28/03/2022 10 28/03/2022 12 28/03/2022 14 28/03/2022 16 28/03/2022 18 28/03/2022 20 28/03/2022 22 28/03/2022 24 28/03/2022 26 28/03/2022 28 28/03/2022 30 28/03/2022 32 28/03/2022 34 28/03/2022 36 28/03/2022 38 28/03/2022 40 28/03/2022 42 28/03/2022 44 28/03/2022 46 28/03/2022 48 28/03/2022 50 28/03/2022 52 28/03/2022 Name: Date, dtype: object' does not match format '%d/%m/%Y'

The original form when I only turn it into a string is as shown below:

0     28.03.2022
2     28.03.2022
4     28.03.2022
6     28.03.2022
8     28.03.2022
10    28.03.2022
12    28.03.2022
14    28.03.2022
16    28.03.2022
18    28.03.2022
20    28.03.2022
22    28.03.2022
24    28.03.2022
26    28.03.2022
28    28.03.2022
30    28.03.2022
32    28.03.2022
34    28.03.2022
36    28.03.2022
38    28.03.2022
40    28.03.2022
42    28.03.2022
44    28.03.2022
46    28.03.2022
48    28.03.2022
50    28.03.2022
52    28.03.2022
Name: Date, dtype: object

Am I misunderstanding something here?



Solution 1:[1]

The date_string in your code contains all components of your dataframe df1 (index + all rows of 'Date' column) which doesn't match the specified date format. The optimal solution is given by r-beginners in the comments.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hamed Rabiei