- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Plot two horizontal bar charts sharing the same Y-axis in Python Matplotlib
To plot two horizontal bar charts sharing the same Y-axis, we can use sharey=ax1 in subplot() method and for horizontal bar, we can use barh() method.
Steps
- Create lists for data points.
- Create a new figure or activate an existing figure using figure() method
- Add a subplot to the current figure using subplot() method, at index=1.
- Plot horizontal bar on axis 1 using barh() method.
- Add a subplot to the current figure using subplot() method, at index=2. Share the Yaxis of axis 1.
- Plot the horizontal bar on axis 2.
- To display the figure, use show() method.
Example
import matplotlib.pyplot as plt import numpy as np plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True y = [3, 1, 5] x1 = [10, 7, 3] x2 = [9, 5, 1] fig = plt.figure() axe1 = plt.subplot(121) axe1.barh(y, x1, align='center', color='red', edgecolor='black') axe2 = plt.subplot(122, sharey=axe1) axe2.barh(y, x2, align='center', color='green', edgecolor='black') plt.show()
Output
- Related Articles
- How to plot two Pandas time series on the same plot with legends and secondary Y-axis in Matplotlib?
- How to make a broken horizontal bar plot in Matplotlib?
- Show the origin axis (x,y) in Matplotlib plot
- How to plot multiple lines on the same Y-axis in Python Plotly?
- How to create two line charts in the same plot in R?
- Python Pandas - Plot a Stacked Horizontal Bar Chart
- Plot a histogram with Y-axis as percentage in Matplotlib
- Horizontal stacked bar chart in Matplotlib
- Show tick labels when sharing an axis in Matplotlib
- Automatically setting Y-axis limits for a bar graph using Matplotlib
- Displaying horizontal bar graphs using Matplotlib
- How to change the Y-axis values in a bar plot using ggplot2 in R?
- Python Pandas - Plot a Grouped Horizontal Bar Chart will all the columns
- How to add group labels for bar charts in Matplotlib?
- How to show a bar and line graph on the same plot in Matplotlib?

Advertisements