如何绘制这张图?

时间:2017-06-24 04:53:39

标签: pandas matplotlib

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

total_income.plot.bar(label="Total Income", color = 'r')
average_income.plot.bar(label="Average Income")

plt.xlabel("Genres")
plt.ylabel("Dollars (Gross)")
plt.yscale("log")

这是我的代码,用电影类型绘制总和和平均值。问题是当我绘制图形时,它给出了一个完整的黑色图形。我认为这是因为它包含多种类型的词汇长度。

我如何解决这个问题,以便显示图表及其类型?我需要帮助。

1 个答案:

答案 0 :(得分:0)

您可以list使用length,然后DataFrame获取constructor

上次使用str.splitlen df = pd.DataFrame({'genres':['Comedy|Crime|Drama|Thriller','Comedy|Crime|Drama','Comedy|Crime','Drama|Thriller','Drama','Comedy|Crime'], 'gross':[10,20,30,40,50,60]}) print (df) genres gross 0 Comedy|Crime|Drama|Thriller 10 1 Comedy|Crime|Drama 20 2 Comedy|Crime 30 3 Drama|Thriller 40 4 Drama 50 5 Comedy|Crime 60 splitted = df['genres'].str.split('|') l = splitted.str.len() df = pd.DataFrame({'gross': np.repeat(df['gross'], l), 'genres':np.concatenate(splitted)}) print (df) genres gross 0 Comedy 10 0 Crime 10 0 Drama 10 0 Thriller 10 1 Comedy 20 1 Crime 20 1 Drama 20 2 Comedy 30 2 Crime 30 3 Drama 40 3 Thriller 40 4 Drama 50 5 Comedy 60 5 Crime 60 d = {'mean':'Average','sum':'Total'} df1 = df.groupby('genres')['gross'].agg(['sum','mean']).rename(columns=d) print (df1) Total Average genres Comedy 120 30 Crime 120 30 Drama 120 30 Thriller 50 25 df1.plot.bar() 创建新的 private final function listeners(action:String):void { if(action == 'add') { stage.addEventListener(TouchEvent.TOUCH_BEGIN, startDraw); stage.addEventListener(TouchEvent.TOUCH_END, spinWheel); } else { stage.removeEventListener(TouchEvent.TOUCH_END, startDraw); stage.removeEventListener(TouchEvent.TOUCH_BEGIN, spinWheel); } }

Popen.communicate()
import subprocess

open_blue = subprocess.Popen(["bluetoothctl"], shell=True, stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
out, err = open_blue.communicate("help")
print(out)