- 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 generate random colors in Matplotlib?
To make a custom color, we can create a hexadecimal string. From it, we can make different sets of color representation and can pass into the scatter method to get the desired output.
Steps
Take an input from the user for the number of colors, i.e., number_of_colors = 20.
Use Hexadecimal alphabets to get a color.
Create a color from (step 2) by choosing a random character from step 2 data.
Plot scatter points for step 1 input data, with step 3 colors.
To show the figure, use plt.show() method.
Example
import matplotlib.pyplot as plt import random number_of_colors = int(input("Please enter number of colors: ")) hexadecimal_alphabets = '0123456789ABCDEF' color = ["#" + ''.join([random.choice(hexadecimal_alphabets) for j in range(6)]) for i in range(number_of_colors)] for i in range(number_of_colors): plt.scatter(random.randint(0, 10), random.randint(0, 10), c=color[i], s=200) plt.show()
Output
Please enter the number of colors: 20
- Related Articles
- How can I generate more colors on a pie chart in Matplotlib?
- Map values to colors in Matplotlib
- How to generate a random number in C++?
- How to generate large random numbers in Java?
- How to generate a random number in JavaScript?
- How to generate Bernoulli random variable in R?
- How to generate non-repeating random numbers in Python?
- How to generate 6-digit random number in MySQL?
- How to generate a random BigInteger value in Java?
- How to generate standard normal random numbers in R?
- How to Generate Random Birthday Wishes using JavaScript?
- How to assign specific colors to specific cells in a Matplotlib table?
- How does Python generate random numbers?
- How to give a Pandas/Matplotlib bar graph custom colors?
- How to use Python Numpy to generate Random Numbers?

Advertisements