2

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.

flT
  • 121
  • 1
  • 2
  • 10
  • I'm new to the forum and just voting my threads down without a comment explaining what's wrond does not help me improve my questions. Was my question unclear or too nooby? – flT Jan 24 '19 at 10:24
  • Possible duplicate of [Plotting multiple dataframes using pandas functionality](https://stackoverflow.com/questions/37414008/plotting-multiple-dataframes-using-pandas-functionality) – Thomas Kühn Jan 24 '19 at 10:27
  • 1
    @flT I didn't downvote, but it's usually a good idea to show your attempts at solving this and any errors/problems you encounter – DavidG Jan 24 '19 at 10:56
  • @DavidG Point noted. Next time will provide my nooby solution :) Hope the other downvotes were for the same matter, otherwise this system makes it difficult for new users to understand their mistakes... Anyhow, thanks Bazingaa, solution works like charm! Much appreciated! – flT Jan 24 '19 at 12:17

1 Answers1

3

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

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • Exactly this output. Thanks! However my dataframe is with 64 columns. I am looking for an authomated way to get this plot without manually entering each column. Guess it wouldn't be too difficult with some loop but I cannot guess the synthaxis.. – flT Jan 24 '19 at 10:38
  • 2
    @flT: Check my edited answer using the for loop. You can turn off the legend if you want by just commenting out the `plt.legend` line – Sheldore Jan 24 '19 at 10:51