

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- 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?
- Is it true that the color of our eyes indicates our health condition?
- How to change the color of bars of a bar plot using ggplot2 in R?
- Changing the color of a single X-axis tick label in Matplotlib
- Setting Different Bar color in Matplotlib
- How to change the color of the axis, ticks and labels for a plot in matplotlib?
- How to change status bar color to match app Android?
- How can I change the text color of the Navigation Bar on iOS?
- How to change the color of the ticks in the colorbar in Matplotlib?
- How to change axes background color in Matplotlib?
- How to change the color of a Tkinter label programmatically?
- How to fully change the color of a Tkinter Listbox?
Advertisements