-1

I have a pandas dataframe as below:

Well Name READTIME WL
0 A 02-Jul-20 12
1 B 03-Aug-22 18
2 C 05-Jul-21 14
3 A 03-May-21 16
4 B 01-Jan-19 19
5 C 12-Dec-20 20
6 D 14-Nov-21 14
7 A 01-Mar-22 17
8 B 15-Feb-21 11
9 C 10-Oct-20 10
10 D 14-Sep-21 5
groupByName = df.groupby(['Well Name', 'READTIME'])

After grouping them by 'Well Name' and Readtime, i got the following:

Well Name   READTIME    WL  
A           2020-07-02  12
            2021-05-03  16
            2022-03-01  17
B           2019-01-01  19
            2021-02-15  11
            2022-08-03  18
C           2020-10-10  10
            2020-12-12  20
            2021-07-05  14
D           2021-09-14  5
            2021-11-14  14

I have got the following graph by running this code:

sns.relplot(data=df, x="READTIME", y="WL", hue="Well Name",kind="line", height=4, aspect=3)

enter image description here

I want to have a separate graph for each "Well Name" and saved it as a pdf. I will really appreciate your help with this. Thank you

Rabinzel
  • 7,757
  • 3
  • 10
  • 30

2 Answers2

1

To separate out the plots, you can iterate over the four unique Well Names in your dataset and filter the dataset for each Well Name before plotting:

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

# I saved your data as an Excel file
df = pd.read_excel('Book1.xlsx')
print(df)

# Get the set of unique Well Names
well_names = set(df['Well Name'].to_list())
for wn in well_names:
    # Create dataframe containing only rows with this Well Name
    this_wn = df[df['Well Name'] == wn]
    # Plot, save, and show
    sns.relplot(data=this_wn, x="READTIME", y="WL", hue="Well Name",kind="line", height=4, aspect=3)
    plt.savefig(f'{wn}.png')
    plt.show(block=True)

This generated the following 4 image files:

A.png B C D

For saving in a PDF file, please see this answer.

ljdyer
  • 1,946
  • 1
  • 3
  • 11
  • Thank you. Its just i had to use this "well_names = set(df['Well Name'].values.tolist())" as I was getting this error if I used .to_list() method, "AttributeError: 'Series' object has no attribute 'to_list'" – Adil Javed Chaudhary Aug 27 '22 at 06:15
  • Is it possible that you're not on the latest version of pandas? `to_list()` works in 1.4.3: https://pandas.pydata.org/docs/reference/api/pandas.Series.to_list.html – ljdyer Aug 27 '22 at 06:22
0

In this case, specifying a row results in a faceted graph.

sns.relplot(data=df, x="READTIME", y="WL", hue="Well Name", kind="line", row='Well Name', height=4, aspect=3)

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32