I would like to plot two horizontal bar charts sharing same y axis. For example, the following question shows how to achieve this in R:
Two horizontal bar charts with shared axis in ggplot2 (similar to population pyramid)
How can I create a similar plot with Python?
The plot from the question above looks like this:
Here is the list of states used in the graph above (the y axis):
["AK", "TX", "CA", "MT", "NM", "AZ", "NV", "CO", "OR", "WY",
"MI", "MN", "UT", "ID", "KS", "NE", "SD", "WA", "ND", "OK"]
Here is the list of the numbers of sales staff for each state:
[20,30,40,10,15,35,18,25,22,7,12,22,3,4,5,8,14,28,24,32]
The sales figures can be random.
Best Answer
Generally speaking, if the two variables you're displaying are in different units or have different ranges, you'll want to use two subplots with shared y-axes for this. This is similar to what the answer by @regdoug does, but it's best to explicitly share the y-axis to ensure that your data stays aligned (e.g. try zooming/panning with this example).
For example:
If you want to more precisely reproduce the example shown in the question you linked to (I'm leaving off the gray background and white grids, but those are easy to add, if you prefer them):
One caveat. I haven't actually aligned the y-tick-labels correctly. It is possible to do this, but it's more of a pain than you might expect. Therefore, if you really want y-tick-labels that are always perfectly centered in the middle of the figure, it's easiest to draw them a different way. Instead of
axes[0].set(yticks=y, yticklabels=states)
, you'd do something like: