Skip to main content
TopMiniSite

Back to all posts

How to Plot A Graph With Matplotlib?

Published on
4 min read
How to Plot A Graph With Matplotlib? image

Best Graph Plotting Tools to Buy in April 2026

1 Mariners Chart Plotting Tool Kit - Marine Navigation Equipment, Weems and Plath Parallel Rulers, Dividers & Accessories for Nautical Charts, Sailing and Boating Exam Preparation

Mariners Chart Plotting Tool Kit - Marine Navigation Equipment, Weems and Plath Parallel Rulers, Dividers & Accessories for Nautical Charts, Sailing and Boating Exam Preparation

  • MASTER NAVIGATION WITHOUT GPS USING OUR COMPLETE PLOTTING TOOLS.
  • PERFECT FOR MARITIME STUDENTS PREPARING FOR BOATING CERTIFICATION EXAMS.
  • DURABLE AND PORTABLE DESIGN ENSURES ACCURACY FOR ALL SAILORS.
BUY & SAVE
$100.00
Mariners Chart Plotting Tool Kit - Marine Navigation Equipment, Weems and Plath Parallel Rulers, Dividers & Accessories for Nautical Charts, Sailing and Boating Exam Preparation
2 Mr. Pen- Metal Geometry Kit, 4 Pack, 45° & 30°/60° Set Squares, 6" Protractor & 12" Ruler, Durable Metal Drafting Tools for Technical Drawing, Math, Engineering & Architecture

Mr. Pen- Metal Geometry Kit, 4 Pack, 45° & 30°/60° Set Squares, 6" Protractor & 12" Ruler, Durable Metal Drafting Tools for Technical Drawing, Math, Engineering & Architecture

  • PREMIUM ALUMINUM BUILD: DURABLE, LIGHTWEIGHT, AND LONG-LASTING.
  • VERSATILE SET: IDEAL FOR SCHOOLS, OFFICES, AND DESIGN PROFESSIONALS.
  • PRECISE MEASUREMENTS: ACCURATE INCH AND CENTIMETER MARKINGS FOR EASE.
BUY & SAVE
$7.99
Mr. Pen- Metal Geometry Kit, 4 Pack, 45° & 30°/60° Set Squares, 6" Protractor & 12" Ruler, Durable Metal Drafting Tools for Technical Drawing, Math, Engineering & Architecture
3 Mead Spiral Notebook, 1 Subject, Graph Ruled Paper, 7-1/2" x 10-1/2", 100 Sheets, Fire Red (05676AB5)

Mead Spiral Notebook, 1 Subject, Graph Ruled Paper, 7-1/2" x 10-1/2", 100 Sheets, Fire Red (05676AB5)

  • 100 GRAPH RULED SHEETS FOR VERSATILE PLOTTING AND DRAWING NEEDS.
  • EASY TEAR-OUT SHEETS WITH CLEAN EDGES FOR NEAT PRESENTATIONS.
  • DURABLE COVERS WITH WRITABLE LABELS-PERFECT FOR ORGANIZATION.
BUY & SAVE
$5.29
Mead Spiral Notebook, 1 Subject, Graph Ruled Paper, 7-1/2" x 10-1/2", 100 Sheets, Fire Red (05676AB5)
4 Thboxes 30 Sheets Graph Paper , 11.6" X 8.5" 4x4(4 Squares Per Inch)Easy Tears Off Grid Paper Pad 2 Pack Engineering Technical Drawings Plotting, Graph Paper Notebook for School, Office Graph Notepads

Thboxes 30 Sheets Graph Paper , 11.6" X 8.5" 4x4(4 Squares Per Inch)Easy Tears Off Grid Paper Pad 2 Pack Engineering Technical Drawings Plotting, Graph Paper Notebook for School, Office Graph Notepads

  • AMPLE 11.6 X 8.5 SIZE WITH 30 SHEETS FOR VERSATILE USE.
  • EASY-TEAR DESIGN ALLOWS FOR QUICK, CLEAN SHEET REMOVAL.
  • DURABLE 80G PAPER PREVENTS INK BLEED FOR CLEAR, CRISP NOTES.
BUY & SAVE
$6.49 $7.49
Save 13%
Thboxes 30 Sheets Graph Paper , 11.6" X 8.5" 4x4(4 Squares Per Inch)Easy Tears Off Grid Paper Pad 2 Pack Engineering Technical Drawings Plotting, Graph Paper Notebook for School, Office Graph Notepads
5 Thboxes Graph Paper 17" X 11.6", 2 Pack 4x4(4 Squares Per Inch) Easy Tears Off Grid Paper Pad Engineering Technical Drawings Plotting, Graph Paper Notebook for School, Office Graph 30 Sheets Notepads

Thboxes Graph Paper 17" X 11.6", 2 Pack 4x4(4 Squares Per Inch) Easy Tears Off Grid Paper Pad Engineering Technical Drawings Plotting, Graph Paper Notebook for School, Office Graph 30 Sheets Notepads

  • AMPLE 17 X 11.6 SIZE WITH 30 SHEETS FOR VERSATILE USE.
  • EASY-TEAR DESIGN FOR QUICK AND CLEAN SHEET REMOVAL.
  • DURABLE 80G PAPER PERFECT FOR FOUNTAIN PENS, NO INK BLEED!
