Matplotlib散点图:如何在Python中创建散点图

767 阅读4分钟

Matplotlib scatter - The Complete Guide

要在Python创建 散点图,可以使用Matplotlib.pyplot.scatter() 函数。scatter()方法为每个观察值绘制一个点。它需要两个相同长度的数组,一个用于X轴的值,另一个用于Y轴的值。

Matplotlib scatter

Matplotlib.pyplot.scatter()是一个内置的库函数,它为给定的点创建一个散点图,并将该图作为输出显示。散点图类似于线形图。散点图和折线图的主要区别在于,这些点不是连续的,不能连接成一条线。当点是分散的,那么我们就可以使用这个散点图。

语法

matplotlib.pyplot.scatter(x, y, s=None, c=None, marker=None, cmap=None, norm=None, vmin=None, 
                          vmax=None, alpha=None, linewidths=None, *, edgecolors=None, 
                          plotnonfinite=False, data=None, **kwargs) 

参数

matplotlib.pyplot.scatter() 函数有两个必要的参数。

x, y:这些是必要参数。这些参数将两个数组作为一个值。这个数组由数据点组成。

s:这个参数是可选的。这个参数可以取浮动值或数组作为值。例如,可以用这个参数改变标记的大小。

c:这是一个可选的参数。由颜色组成的数组作为一个值传递给这个参数。这个参数以一个元素的列表作为值。

marker(标记):这个参数指定散点图中使用的标记的样式。默认情况下,标记样式保持为'o'。然而,可以通过在此参数中传递标记样式来修改标记样式。

cmap:该参数仅在c参数与浮点值一起传递时使用。通过使用这个参数,浮动值被转换为相应的颜色。

norm:该参数仅在c参数与浮点数一起传递时使用。该函数用于对c参数中的数据进行规范化处理。浮点数的范围在0到1之间被归一化。

vmin, vmax:这个参数只有在不使用norm参数时才能使用。这个vmin和vmax与默认的norm一起用于将颜色阵列c映射到颜色映射阵列cmap。

alpha:这是一个可选的参数。这个参数的取值范围是0到1。0用来表示透明,1用来表示不透明。

linewidths(线宽):标记边缘的线宽在这个参数中传递。这个参数接受浮点数或数组作为数值。

edgecolors:标记的边缘颜色在这个参数中传递。

plotnonfinite:这是一个布尔值。如果是 "真",无限的点会被绘制在图中。默认情况下,它被设置为False。

返回值

matplotlib scatter() 函数绘制了一个散点图作为输出。matplotlib.pyplot.scatter()函数创建一个散点图并在输出中显示。

使用matplotlib.pyplot.scatter创建散点图的程序

# Importing matplotlib.pyplot as plt.
import matplotlib.pyplot as plt

# Importing numpy as np
import numpy as np


# x coordinates are created
x = np.array([5,20,10,67,99,45,32,34,42])

# y coordinates are created
y = np.array([90,80,8,20,10,90,5,99,54])

# scatter plot is created
plt.scatter(x,y)

# x axis is labeled as X-Axis
plt.xlabel('X-Axis')

# y axis is labeled as Y-Axis
plt.ylabel('Y-Axis')

# Title is kept for the Scatter plot
plt.title('Scatter Plot Example')

# Displaying the created graph using the show method
plt.show()

输出

Program for creating a scatter plot using matplotlib.pyplot.scatter

在这个程序中,我们导入了matplotlib.pyplot来绘制散点图。matplotlib库包含了所有用于绘制不同类型的图形和图表的函数。

然后,我们导入了一个numpy来创建x坐标和y坐标。然后,我们将这两个坐标传递给scatter函数。最后,scatter()函数通过结合x和y坐标创建一个散点图。

这个散点图是在数据点无序散布时使用的。在这个例子中,数据点是无序的;因此这个例子正确描述了散点图。然后我们用show函数来显示生成的散点图。

使用matplotlib.pyplot.scatter创建一个具有多个标记的散点图的程序

# Importing matplotlib.pyplot as plt.
import matplotlib.pyplot as plt

# Importing numpy as np
import numpy as np

# x coordinates are created
x = np.array([5, 20, 10, 67, 99, 45, 32, 34, 42])

# y coordinates are created
y = np.array([90, 80, 8, 20, 10, 90, 5, 99, 54])

# x1 coordinates are created
x1 = np.array([10, 30, 15, 60, 50, 90, 40, 39, 62])

# y1 coordinates are created
y1 = np.array([90, 80, 8, 20, 10, 90, 5, 99, 54])

# scatter plot is created
plt.scatter(x, y, c="pink",
 linewidths=2,
 marker="^",
 edgecolor="green",
 s=50)
plt.scatter(x1, y1, c="blue",
 linewidths=2,
 marker="*",
 edgecolor="yellow",
 s=150)

# x axis is labeled as X-Axis
plt.xlabel('X-Axis')

# y axis is labeled as Y-Axis
plt.ylabel('Y-Axis')

# Title is kept for the Scatter plot
plt.title('Scatter Plot Example')

# displaying the created graph using the show method
plt.show()

输出

Program for creating a scatter plot having multiple markers using matplotlib.pyplot.scatter

在这个程序中,我们导入了matplotlib.pyplot库来绘制散点图。matplotlib库包含了所有用于绘制不同类型的图形和图表的函数。

然后我们导入了numpy来创建x坐标和y坐标。然后我们创建了另一组名为x1和y1的点。然后我们将x和y坐标传入散点函数,颜色为粉红色,边缘颜色为绿色,标记为一个三角形。

然后,我们将点x1和y1传入散点函数,颜色为蓝色,边缘颜色为黄色,标记为星。这个函数通过结合x和y坐标创建了一个散点图。然后我们用show()函数来显示生成的散点图。在这个例子中,两个坐标被绘制在一个散点图中。

本教程就到此为止。