- 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
Plotting multiple line graphs using Pandas and Matplotlib
To plot multiple line graphs using Pandas and Matplotlib, we can take the following steps −
Set the figure size and adjust the padding between and around the subplots.
Make a 2D potentially heterogeneous tabular data using Pandas DataFrame class, where the column are x, y and equation.
Get the reshaped dataframe organized by the given index such as x, equation, and y.
Use the plot() method to plot the lines.
To display the figure, use show() method.
Example
import pandas as pd from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True df = pd.DataFrame([ ["y=x^3", 0, 0], ["y=x^3", 1, 1], ["y=x^3", 2, 8], ["y=x^3", 3, 27], ["y=x^3", 4, 64], ["y=x^2", 0, 0], ["y=x^2", 1, 1], ["y=x^2", 2, 4], ["y=x^2", 3, 9], ["y=x^2", 4, 16], ["y=mx", 0, 0], ["y=mx", 1, 1], ["y=mx", 2, 2], ["y=mx", 3, 3], ["y=mx", 4, 3], ], columns=['equation', 'x', 'y']) df = df.pivot(index='x', columns='equation', values='y') df.plot() plt.show()
Output
- Related Articles
- Plotting multiple dataframes using Pandas functionality
- Plotting Pandas DataFrames in Pie Charts using Matplotlib
- How to plot multiple graphs in Matplotlib?
- Plotting histograms against classes in Pandas / Matplotlib
- Plotting a horizontal line on multiple subplots in Python using pyplot
- Interactive plotting with Python Matplotlib via command line
- Displaying bar graphs using Matplotlib
- Displaying horizontal bar graphs using Matplotlib
- Adding textures to graphs using Matplotlib
- How to make multipartite graphs using networkx and Matplotlib?
- Plotting animated quivers in Python using Matplotlib
- Plotting a masked surface plot using Python, Numpy and Matplotlib
- How to plot multiple Pandas columns on the Y-axis of a line graph (Matplotlib)?
- Plot multiple time-series DataFrames into a single plot using Pandas (Matplotlib)
- How to plot 3D graphs using Python Matplotlib?

Advertisements