[英]How to plot this DataFrame easier in Pandas
This is my DataFrame:这是我的数据帧:
I plotted my df in Pandas with this code:我使用以下代码在 Pandas 中绘制了我的 df:
Armenia = [100.0, 21.943089, 23.344123]
Moldova = [100.0, 25.468598, 25.856620]
Ukraine = [100.0, 45.253380, 26.266646]
Vietnam = [100.0, 491.680706, 1083.782579]
Oman = [100.0, 277.381353, 659.887243]
Bangladesh = [100, 280.025959, 609.648111]
df = pd.DataFrame({'Vietnam' : Vietnam, 'Ukraine' : Ukraine, 'Bangladesh' : Bangladesh, 'Oman' : Oman,'Armenia': Armenia,'Moldova': Moldova}, index = ['1990', '2005', '2017'])
ax = df.plot.line(rot=0)
plt.title('Top 3 best and worst countries in combatting CO2 emission above 5 mt CO2', fontsize=14)
plt.xlabel('years', fontsize=14)
plt.ylabel('relative amount of CO2 emitted in %', fontsize=12)
plt.grid(True)
Output:输出:
Is there any easier way to do this?有没有更简单的方法来做到这一点? Because it takes a lot of time to write all the data.
因为写入所有数据需要很多时间。
I really appreciate any comments.我真的很感激任何评论。 Thanks!
谢谢! :)
:)
Your dataframe is in wide format so I changed it to long format using pd.melt
( docs )您的数据
pd.melt
采用宽格式,因此我使用pd.melt
( docs ) 将其更改为长格式
I plotted the measurements using barplots since I thought you could see the evolution of CO2 emissions better like this.我使用条形图绘制了测量结果,因为我认为您可以像这样更好地看到 CO2 排放的演变。
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
dic = {'country' : ['Vietnam', 'Oman', 'Bangladesh', 'Ukraine', 'Moldova', 'Armenia'],
'base' : [100.0, 100.0, 100.0, 100.0, 100.0, 100.0],
'RA05' : [491, 277, 280, 45, 25, 21],
'RA17' : [1083, 659, 609, 26, 25, 23]
}
df = pd.DataFrame(dic)
df_melted = pd.melt(df, id_vars='country', value_name='Value', var_name='Measurement')
print(df_melted)
ax = sns.barplot(data=df_melted, x='country', y='Value', hue='Measurement')
ax.set(xlabel='Country', ylabel='Relative ammount of CO2 emitted in %', title='Top 3 best and worst countries in combatting CO2 emission above 5 mt CO2')
plt.show()
I'll update my answer once I figure out how to plot it with a line plot in pandas.一旦我弄清楚如何用熊猫中的线图绘制它,我将更新我的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.