'How to convert str to date? and I want the date appears mm/yy as the x-axis in the plot (python)

How to convert str to date? I also want the date only appears mm/yy as the x-axis in the plot.

The data shows as below. 21/12/2020, 0, 22/12/2020, 1, 23/12/2020, 0, 24/12/2020, 0, 25/12/2020, 1,

Below is the code I used.

import numpy as np
import matplotlib.dates as mdates
from datetime import datetime as dt

import csv
with open('p211.csv', newline='') as f:
reader = csv.reader(f)
rain = []
for row in reader:
   rain_t = float(row[1]) 
   rain.append(rain_t)

import csv
with open('p211.csv', newline='') as f:
 reader = csv.reader(f)
 date = []
 for row in reader:
  date_t=str(row[0])
  date.append(date_t)

P=np.array(rain)

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(20, 12))
ax.bar(x=date, height=P, color='royalblue')
ax.legend()
ax.set_ylabel('Inputs [mm/day]')

plt.show() 


Solution 1:[1]

using datetime package

from datetime import datetime
datetime.strptime(dtime, '%d/%m/%Y') # dtime = '21/12/2020'

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 Amit