- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 change the text color of font in the legend using Matplotlib?
To change the text color of font in the legend in matplotlib, we can take the following steps−
- Create x and y data points using numpy.
- Plot x and y using plot() method, where color of line is red and label is "y=exp(x)".
- To place the legend, use legend() method with location of the legend and store the returned value to set the color of the text.
- To set the color of the text, use set_color() method with green color.
- To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-2, 2, 100) y = np.exp(x) plt.plot(x, y, label="y=exp(x)", c='red') leg = plt.legend(loc='upper left') for text in leg.get_texts(): text.set_color("green") plt.show()
Output
- Related Articles
- How to change the font color of a text using JavaScript?
- How do you change the default font color for all text in Matplotlib?
- How to increase the font size of the legend in my Seaborn plot using Matplotlib?
- How to set the font size of Matplotlib axis Legend?
- How to change the Foreground color or Font color of the console using PowerShell?
- How to change the font family of Text using FabricJS?
- How to change the font size of Text using FabricJS?
- How to change the font style of Text using FabricJS?
- How to change the font weight of Text using FabricJS?
- How to change the legend fontname in Matplotlib?
- How to change the font size of legend in base R plot?
- How to change the font size of a text using JavaScript?
- How to change the font-weight of a text using JavaScript?
- How to change the color and font of Android ListView using Kotlin?
- Change the color of legend element border using ggplot2 in R.

Advertisements