I have two dictionaries of dataframes that are generated from separate for loops.
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
Control_Keys = ('C1','C2','C3','C4')
Patient_Keys = ('P1','P2','P3','P4','P5','P6')
df_columns = ['Reads','Score']
C_dict = {}
P_dict = {}
# Generating dictionaries of dataframes
for i in Control_Keys:
C_dict[i] = pd.DataFrame(np.random.randint(0,100,size=(100,2)), columns=df_columns)
for i in Patient_Keys:
P_dict[i] = pd.DataFrame(np.random.randint(0,100,size=(100,2)), columns=df_columns)
I want to plot their data using the same axis on the same graph, where the x-axis is 'Reads' and the y-axis is 'Score'. I also want to be able to differentiate between the two groups, (not individual data) where all data belonging to Controls are blue, and Patients are red. I am able to plot these individually, but I have not been able to find a reliable way to merge these two graphs into one.
CodePudding user response:
I would slightly redesign your dataframe in order to achieve your plotting goal. I would first assign the key as a column (optionally, if you need to distinguish) and also assign the column as another property to help with color distinguishing (hue
)
Below I combine all the C's, P's and assign two new columns: Table
and Hue
c = pd.DataFrame()
for key in Control_Keys:
C_dict[key]['Table'] = key
C_dict[key]['Hue'] = 'C'
c = c.append(C_dict[key])
p = pd.DataFrame()
for key in Patient_Keys:
P_dict[key]['Table'] = key
P_dict[key]['Hue'] = 'P'
p = p.append(P_dict[key])
Then you plot them and assign hue
parameter for color assignment and palette
to change the color:
sns.scatterplot(x='Reads', y='Score', hue='Hue', data=p.append(c), palette=['Red', 'Blue'])
plt.show()