- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 −
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.
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()
It will produce the following output −
- 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
- 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 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?
- Plotting two different arrays of different lengths in matplotlib
- Python matplotlib multiple bars
- How to plot with different scales in Matplotlib?