- 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 create a Matplotlib bar chart with a threshold line?
To create a Matplotlib bar chart with a threshold line, we have to use axhline() method.
Steps
- Set the figure size and adjust the padding between and around the subplots.
- Initialize a variable, threshold.
- Make lists for bars values.
- Get the below and above bar values based on the threshold value.
- Create a figure and a set of subplots using subplots() method.
- Plot bars with x, a_threshold and b_threshold values.
- Add a horizontal line across the axis using axhline() method.
- To display the figure, use show() method.
Example
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True threshold = 10 values = np.array([8.0, 10.0, 15.0, 9.0, 12.0]) x = range(len(values)) a_threshold = np.maximum(values - threshold, 0) b_threshold = np.minimum(values, threshold) fig, ax = plt.subplots() ax.bar(x, b_threshold, 0.35, color="blue") ax.bar(x, a_threshold, 0.35, color="yellow", bottom=b_threshold) plt.axhline(threshold, color='red', ls='dotted') plt.show()
Output
- Related Articles
- How to create a line chart using Matplotlib?
- How to Create a Diverging Stacked Bar Chart in Matplotlib?
- How to increase the thickness of error line in a Matplotlib bar chart?
- How to plot a Bar Chart with multiple labels in Matplotlib?
- How to create a stacked bar chart for my DataFrame using Seaborn in Matplotlib?
- How to create a bar chart using JavaFX?
- How to create a 100% stacked Area Chart with Matplotlib?
- How to display percentage above a bar chart in Matplotlib?
- How to create a line chart using ggplot2 with a vertical line in R?
- How to align the bar and line in Matplotlib two Y-axes chart?
- How to create a stacked bar chart using JavaFX?
- How to plot a bar chart for a list in Python matplotlib?
- How to create a line chart using JavaFX?
- How to create bar chart using ggplot2 with chart sub-title in R?
- How to create a bar chart using plotly in R?

Advertisements