Home > Backend Development > Python Tutorial > How to draw a Matplotlib scatter plot in Python

How to draw a Matplotlib scatter plot in Python

PHPz
Release: 2023-05-10 14:19:20
forward
1696 people have browsed it

Use the plt.plot method:

In the previous article Python data analysis - Matplotlib line chart drawing, we introduced that you can use plt.plot()Method to draw a line chart, this method can also draw a scatter chart, is as follows:

import random
x = range(15)
y = [i + random.randint(-2,2) for i in x]
plt.plot(x, y, "o")
plt.show()
Copy after login

The result output is as follows:

How to draw a Matplotlib scatter plot in Python

Because the plot method draws a line chart by default, plt.plot(x, y) is equivalent to plt.plot(x, y, "-" ), the third parameter is "-", which means using lines to connect the coordinate points. If you use points . or circles o to connect these 10 points, What is presented is a scatter plot.

In addition to -, ., o, there are other types, such as x, ,v,^,<, >Wait, you can explore on your own.

Use plt.scatter method:

Matplotlib also provides another powerful methodplt.scatter(),Use format As follows:

plt.scatter(x, y, s=None, c=None, marker=None, ···)
Copy after login

The main parameters in the function are described as follows:

  • x, y: represents respectively Data corresponding to the x-axis and y-axis, receiving list type parameters

  • #s: represents the size of the point, the default is 20, it can be a character or a list, which is a list Each element of the list represents the size of the corresponding point

  • c: represents the color of the point, which can be a character or a list, for each element of the list Represents the color of the corresponding point

  • marker: Indicates the type of drawn point, the default small circleo

  • ##······

  • ##alpha:

    Represents the transparency of the point, accepting decimals between 0 and 1

For example:

import random
x = range(15)
y = [i + random.randint(-2,2) for i in x]
plt.scatter(x, y, marker="v")
plt.show()
Copy after login
The result output is as follows:

The above is the detailed content of How to draw a Matplotlib scatter plot in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template