python - how to plot two barh in one axis? -
these pandas series more 500 items,i pick top 10 , bottom 10 plot in 1 matplotlib axis,here picture draw manually:
data here:
bottom10 out[12]: 0 -9.823127e+08 1 -8.069270e+08 2 -6.030317e+08 3 -5.709379e+08 4 -5.224355e+08 5 -4.755464e+08 6 -4.095561e+08 7 -3.989287e+08 8 -3.885740e+08 9 -3.691114e+08 name: amount, dtype: float64 top10 out[13]: 0 9.360520e+08 1 9.078776e+08 2 6.603838e+08 3 4.967611e+08 4 4.409362e+08 5 3.914972e+08 6 3.547471e+08 7 3.538894e+08 8 3.368558e+08 9 3.189895e+08 name: amount, dtype: float64 top10.barh(top10.index,top10.amount,color='red',align='edge') bottom10.barh(bottom10.index,bottom10.amount,color='green',align='edge')
now shows this, not want: .
what right way plot?
you can creating twiny
axes, , plotting bottom10
dataframe on there.
for example:
import matplotlib.pyplot plt import pandas pd import numpy np # random data bottom10 = pd.dataframe({'amount':-np.sort(np.random.rand(10))}) top10 = pd.dataframe({'amount':np.sort(np.random.rand(10))[::-1]}) # create figure , axes top10 fig,axt = plt.subplots(1) # plot top10 on axt top10.plot.barh(color='red',edgecolor='k',align='edge',ax=axt,legend=false) # create twin axes axb = axt.twiny() # plot bottom10 on axb bottom10.plot.barh(color='green',edgecolor='k',align='edge',ax=axb,legend=false) # set sensible axes limits axt.set_xlim(0,1.5) axb.set_xlim(-1.5,0) # add axes labels axt.set_ylabel('best items') axb.set_ylabel('worst items') # need manually move axb label right hand side axb.yaxis.set_label_position('right') plt.show()
Comments
Post a Comment