BUY & SAVE
$11.99 $13.99
Save 14%
Thboxes Graph Paper 17" X 11.6", 2 Pack 4x4(4 Squares Per Inch) Easy Tears Off Grid Paper Pad Engineering Technical Drawings Plotting, Graph Paper Notebook for School, Office Graph 30 Sheets Notepads
6 Casio fx-9750GIII Graphing Calculator | Natural Textbook Display | Python Programming | Ideal for Exams, STEM & Advanced Math | Black

Casio fx-9750GIII Graphing Calculator | Natural Textbook Display | Python Programming | Ideal for Exams, STEM & Advanced Math | Black

  • NATURAL TEXTBOOK DISPLAY SIMPLIFIES COMPLEX MATH INTERPRETATION.
  • IDEAL FOR ALL MATH LEVELS; SUPPORTS GRAPHING AND ADVANCED FUNCTIONS.
  • EXAM-APPROVED AND USB CONNECTIVITY FOR SEAMLESS FILE MANAGEMENT.
BUY & SAVE
$53.98 $79.99
Save 33%
Casio fx-9750GIII Graphing Calculator | Natural Textbook Display | Python Programming | Ideal for Exams, STEM & Advanced Math | Black
7 Parallel 12'' Economy

Parallel 12'' Economy

  • PRECISE 12-INCH LENGTH FOR ACCURATE MEASUREMENTS EVERY TIME.
  • DURABLE BRUSHED ALUMINUM ARMS ENSURE LONG-LASTING PERFORMANCE.
  • LIMITED LIFETIME WARRANTY GUARANTEES QUALITY AND PEACE OF MIND.
BUY & SAVE
$40.00
Parallel 12'' Economy
+
ONE MORE?

To plot a graph with matplotlib, you first need to import the matplotlib library using the following command:

import matplotlib.pyplot as plt

Then you can create a figure and axes using the subplots function:

fig, ax = plt.subplots()

Next, you can plot your data on the axes using the plot function. For example, to plot a line graph, you can use:

ax.plot(x_data, y_data)

You can customize the graph by adding labels to the axes, a title, and a legend by using functions such as xlabel, ylabel, title, and legend.

Finally, you can show the graph using the show function:

plt.show()

This will display the plotted graph in a window or within a Jupyter notebook if you are using one.

What is a legend in matplotlib?

In matplotlib, a legend is a box or area on a plot that provides an explanation of the symbols, colors, and line styles used to represent different data sets or categories in the plot. Legends are helpful for making plots easier to understand and interpret, especially when there are multiple elements on the plot. Legends can be placed in different locations on the plot, and can be customized in terms of font size, style, and position.

How to display multiple plots in a single figure with matplotlib?

To display multiple plots in a single figure with matplotlib, you can use the subplot() function. The subplot() function takes three arguments - the number of rows, the number of columns, and the index of the subplot you want to create.

Here is an example code snippet to display multiple plots in a single figure:

import matplotlib.pyplot as plt

Create some data for the plots

x = [1, 2, 3, 4, 5] y1 = [1, 4, 9, 16, 25] y2 = [1, 2, 3, 4, 5]

Create a figure and set the size

plt.figure(figsize=(10, 5))

Create the first subplot

plt.subplot(1, 2, 1) plt.plot(x, y1) plt.title('Plot 1')

Create the second subplot

plt.subplot(1, 2, 2) plt.plot(x, y2) plt.title('Plot 2')

Display the plots

plt.show()

This code will create a single figure with two subplots, each displaying a different plot. You can adjust the number of rows and columns in the subplot() function to create more subplots in a single figure.

How to save a matplotlib plot as an image file?

To save a matplotlib plot as an image file, you can use the savefig() function provided by matplotlib. Here is an example code snippet showing how to save a plot as a PNG image file:

import matplotlib.pyplot as plt

Create a sample plot

plt.plot([1, 2, 3, 4], [1, 4, 9, 16])

Save the plot as a PNG image file

plt.savefig('plot.png')

Display the plot

plt.show()

In this example, the savefig() function is used to save the plot as a PNG image file named plot.png. You can specify the file format by providing the file extension in the filename. You can also specify the dpi (dots per inch) of the image by passing the dpi parameter to the savefig() function.

How to customize the tick marks on an axis in a matplotlib plot?

You can customize the tick marks on an axis in a matplotlib plot by using the xticks() and yticks() functions. These functions allow you to specify the positions and labels of the tick marks on the x-axis and y-axis respectively.

Here is an example of how to customize the tick marks on the x-axis of a matplotlib plot:

import matplotlib.pyplot as plt import numpy as np

Generate some data

x = np.linspace(0, 10, 100) y = np.sin(x)

Create the plot

plt.plot(x, y)

Customize the tick marks on the x-axis

plt.xticks([0, 2, 4, 6, 8, 10], ['A', 'B', 'C', 'D', 'E', 'F'])

Show the plot

plt.show()

In this example, we use the xticks() function to specify the positions of the tick marks on the x-axis at 0, 2, 4, 6, 8, and 10, and we set the labels for these tick marks as 'A', 'B', 'C', 'D', 'E', and 'F' respectively.

You can also customize the appearance of the tick marks by using additional arguments in the xticks() and yticks() functions, such as setting the font size, font color, rotation angle, and alignment of the tick labels.

What is the purpose of the plt.xlabel() and plt.ylabel() functions in matplotlib?

The purpose of the plt.xlabel() and plt.ylabel() functions in matplotlib is to set the labels for the x-axis and y-axis of a plot, respectively. These functions allow the user to provide names or descriptions for the axes of a plot, making it easier to interpret the data being visualized.