简体   繁体   中英

Plot graph of the same variable from two different dataframes in Python

I have two dataframes which have the same columns names but different values. Consider df1 with shape (5, 2) and df2 with shape (4,2). I want to plot a graph of the variable 'F1_S' vs 'ID' from df1 and df2 in the same graph. Note that variable 'ID' on the x-axis is categorical. I used the following to plot one of the dataframes.

names=list(df1['ID'])
values = list(df1['F1_S'])
fig, axs = plt.subplots(figsize=(20,20), sharey=True)
axs.scatter(names,values)

When I add the other dataframe to this, the shapes of the dfs causes an error.

Note that not all values of 'ID' in df1 will have corresponding 'F1_S' values in df2 and vice-versa.

EDIT:
Consider the two dataframes df1 and df2.

df1=pd.DataFrame({ 'ID': ['A1','A2','A6','A7','A9'], 'F1_S': [23,75,42,77,56] },  columns=['ID', 'F1_S'])
df2=pd.DataFrame({ 'ID': ['A3','A4','A5','A8'], 'F1_S': [66,43,56,86] },  columns=['ID', 'F1_S'])
import pandas as pd
import matplotlib.pyplot as plt

# TEST

df1=pd.DataFrame({ 'ID': ['A1','A2','A6','A7','A9'], 'F1_S': [23,75,42,77,56] },
                 columns=['ID', 'F1_S'])
df2=pd.DataFrame({ 'ID': ['A3','A4','A5','A8'], 'F1_S': [66,43,56,86] },
                 columns=['ID', 'F1_S'])

fig, axs = plt.subplots(figsize=(20,20), sharey=True)
plt.title('Testplot')
axs.scatter(df1['ID'],df1['F1_S'])
axs.scatter(df2['ID'],df2['F1_S'])

I added both pandas dataframes to a scatterplot, this did not raise a shape error. Testplot link please klick here

Does this help you? If not, please post your complete code and the error message. Have a nice day :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM