从枢轴表中删除零值以某种方式造成了另一个问题
我从Python课程中进行了一项分配,以挖掘一组数据,该数据包括(几乎)从1960年到2011年全球所有国家 /地区的二氧化碳排放。在一个特定国家的二氧化碳生产的增长,我想避免将零插入图。这是我一直在使用的代码。
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sn
import numpy as np
# Creating DataFrame
Data = pd.read_excel('CO2 Sorted Data.xlsx')
df = pd.DataFrame(Data, columns=['Year','CountryName','Region','IncomeType','CO2Emission','CO2EmCMT'])
df.replace(0,np.nan,inplace=True)
print(df)
# Creating the Pivot Table
pvt = df.pivot_table(index=['Year'],columns=['CountryName'],values='CO2Emission',aggfunc='sum')
# Creating the Graph
pvt2 = pvt.reindex()
CO2Country = input('Input Country Name = ')
remove_zero=pvt2[CO2Country]
rz1=[i for i in remove_zero if i !=0]
plt.plot(rz1,c='red')
plt.title('CO2 Emission of ' +CO2Country +' (1960-2011)', fontsize=10)
plt.xlabel('Year',fontsize=10)
plt.ylabel('CO2 Emission (kiloton)')
plt.grid(True)
plt.show
例如,如果我输入Aruba,则输出看起来像这样。 aruba的线图
但是,x-轴仅显示在该年的'数字'数据要求,而不是一年本身。除了将零更改为NAN之外,我不知道这触发了什么,但这在我看来没有任何意义。与1986 - 2011年一样,我该如何使X轴显示真实的一年?
I have an assignment from my Python class to mine a set of data consisting CO2 emissions from (almost) all the countries in the world from 1960 to 2011. One of the task i've been working on is to produce a line graph that represents the growth of CO2 production in a specific country, and i'd like to avoid inserting zeros into the graph. Here is the code i've been using.
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sn
import numpy as np
# Creating DataFrame
Data = pd.read_excel('CO2 Sorted Data.xlsx')
df = pd.DataFrame(Data, columns=['Year','CountryName','Region','IncomeType','CO2Emission','CO2EmCMT'])
df.replace(0,np.nan,inplace=True)
print(df)
# Creating the Pivot Table
pvt = df.pivot_table(index=['Year'],columns=['CountryName'],values='CO2Emission',aggfunc='sum')
# Creating the Graph
pvt2 = pvt.reindex()
CO2Country = input('Input Country Name = ')
remove_zero=pvt2[CO2Country]
rz1=[i for i in remove_zero if i !=0]
plt.plot(rz1,c='red')
plt.title('CO2 Emission of ' +CO2Country +' (1960-2011)', fontsize=10)
plt.xlabel('Year',fontsize=10)
plt.ylabel('CO2 Emission (kiloton)')
plt.grid(True)
plt.show
If i input Aruba for example, output would look like this.
Line Graph of Aruba
However, the x-axis only shows the 'number' of years on the data requested, not the year itself. I have no clue on what triggers this other than changing the zeroes to NaN, but that doesn't make any sense in my mind. How can i make the x-axis show the true year, as in 1986-2011?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要以适当的年份格式获取输出,您必须先列举数据。
因此:
data = list(枚举(rz1,start = 1960))
有一些方法可以绘制此新数据,一个是将数据转换为NP数组和转移,另一个是通过使用zip函数。他们都有相同的输出。
data = list(zip(*b))
或
data = np.array(data).transpose()
最终代码(在创建图中)为:
s/n:呼叫
plt.show()
,而不是plt.showTo get the output in proper year format, you must enumerate the data first.
So:
data = list(enumerate(rz1, start=1960))
There are to ways to go about plotting this new data, one is by converting the data into a np Array and transposing, the other is by using the zip function. They both have the same output.
data = list(zip(*b))
or
data = np.array(data).transpose()
The final code(in the creating the graph section) is:
s/n: call
plt.show()
, not plt.show我不知道枢纽的事情,但是以下效果很好:
I don't know about the pivoting thing, but the following works fine: