- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 sort bars in increasing order in a bar chart in matplotlib?
To sort bars in increasing order in a bar chart in matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Make a data frame, df, of two-dimensional, size-mutable, potentially heterogeneous tabular data.
- Add a subplot to the current figure.
- Make a bar plot with the dataframe, df.
- Add a subplot to the current figure.
- Make a df_sorted by a column marks.
- Make a bar plot with df_sorted.
- To display the figure, use show() method.
Example
import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame( dict( names=['John', 'James', 'David', 'Gary', 'Watson'], marks=[23, 34, 30, 19, 20] ) ) plt.subplot(121) plt.bar('names', 'marks', data=df, color='red') plt.subplot(122) df_sorted = df.sort_values('marks') plt.bar('names', 'marks', data=df_sorted, color='orange') plt.show()
Output
It will produce the following output
Notice the bar chart on the right. The bars are sorted in the increasing order of their values.
- Related Articles
- How to determine the order of bars in a matplotlib bar chart?
- How to sort bars in a bar plot in ascending order (Matplotlib)?
- How to remove gaps between bars in Matplotlib bar chart?
- How to change the order of bars in bar chart in R?
- How do I get all the bars in a Matplotlib bar chart?
- How to Adjust a Bar Chart to Make the Bars Wider in Excel?
- How to display percentage above a bar chart in Matplotlib?
- How to Create a Diverging Stacked Bar Chart in Matplotlib?
- How to plot multiple horizontal bars in one chart with matplotlib?
- How to plot a Bar Chart with multiple labels in Matplotlib?
- How to change Bar Chart values to percentages in Matplotlib?
- Horizontal stacked bar chart in Matplotlib
- How to decrease the width of bars for plotly bar chart in R?
- How to plot a bar chart for a list in Python matplotlib?
- How to display stacked bar chart using matplotlib in Python?

Advertisements