flT
flT

Reputation: 131

Plot two pandas dataframes in one scatter plot

I have two dataframes with the same index and columns like:

import pandas as pd
dfGDPgrowth = pd.DataFrame({'France':[2%, 1.8%, 3%], 'Germany':[3%, 2%, 2.5%]}, index = [2007, 2006, 2005])
dfpopulation = pd.DataFrame({'France':[100, 105, 112], 'Germany':[70, 73, 77]}, index = [2007, 2006, 2005])

Is there a straightforward matplotlib way to create a scatter plot with x-axis % grow and y-axis population?

Edit: My dataframe has 64 columns so I wonder if it could be done with some loop so I don't have to input them all manualy.

Upvotes: 2

Views: 12443

Answers (1)

Sheldore
Sheldore

Reputation: 39042

Are you looking for something like this

import pandas as pd
import matplotlib.pyplot as plt

dfGDPgrowth = pd.DataFrame({'France':[2, 1.8, 3], 'Germany':[3, 2, 2.5]}, index = [2007, 2006, 2005])
dfpopulation = pd.DataFrame({'France':[100, 105, 112], 'Germany':[70, 73, 77]}, index = [2007, 2006, 2005])

for col in dfGDPgrowth.columns:
    plt.scatter(dfGDPgrowth[col], dfpopulation[col], label=col)
plt.legend(loc='best', fontsize=16)
plt.xlabel('Growth %')
plt.ylabel('Population')

enter image description here

Upvotes: 3

Related Questions