- 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 specify different colors for different bars in a Python matplotlib histogram?
To specify different colors for different bars in a matplotlib histogram, we can take the following steps −
Steps
Set the figure size and adjust the padding between and around the subplots.
Create a figure and a set of subplots.
Plot a histogram with random data with 100 sample data.
Iterate in the range of number of bins and set random facecolor for each bar.
To display the figure, use show() method.
Example
import numpy as np import matplotlib.pyplot as plt import random import string # Set the figure size plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Figure and set of subplots fig, ax = plt.subplots() # Random data data = np.random.rand(100) # Plot a histogram with random data N, bins, patches = ax.hist(data, edgecolor='black', linewidth=1) # Random facecolor for each bar for i in range(len(N)): patches[i].set_facecolor("#" + ''.join(random.choices("ABCDEF" + string.digits, k=6))) # Display the plot plt.show()
Output
It will produce the following output −
- Related Articles
- How to fill histogram bars using ggplot2 in R with different colors?
- Plot different colors for different categorical levels using matplotlib
- How to show an image in Matplotlib in different colors with different channels?
- How to create histogram like plot with different color of bars in R?
- Setting different error bar colors in barplot in Matplotlib
- Why different flowers bear different colors?
- How to use different markers for different points in a Pylab scatter plot(Matplotlib)?
- How to show different colors for points and line in a Seaborn regplot?
- Plot a histogram with colors taken from colormap in Matplotlib
- How to create density plot for categories filled with different colors in R?
- How to plot histograms of different colors of an image in OpenCV Python?
- How to display legend in base R with different colors?
- Python - Plot a Histogram for Pandas Dataframe with Matplotlib?
- How do I specify different layouts for portrait and landscape orientations in Android?
- Program to find minimum cost to paint fences with k different colors in Python

Advertisements