- 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
How to remove gaps between bars in Matplotlib bar chart?
To remove gaps between bars, we can change the align value to center in the argument of bar() method.
Steps
Create a dictionary called data with two keys, milk and water.
Get the list of keys and values in the dictionay.
Using subplots() method, create a figure and add a set of two subplots.
On axis 2, use bar method to plot bars without gaps. Set the width attribute as 1.0. Set the title using set_title() method.
Use tight_layout() to adjust the padding between and around the subplots.
To display the figure, use show() method.
Example
import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True data = {'milk': 12, 'water': 10} names = list(data.keys()) values = list(data.values()) fif, (ax1, ax2) = plt.subplots(2) ax1.bar(range(len(data)), values, align='center', width=.05, color='yellow') ax1.set_title("with gap") ax2.bar(range(len(data)), values, align='edge', width=1.0, color='yellow') ax2.set_title("without gap") plt.tight_layout() plt.show()
Output
- Related Articles
- How to determine the order of bars in a matplotlib bar chart?
- How to sort bars in increasing order in a bar chart in matplotlib?
- How do I get all the bars in a Matplotlib bar chart?
- How to change the order of bars in bar chart in R?
- How to plot multiple horizontal bars in one chart with matplotlib?
- How to Adjust a Bar Chart to Make the Bars Wider in Excel?
- How To Annotate Bars in Bar Plot with Matplotlib in Python?
- How to decrease the width of bars for plotly bar chart in R?
- How to change Bar Chart values to percentages in Matplotlib?
- Horizontal stacked bar chart in Matplotlib
- How to display stacked bar chart using matplotlib in Python?
- How to display percentage above a bar chart in Matplotlib?
- How to Create a Diverging Stacked Bar Chart in Matplotlib?
- How to sort bars in a bar plot in ascending order (Matplotlib)?
- How to create plotly bar chart with values on top of bars in R?

Advertisements