Save each subplot individually with pandas and matplotlib

Viewed 76

I want to make a line graph for each row in a pandas dataframe and save it with the filename being the name of the first cell of the row. I have made some progress but I had to switch my columns and rows which is not ideal.

My attempt:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("data/line_graph_c.csv", sep=';')
f = pd.DataFrame(data)
df.Date = pd.to_datetime(df.Date)
df.set_index('Date', inplace=True)
dfs = df.stack().reset_index().rename(columns={'level_1': 'field_name', 0: 'crop_health'})
sns.relplot(data=df_select, x='Date', y='crop_health', col='field_name', col_wrap=2, kind='line')

The data looks as follow

Date            a   b   c   d
0   2021/01/05  1   2   3   4
1   2021/01/06  4   3   2   1
2   2021/01/07  5   6   7   8
3   2021/01/08  8   7   6   5

The ideal will be to make a line graph for each row from the following data:


    date       2021/01/05   2021/01/06  2021/01/07  2021/01/08
0   field_name NaN          NaN         NaN         NaN
1   a          1.0          2.0         3.0         4.0
2   b          4.0          3.0         2.0         1.0
3   c          5.0          6.0         7.0         8.0
4   d          8.0          7.0         6.0         5.0

And save each line graph individually with the field name as the name of the image.

Any help will be appreciated

0 Answers
Related