1

I am trying to plot two columns from two different dataframes in one graph. I have figured out the code to plot it in one but the x axis needs to be in ascending order. Can we do that in this case?

df1=pd.DataFrame({ 'ID': ['A1','A2','A6','A7','A9'], 'F1_S': [23,75,42,77,54] },
                 columns=['ID', 'F1_S'])
df2=pd.DataFrame({ 'ID': ['A3','A4','A5','A8'], 'F1_S': [66,43,56,86] },
                 columns=['ID', 'F1_S'])
x1 = list(df1['ID'])
y1= list(df1['F1_S'])
lists1 = sorted(zip(*[x, y1]))
x1, y1 = zip(*sorted(zip(x1, y1)))

x2 = list(df2['ID'])
y2= list(df2['F1_S'])
lists2 = sorted(zip(*[x2, y2]))
x2, y2 = zip(*sorted(zip(x2, y2)))

plt.plot (x1,y1)
plt.plot(x2,y2)
1
  • 1
    @sobek but the axis labels aren't in order, look more closely. also, there's an A9 that should come after A8 Commented Sep 6, 2018 at 19:58

3 Answers 3

1

One possible solution is to plot the concatenated dataframe against a numeric index and set the ticks to categories.

import matplotlib.pyplot as plt
import pandas as pd

df1=pd.DataFrame({ 'ID': ['A1','A2','A6','A7','A9'], 'F1_S': [23,75,42,77,54] },
                 columns=['ID', 'F1_S'])
df2=pd.DataFrame({ 'ID': ['A3','A4','A5','A8'], 'F1_S': [66,43,56,86] },
                 columns=['ID', 'F1_S'])
# Rename the column of one of the dataframe
df2 = df2.rename(columns={'F1_S' : 'F2_S'})
# Concatenate dataframes, reset the index (such that it will be numeric)
df3 = pd.concat([df1.set_index("ID"), df2.set_index("ID")], sort=True).sort_index().reset_index()

#Plot each column without nan values, against the numeric index
plt.plot(df3["F1_S"].dropna())
plt.plot(df3["F2_S"].dropna())
# Set labels to category names
plt.xticks(range(len(df3)), df3["ID"])
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Not sure if this is entirely what you want but here goes:

import pandas as pd
import matplotlib.pyplot as plt


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

df1.set_index('ID', inplace=True)
df1.index.name = None
df1.columns = ['F1_S_1']
df2.set_index('ID', inplace=True)
df2.index.name = None
df2.columns = ['F1_S_2']
df = pd.concat([df1, df2], sort=True).sort_index().interpolate(limit_area='inside')
ax = df.plot()
ax.set_xticklabels(['A0'] + df.index.tolist())
plt.show()

The result is this:

enter image description here

df.plot() seems to have an off-by-one error with the axis labeling, i had to hack a fake element into the xlabel list to get it to display correctly.

Comments

0

Why not just concatenate the two?

pd.concat([df1[['ID', 'F1_S']], df2[['ID', 'F1_S']])\
  .sort_values(by='ID')\
  .plot(x='ID', y='F1_S')

Edit: to make them separate lines plotted in the same axis:

fig, ax = plt.subplots()
df1.sort_values(by='ID')\
   .plot(x='ID', y='F1_S', ax=ax)
df2.sort_values(by='ID')\
   .plot(x='ID', y='F1_S', ax=ax)

The call to df.plot accepts the same kwargs asplt.plot, so you can specify different colors for each.

3 Comments

This way you would get one line instead of two.
I wanted the graph in two different colors to be able to differentiate which points belong to which dataframe
The updated code is plotting point with x = df1['ID'], y = df2['F1_S'] and not x = df2['ID'], y = df2['F1_S']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.