Reputation: 691
These is a pandas series with more than 500 items,I pick up the top 10 and bottom 10 to plot in one matplotlib axis,here is the picture I draw manually:
data is 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 it shows like this, which is not what I want:
.
What is the right way to plot?
Upvotes: 1
Views: 1258
Reputation: 69223
You can do this by creating a twiny
Axes, and plotting the bottom10
DataFrame on there.
For example:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as 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 and axes for 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 some sensible axes limits
axt.set_xlim(0,1.5)
axb.set_xlim(-1.5,0)
# Add some axes labels
axt.set_ylabel('Best items')
axb.set_ylabel('Worst items')
# Need to manually move axb label to right hand side
axb.yaxis.set_label_position('right')
plt.show()
Upvotes: 5