绘制总和和平均值;大熊猫

时间:2017-06-23 16:10:27

标签: pandas

total_income = df.groupby('title_year')['gross'].sum()
average_income = df.groupby('title_year')['gross'].mean()

print(plt.semilogy(total_income,average_income))

所以我想在显示两行的同一图表上绘制总收入和平均收入。我希望我的x轴显示1916-2016年的年份和y轴以美元显示。但我的代码并没有这样做。我需要有关如何更改代码以获得所需内容的帮助

Here's my output of my code

1 个答案:

答案 0 :(得分:0)

这是我的名为data.csv的数据文件:

year,gross
2015,45
2015,47
2015,49
2016,76
2016,78
2016,87
2017,103
2017,115
2017,133

1。)这是获取对数正态图的所有代码:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("data.csv")
total_income = df.groupby('year')['gross'].sum()
average_income = df.groupby('year')['gross'].mean()

total_income.plot(label="Total Income")
average_income.plot(label="Average Income")

plt.xlabel("Year")
plt.ylabel("log$_{10}$(Gross)")
plt.yscale("log")
plt.legend()
plt.tight_layout()
plt.savefig("plot.png")

2。)这是您使用plt.semilogy()的方式:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv("data.csv")
total_income = pd.DataFrame(df.groupby('year')['gross'].sum())
average_income = pd.DataFrame(df.groupby('year')['gross'].mean())

plt.semilogy(total_income.index, total_income["gross"],
             label="Total Income")
plt.semilogy(average_income.index, average_income["gross"],
             label="Average Income")

plt.xlabel("Year")
plt.ylabel("log$_{10}$(Gross)")
plt.legend()
plt.tight_layout()
plt.savefig("plot.png")

1。)和2.)方法产生以下相同的图。

enter image description here