Matplotlib pyplot.colors()
Last Updated :
23 Dec, 2024
In Python, we can plot graphs for visualization using the Matplotlib library. For integrating plots into applications, Matplotlib provides an API. Matplotlib has a module named pyplot which provides a MATLAB-like interface.
Matplotlib Add Color
This function is used to specify the color. It is a do-nothing function.
Alias | Color |
---|
'b' | Blue |
'r' | Red |
'g' | Green |
'c' | Cyan |
'm' | Magenta |
'y' | Yellow |
'k' | Black |
'w' | White |
We can use this function for various data visualizations and obtain insights from them.
Matplotlib.pyplot.colors()
Below are some examples by which we can add color in Matplotlib.
Plotting a Simple Line with Matplotlib Colors
In this example, a line plot is created using Matplotlib, with data points [1, 2, 3, 4]
. The line color is specified as 'green'.
Python
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4], color="green")
plt.show()
Output:

Plotting Data Points with Red Circular Markers
In this example, a line plot is generated using Matplotlib with data points at coordinates [1, 1], [2, 4], [3, 9], [4, 16]
. The points are marked with red circular markers (marker='o'
). Here, marker=’o’ represents circle while markerfacecolor is used for specifying color of the point marker.
Python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 16]
plt.plot(x, y, marker='o', markerfacecolor='r')
plt.show()
Output:

Plotting a Line with a Hexadecimal Color
In this example, a line plot is created in Matplotlib, extending from (0, 0)
to (1, 1)
. The line color is defined using the hexadecimal code '#FF5733'
, representing a shade of orange-red.
Python
import matplotlib.pyplot as plt
plt.plot([3, 4], [6, 4], color='#b717bd')
plt.show()
Output:
plotting a line with hexadecimal color
Plotting a Line with an RGBA Color
In this example, a line plot is rendered using Matplotlib, spanning from x=[0,1,2,3] and y=[0,1,4,9]
. The line color is set using RGBA values (1, 0.5, 0.5, 0.5)
, producing a semi-transparent light pink for the line.
Python
import matplotlib.pyplot as plt
x = [0, 1, 2, 3]
y = [0, 1, 4, 9]
plt.plot(x, y, color=(1, 0.5, 0.5, 0.5)) # RGBA tuple (Red, Green, Blue, Alpha)
plt.show()
Output:
Plotting the line with RGBA color
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Machine Learning Tutorial Machine learning is a branch of Artificial Intelligence that focuses on developing models and algorithms that let computers learn from data without being explicitly programmed for every task. In simple words, ML teaches the systems to think and understand like humans by learning from the data.It can
5 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Linear Regression in Machine learning Linear regression is a type of supervised machine-learning algorithm that learns from the labelled datasets and maps the data points with most optimized linear functions which can be used for prediction on new datasets. It assumes that there is a linear relationship between the input and output, mea
15+ min read
Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or
9 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read