Python 如何并排绘制图形以进行比较?
我做了两个条形图,就像Python 如何并排绘制图形以进行比较?,python,pandas,Python,Pandas,我做了两个条形图,就像 dirmedtop.plot.barh() 现在我试着去做 fig, (ax1, ax2) = plt.subplots(1, 2) fig.suptitle('Horizontally stacked subplots') ax1 = dirmedtop.plot.barh ax2 = dirmeantop.plot.barh 但结果显示类型错误“Axessubplot”不可调用,这是错误的 我希望这些条形图并排排列,以便进行比较。谁能帮我做这个 最终,我希望
dirmedtop.plot.barh()
现在我试着去做
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1 = dirmedtop.plot.barh
ax2 = dirmeantop.plot.barh
但结果显示类型错误“Axessubplot”不可调用,这是错误的
我希望这些条形图并排排列,以便进行比较。谁能帮我做这个
最终,我希望图表看起来像这样
我所说的dirmedtop和dirmeantop就是这个。dirmedtop是前10名每位董事毛额中位数最高的董事。dirmeantop是imdb平均得分最高的前10名董事
dirmean= df.loc[df['director_name'].isin(director2.index)].groupby('director_name')['imdb_score'].mean()
dirmean
dirmeansort= dirmean.sort_values(ascending=False)
dirmeansort
dirmeantop=dirmeansort.head(10)
dirmeantop
director_name
Christopher Nolan 8.425000
Quentin Tarantino 8.200000
Stanley Kubrick 8.000000
James Cameron 7.914286
David Fincher 7.750000
Peter Jackson 7.675000
Martin Scorsese 7.660000
Wes Anderson 7.628571
Paul Greengrass 7.585714
Sam Mendes 7.500000
Name: imdb_score, dtype: float64
dirmed= df.loc[df['director_name'].isin(director2.index)].groupby('director_name')['gross'].median()
dirmed
dirmedsort= dirmed.sort_values(ascending=False)
dirmedsort
dirmedtop= dirmedsort.head(10)
dirmedtop
director_name
Jon Favreau 312057433.0
Peter Jackson 236579815.0
Christopher Nolan 196667606.5
Bryan Singer 156142402.0
James Cameron 146282411.0
Sam Raimi 138480208.0
Michael Bay 138396624.0
Steven Spielberg 132014112.0
Tom Shadyac 128769345.0
Jay Roach 126561111.0
Name: gross, dtype: float64
将参数
ax
添加到,并对两者进行排序,因为使用了添加间距:
dirmean= df.loc[df['director_name'].isin(director2.index)].groupby('director_name')['imdb_score'].mean()
dirmean
dirmeansort= dirmean.sort_values(ascending=False)
dirmeansort
dirmeantop=dirmeansort.head(10)
dirmeantop
director_name
Christopher Nolan 8.425000
Quentin Tarantino 8.200000
Stanley Kubrick 8.000000
James Cameron 7.914286
David Fincher 7.750000
Peter Jackson 7.675000
Martin Scorsese 7.660000
Wes Anderson 7.628571
Paul Greengrass 7.585714
Sam Mendes 7.500000
Name: imdb_score, dtype: float64
dirmed= df.loc[df['director_name'].isin(director2.index)].groupby('director_name')['gross'].median()
dirmed
dirmedsort= dirmed.sort_values(ascending=False)
dirmedsort
dirmedtop= dirmedsort.head(10)
dirmedtop
director_name
Jon Favreau 312057433.0
Peter Jackson 236579815.0
Christopher Nolan 196667606.5
Bryan Singer 156142402.0
James Cameron 146282411.0
Sam Raimi 138480208.0
Michael Bay 138396624.0
Steven Spielberg 132014112.0
Tom Shadyac 128769345.0
Jay Roach 126561111.0
Name: gross, dtype: float64
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10,5))
plt.subplots_adjust(wspace = 0.7)
fig.suptitle('Top 10 movie directors')
dirmeantop.rename_axis(None).sort_values().plot.barh(ax=ax1, title='By IMDB rank')
dirmedtop.rename_axis(None).sort_values().plot.barh(ax=ax2, title='By Gross')
ax1.set_ylabel('Director')
ax1.set_xlabel('IMDB Score')
ax2.set_xlabel('Gross')