- 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
Styling a part of label in legend in Matplotlib
To style a part of label in legend, we can take the following steps −
Create data point for x using numpy.
Plot a sine curve using np.sin(x) with a text label.
Plot a cosine curve using np.cos(x) with a text label.
To place the legend on the plot, use legend() method.
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(-1, 1, 10) plt.plot(x, np.sin(x), label="This is $\it{a\ sine\ curve}$") plt.plot(x, np.cos(x), label="This is $\bf{a\ cosine\ curve}$") plt.legend(loc='lower right') plt.show()
Output
- Related Articles
- How do you just show the text label in a plot legend in Matplotlib?
- How to make two markers share the same label in the legend using Matplotlib?
- Text alignment in a Matplotlib legend
- Automated legend creation in Matplotlib
- How to create a draggable legend in Matplotlib?
- Legend with vertical line in matplotlib
- How to label a patch in matplotlib?
- Get the legend as a separate picture in Matplotlib
- Add a legend in a 3D scatterplot with scatter() in Matplotlib
- How to label a line in Matplotlib (Python)?
- Specifying the line width of the legend frame in Matplotlib
- How to create a legend for a 3D bar in Matplotlib?
- How to add legend to imshow() in Matplotlib?
- Show only certain items in legend Python Matplotlib
- How to show legend elements horizontally in Matplotlib?

Advertisements