- 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 change the color of a single bar if a condition is true (Matplotlib)?
To change the color of a single bar if a condition is true, we can make a set of values and a list of colors with red until the value is 2; else add yellow color in the list.
Steps
- Set the figure size and adjust the padding between and around the subplots.
- Initialize a variable width of a bar.
- Make two lists of values and colors.
- Use bar() method to plot bars.
- 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 data = np.arange(5) width = 0.5 vals = [1, 2, 1, 5, 3] colors = ["red" if i != 2 else "yellow" for i in vals] plt.bar(data, vals, width, color=colors) plt.show()
Output
- Related Articles
- How to change the color of a plot frame in Matplotlib?
- How to change the face color of a plot using Matplotlib?
- How to change the color of a particular bar using geom_bar in R?
- How to change the color of a line using radiobuttons in Matplotlib?
- How to change the color of bars of a bar plot using ggplot2 in R?
- How to plot a single line in Matplotlib that continuously changes color?
- How to change progress bar progress color in Android?
- How to change axes background color in Matplotlib?
- How to change status bar color to match app Android?
- Changing the color of a single X-axis tick label in Matplotlib
- Is it true that the color of our eyes indicates our health condition?
- How to change the color of the axis, ticks and labels for a plot in matplotlib?
- Setting Different Bar color in Matplotlib
- How to change the color of the ticks in the colorbar in Matplotlib?
- How to change the color and add grid lines to a Python Matplotlib surface plot?

Advertisements