plot dataframe : Use row as X axe and every line in Y axe

Viewed 41

I'm not very confident with python, most of the time I get a code on the web (frequently here). I have a dataframe that I read in panda and I just can't plot it in the way I want (I can't plot it at all, by the way).

Here a sample of my df :

    date    00:00   00:30   01:00   01:30   02:00   02:30
16/03/2021  0.518   0.679   0.516   0.633   0.606   0.775
17/03/2021  0.751   0.956   0.823   0.975   1.5     0.73
18/03/2021  0.925   0.733   0.825   1.489   0.762   0.686
19/03/2021  0.834   0.726   0.887   0.712   0.769   0.713
20/03/2021  0.735   0.799   0.732   0.803   0.629   0.811
21/03/2021  0.61    0.425   0.645   0.518   0.451   0.629
22/03/2021  0.401   0.525   0.472   0.518   0.508   0.432

As you see, the very first column is the dates and the very first row is hours of these dates (by step of 30 minutes). The original file cover all 24 hours and is separate by spaces (" ").

I want to plot a curve by day and, for example, 3 days by plot (and I'll make some subplots, I have already done that before, it should be ok...).

Here the beginning of my code :

import pandas as pd
df = pd.read_csv("file.csv", sep=" ",on_bad_lines='skip',usecols=range(48))

Yeah, that's not so much, I know.

1 Answers

These days, I spent a lot of time on python to learn and I now have a solution for this post that I forgot since. If someone needs this some day, here is a code that creates a dataframe line per line, then plot it on a figure with subplot (but we can easily put all data on the same plot when needed):

import pandas as pd
import matplotlib.pyplot as plt



df_all = pd.read_csv('df_test.csv')
df_all.index = df_all['Unnamed: 0']
df_all.drop(['Unnamed: 0'], axis=1, inplace=True)

df = {}
x = 0
for x in range (6) :
    
    globals()[f"df{x}"] = df_all.iloc[x]
    x += 1


fig, ax = plt.subplots((x+1),1,sharex=True,sharey=True,figsize=(18,15), dpi=200)


y = 0
for y in range(x):
    
    ax[y].plot(globals()[f"df{y}"].index, globals()[f"df{y}"].iloc[0:])
    y += 1
Related