简体   繁体   English

在一个散点图中绘制两个熊猫数据帧

[英]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? 是否有直接的matplotlib方法创建具有x轴增长百分比和y轴填充的散点图?

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. 编辑:我的数据框有64列,所以我想知道是否可以通过一些循环来完成,所以我不必手动输入它们。

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')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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