You are on page 1of 1

 Tutorials  Exercises  Get Certified  Services  Bootcamps Spaces Sign Up Log in

Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO   
Python Try...Except
ADVERTISEMENT
Python User Input
Python String Formatting

File Handling
Python File Handling
Python Read Files Matplotlib Plotting
Python Write/Create Files
Python Delete Files ❮ Previous Next ❯

Python Modules
NumPy Tutorial
Plotting x and y points
Pandas Tutorial
SciPy Tutorial The plot() function is used to draw points (markers) in a diagram.
Django Tutorial
By default, the plot() function draws a line from point to point.
Python Matplotlib
The function takes parameters for specifying points in the diagram.
Matplotlib Intro
Matplotlib Get Started Parameter 1 is an array containing the points on the x-axis.
Matplotlib Pyplot
Parameter 2 is an array containing the points on the y-axis.
Matplotlib Plotting
Matplotlib Markers If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to the plot function.
Matplotlib Line
Matplotlib Labels
Matplotlib Grid Example Get your own Python Server
Matplotlib Subplot
Draw a line in a diagram from position (1, 3) to position (8, 10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()

Result:

COLOR PICKER



Try it Yourself »

The x-axis is the horizontal axis.

The y-axis is the vertical axis.

ADVERTISEMENT

ADVERTISEMENT

Plotting Without Line


To plot only the markers, you can use shortcut string notation parameter 'o', which means 'rings'.

Example
Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints, 'o')


plt.show()

Result:

Try it Yourself »

You will learn more about markers in the next chapter.

Multiple Points
You can plot as many points as you like, just make sure you have the same number of points in both axis.

Example
Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 2, 6, 8])


ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()

Result:

Try it Yourself »

Default X-Points
If we do not specify the points on the x-axis, they will get the default values 0, 1, 2, 3 (etc., depending on the length of the y-
points.

So, if we take the same example as above, and leave out the x-points, the diagram will look like this:

Example
Plotting without x-points:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10, 5, 7])

plt.plot(ypoints)
plt.show()

Result:

Try it Yourself »

The x-points in the example above are [0, 1, 2, 3, 4, 5].

❮ Previous Log in to track progress Next ❯

ADVERTISEMENT

ADVERTISEMENT

Spaces Upgrade Newsletter Get Certified Report Error

Top Tutorials Top References Top Examples Get Certified


HTML Tutorial HTML Reference HTML Examples HTML Certificate
CSS Tutorial CSS Reference CSS Examples CSS Certificate
JavaScript Tutorial JavaScript Reference JavaScript Examples JavaScript Certificate
How To Tutorial SQL Reference How To Examples Front End Certificate
SQL Tutorial Python Reference SQL Examples SQL Certificate
Python Tutorial W3.CSS Reference Python Examples Python Certificate
W3.CSS Tutorial Bootstrap Reference W3.CSS Examples PHP Certificate
Bootstrap Tutorial PHP Reference Bootstrap Examples jQuery Certificate
PHP Tutorial HTML Colors PHP Examples Java Certificate
Java Tutorial Java Reference Java Examples C++ Certificate
C++ Tutorial Angular Reference XML Examples C# Certificate
jQuery Tutorial jQuery Reference jQuery Examples XML Certificate

FORUM | ABOUT

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.


W3Schools is Powered by W3.CSS.

You might also like