- 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 can I get the color of the last figure in Matplotlib?
To get the color of the last figure, we can use get_color() method for every plot.
- Set the figure size and adjust the padding between and around the subplots.
- Create x and y data point using numpy.
- Plot (x, x), (x, x2) and (x, x3) using plot() method.
- Place a legend for every plot line.
- Get the color of each plot using get_color() method.
- 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 x = np.arange(10) y = np.arange(10) p = plt.plot(x, y, x, y ** 2, x, y ** 3) plt.legend([p[0], p[1], p[2]], ["$y=x$", "$y=x^2$", "$y=x^3$"]) print("Color of the first plot: ", p[0].get_color()) print("Color of the second plot: ", p[1].get_color()) print("Color of the third plot: ", p[2].get_color()) plt.show()
Output
Along with the plots, you will get the following output printed on the console −
Color of the first plot: #1f77b4 Color of the second plot: #ff7f0e Color of the third plot: #2ca02c
- Related Articles
- How can I set the background color on specific areas of a Pyplot figure using Matplotlib?
- How can I convert numbers to a color scale in Matplotlib?
- How can I dynamically update my Matplotlib figure as the data file changes?
- How can I get last 4 characters of a string in Python?
- How can I get the output of a Matplotlib plot as an SVG?
- How can I get the length of a single unit on an axis in Matplotlib?
- How I can get a Cartesian coordinate system in Matplotlib?
- How do I extend the margin at the bottom of a figure in Matplotlib?
- How can I plot NaN values as a special color with imshow in Matplotlib?
- How can I attach a pyplot function to a figure instance? (Matplotlib)
- How do I set color to Rectangle in Matplotlib?
- How can one modify the outline color of a node in networkx using Matplotlib?
- How can I set the location of minor ticks in Matplotlib?
- How can I hide the axes in Matplotlib 3D?
- How can I change the text color with jQuery?

Advertisements