- 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 annotate the end of lines using Python and Matplotlib?
To annotate the end of lines using Python and Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Initalize a variable, rows, to get the number of rows data.
- Get a Pandas dataframe in a rectangular tabular data.
- Calculate the cumsum (cumulative sum) of the dataframe.
- Plot the dataframe using plot() method.
- Iterate line and name to annotate the end of lines.
- Use annotate() method with column's name, xy co-ordinates, color of the lines, sizes, etc.
- Place a legend on the figure.
- To display the figure, use show() method.
Example
import pandas as pd import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True rows = 75 df = pd.DataFrame(np.random.randint(-5, 5, size=(rows, 3)), columns=['A', 'B', 'C']) df = df.cumsum() ax = df.plot() for line, name in zip(ax.lines, df.columns): y = line.get_ydata()[-1] ax.annotate(name, xy=(1, y), xytext=(6, 0), color=line.get_color(), xycoords=ax.get_yaxis_transform(), textcoords="offset points", size=14, va="center") plt.legend(loc='lower left') plt.show()
Output
- Related Articles
- How to annotate a range of the X-axis in Matplotlib?
- How to annotate a heatmap with text in Matplotlib?
- How to plot two dotted lines and set marker using Matplotlib?
- How to annotate several points with one text in Matplotlib?
- Annotate Time Series plot in Matplotlib
- Annotate Subplots in a Figure with A, B, C using Matplotlib
- How to change the color and add grid lines to a Python Matplotlib surface plot?
- How to hide lines in Matplotlib?
- How to plot the lines first and points last in Matplotlib?
- How to remove grid lines from an image in Python Matplotlib?
- How to annotate the points on a scatter plot with automatically placed arrows in Matplotlib?
- How to draw more type of lines in Matplotlib?
- How to name different lines in the same plot of Matplotlib?
- How to plot overlapping lines in Matplotlib?
- How to annotate seaborn pairplots?

Advertisements