I am new to python and need your help. I have several dataframes. Each dataframe is for one day. So I am using for loop to plot for all dataframe. For each plot I want to add the date in my title. Can anyone help me. I have created a variable ‘date_created and assigned the dates which I want. I want my title to look like below : ‘Voltage vs time 28-01-2022’
JavaScript
x
10
10
1
for df in (df1,df2,df3,df4,df5,df6,df7,df8):
2
y = df[' Voltage']
3
x = df['time']
4
date_created = [ '28-01-2022, 29-01-2022, 30-01-2022, 31-08-2022, 01-02-2022, 02-02-2022, 03-02-2022, 04-02-2022' ]
5
fig, ax = plt.subplots(figsize=(18,7))
6
plt.plot(x,y, 'b')
7
plt.xlabel("time")
8
plt.ylabel(" Voltage [V]")
9
plt.title("Voltage vs time")
10
Advertisement
Answer
To make code work more effective it would be better to create a dictionary of dataframes and dates (if you haven’t got date column in your dataframe).
JavaScript
1
2
1
dict = {df1: '28-01-2022', df2: '29-01-2022', df3: '30-01-2022'}
2
Than we will use for loop for elements of this dictionary
JavaScript
1
9
1
for key, value in dict.items():
2
y = key['Voltage']
3
x = key['time']
4
fig, ax = plt.subplots(figsize=(18,7))
5
plt.plot(x,y, 'b')
6
plt.xlabel("time")
7
plt.ylabel(" Voltage [V]")
8
plt.title(f"Voltage vs time {value}")
9
Hope this will work for you!