在Python中使用matplotlib.pyplot.scatter进行数据可视化

501 阅读8分钟

任何一种数据分析的重要方法都是观察关键特征之间的关系,以及看它们是否以某种方式相互依赖。通过某种图谱或数字将这些关系可视化甚至更有用。比方说,我们有一个用例,我们需要看到我们数据中的某种趋势。我们当然需要某种工具来解决这个问题。

Matplotlib 是一个全面的库,可以在Python中创建静态、动画和交互式的可视化效果。它可以帮助我们创建交互式绘图、数字和布局,可以根据我们的需要进行大量定制。

Scatter()方法

散点图是我们将在本文中讨论的内容,特别是matplotlib.pyplot.scatter 方法。它被用来创建散点图,以观察特征或变量之间的关系,这可能有助于我们获得洞察力。

使用这个工具的语法非常简单,只需要几行代码和某些参数。让我们先看一下语法,然后我们将看到如何使用最常用的参数来获得一些漂亮的可视化效果。

散点法的语法

matplotlib.pyplot.scatter(x_axis_array_data, y_axis_array_data, 
                                        s=None, c=None, marker=None, 
                                        cmap=None,  alpha=None, 
                                        linewidths=None, edgecolors=None)

  • x_axis_array_data:这是X轴的数据。这是包含X轴数据的数组。
  • y_axis_array_data:这是Y轴的数据。这是包含Y轴数据的数组。
  • s:该参数用于设置数据点的大小。
  • c:该参数用于设置数据点的颜色。
  • marker:该参数用于设置数据点的标记样式。
  • cmap:该参数用于设置数据点的颜色图。
  • alpha:该参数用于设置数据点的透明度。
  • linewidths:该参数用于设置连接数据点的线的宽度。
  • edgecolors:该参数用于设置连接数据点的线的颜色。

修改散点图参数,用PyPlot散点图创建可视化效果

你可以用以下命令安装matplotlib

!pip install matplotlib

或者,你也可以用Anaconda来安装它。

x_axis_array_data 和 y_axis_array_data

所有 参数 上面提到的文件是可选的 ,除了x_axis_array_data y_axis_array_data, 正如它们的名字所示,它们以数组的形式接收两组数值。最常见的是,NumPy数组的使用是为了让代码更有效地运行。 形状 (n, ), 需要.

例如 - 我们有一个数据集,其特征是在一些社交媒体上发布的视频的评分 数量,我们有一个评分值 ,从1-9不等。我们想从观众那里找到评分趋势。让我们试着做一些图,并尝试将趋势可视化。

# Basic scatter plot
import matplotlib.pyplot as plt
import numpy as np

ratings_value = np.asarray([2, 4, 5, 6, 8, 5, 2, 8, 5,
                            3, 2, 8, 6, 5, 4, 7, 8, 9, 7, 1])
number_of_ratings = np.asarray([10, 24, 17, 45, 23, 32, 67, 
                                34, 54, 54, 32, 67, 35, 23, 14, 16, 28, 32, 29, 28])

plt.title("Ratings Trend Visualization")
plt.xlabel("Number of ratings")
plt.ylabel("Ratings value")

plt.scatter(x = number_of_ratings, y = ratings_value)
plt.show()

基本散点图

尺寸参数

s - 指的是数据点的标记大小。它可以是一个浮点数或类似于数组。 shape (n, ), 可选

# Scatter plot with one specific size for all the markers: s parameter
import matplotlib.pyplot as plt
import numpy as np

ratings_value = np.asarray([2, 4, 5, 6, 8, 5, 2, 8, 5, 
                            3, 2, 8, 6, 5, 4, 7, 8, 9, 7, 1])
number_of_ratings = np.asarray([10, 24, 17, 45, 23, 32, 67, 
                                34, 54, 54, 32, 67, 35, 23, 14, 16, 28, 32, 29, 28])

plt.title("Ratings Trend Visualization")
plt.xlabel("Number of ratings")
plt.ylabel("Ratings value")

plt.scatter(x = number_of_ratings, y = ratings_value, s = 120)
plt.show()

带有特定尺寸标记的散点图

# Providing different sizes for each marker: As an array
import matplotlib.pyplot as plt
import numpy as np

ratings_value = np.asarray([2, 4, 5, 6, 8, 5, 2, 8, 5, 
                            3, 2, 8, 6, 5, 4, 7, 8, 9, 7, 1])
number_of_ratings = np.asarray([10, 24, 17, 45, 23, 32, 67, 
                                34, 54, 54, 32, 67, 35, 23, 14, 16, 28, 32, 29, 28])

plt.title("Ratings Trend Visualization")
plt.xlabel("Number of ratings")
plt.ylabel("Ratings value")

sizes = np.asarray([100, 240, 170, 450, 230, 320, 670, 340, 540, 
                                540, 320, 670, 350, 230, 140, 160, 280, 320, 290, 280])

plt.scatter(x = number_of_ratings, y = ratings_value, s = sizes)
plt.show()

带有多个尺寸标记的散点图

颜色参数

c - 类似数组或颜色列表或颜色。 可选.我们可以使用单一的颜色,甚至是颜色代码HEX值来得到一些非常好看的图。

# Using "c" parameter: with a specific color
import matplotlib.pyplot as plt
import numpy as np

ratings_value = np.asarray([2, 4, 5, 6, 8, 5, 2, 8, 5, 
                            3, 2, 8, 6, 5, 4, 7, 8, 9, 7, 1])
number_of_ratings = np.asarray([10, 24, 17, 45, 23, 32, 67, 
                                34, 54, 54, 32, 67, 35, 23, 14, 16, 28, 32, 29, 28])


sizes = np.asarray([100, 240, 170, 450, 230, 320, 670, 340, 540, 
                                540, 320, 670, 350, 230, 140, 160, 280, 320, 290, 280])


plt.title("Ratings Trend Visualization")
plt.xlabel("Number of ratings")
plt.ylabel("Ratings value")

plt.scatter(x = number_of_ratings, y = ratings_value, s = sizes, c = "green")
plt.show()

带C参数的散点图

标记参数

标记 - 指的是标记样式***,(默认:'o')。

# Using a different marker: (default: 'o')
import matplotlib.pyplot as plt
import numpy as np

ratings_value = np.asarray([2, 4, 5, 6, 8, 5, 2, 8, 5, 
                            3, 2, 8, 6, 5, 4, 7, 8, 9, 7, 1])
number_of_ratings = np.asarray([10, 24, 17, 45, 23, 32, 67, 
                                34, 54, 54, 32, 67, 35, 23, 14, 16, 28, 32, 29, 28])


sizes = np.asarray([100, 240, 170, 450, 230, 320, 670, 340, 540, 
                                540, 320, 670, 350, 230, 140, 160, 280, 320, 290, 280])



plt.title("Ratings Trend Visualization")
plt.xlabel("Number of ratings")
plt.ylabel("Ratings value")

plt.scatter(x = number_of_ratings, y = ratings_value, s = sizes, c = "green", marker = "^" )
plt.show()

带标记参数的散点图

色图参数

cmap - A 颜色图(Colormapcmap只在c是浮点数组的情况下使用,(默认:'viridis')。我们的颜色数组中的每个浮点值代表不同的颜色强度来绘制我们的数据。

Matplotlib模块有许多可用的颜色图。

颜色图就像一个颜色列表,每种颜色都有一个从0到100的值。

下面是一个颜色图的例子。

颜色条的图片

# Using cmap parameter: (Default: 'viridis')
import matplotlib.pyplot as plt
import numpy as np

ratings_value = np.asarray([2, 4, 5, 6, 8, 5, 2, 8, 5, 
                            3, 2, 8, 6, 5, 4, 7, 8, 9, 7, 1])
number_of_ratings = np.asarray([10, 24, 17, 45, 23, 32, 67, 
                                34, 54, 54, 32, 67, 35, 23, 14, 16, 28, 32, 29, 28])


sizes = np.asarray([100, 240, 170, 450, 230, 320, 670, 340, 540, 
                                540, 320, 670, 350, 230, 140, 160, 280, 320, 290, 280])

colors = np.asarray([1, 2, 5, 4, 6, 8, 6, 3, 5, 
                                4, 3, 6, 9, 2, 1, 6, 8, 8, 4, 5])

plt.title("Ratings Trend Visualization")
plt.xlabel("Number of ratings")
plt.ylabel("Ratings value")

plt.scatter(x = number_of_ratings, y = ratings_value, s = sizes, c = colors, cmap = "viridis" )
plt.show()

带有Cmap参数的散点图

阿尔法参数

alpha - 指的是生成的标记的透明度强度,范围从0到1。我们也使用cmap值作为 "绿色",以便更好地了解我们的alpha参数。

# Using alpha parameter
import matplotlib.pyplot as plt
import numpy as np

ratings_value = np.asarray([2, 4, 5, 6, 8, 5, 2, 8, 5, 
                            3, 2, 8, 6, 5, 4, 7, 8, 9, 7, 1])
number_of_ratings = np.asarray([10, 24, 17, 45, 23, 32, 67, 
                                34, 54, 54, 32, 67, 35, 23, 14, 16, 28, 32, 29, 28])


sizes = np.asarray([100, 240, 170, 450, 230, 320, 670, 340, 540, 
                                540, 320, 670, 350, 230, 140, 160, 280, 320, 290, 280])

colors = np.asarray([1, 2, 5, 4, 6, 8, 6, 3, 5, 
                                4, 3, 6, 9, 2, 1, 6, 8, 8, 4, 5])

plt.title("Ratings Trend Visualization")
plt.xlabel("Number of ratings")
plt.ylabel("Ratings value")

plt.scatter(x = number_of_ratings, y = ratings_value, s = sizes, c = colors, cmap = "Greens",
           alpha = 0.75)
plt.show()

带有阿尔法参数的散点图

线宽参数

linewidths- 指的是标记边缘的宽度,而 ***edgecolors-***指的是标记的颜色或颜色序列

# Using linewidths: (Default: 1.5)
# Using edgecolors

import matplotlib.pyplot as plt
import numpy as np

ratings_value = np.asarray([2, 4, 5, 6, 8, 5, 2, 8, 5, 
                            3, 2, 8, 6, 5, 4, 7, 8, 9, 7, 1])
number_of_ratings = np.asarray([10, 24, 17, 45, 23, 32, 67, 
                                34, 54, 54, 32, 67, 35, 23, 14, 16, 28, 32, 29, 28])


sizes = np.asarray([100, 240, 170, 450, 230, 320, 670, 340, 540, 
                                540, 320, 670, 350, 230, 140, 160, 280, 320, 290, 280])

colors = np.asarray([1, 2, 5, 4, 6, 8, 6, 3, 5, 
                                4, 3, 6, 9, 2, 1, 6, 8, 8, 4, 5])

plt.title("Ratings Trend Visualization")
plt.xlabel("Number of ratings")
plt.ylabel("Ratings value")

plt.scatter(x = number_of_ratings, y = ratings_value, s = sizes, c = colors, cmap = "Greens",
           alpha = 0.75, linewidths = 1, edgecolors = "Black")
plt.show()

边缘色和线宽的散点图

总结

在这篇文章中,我们学习了python中最常用的数据可视化方法之一。在多个图的帮助下,我们也看到了各种展示数据的方法,这些方法可以通过不同的组合来获得一些关于数据的伟大概述。散点图在整个Python社区被广泛使用,而matplotlib提供了这样一个工具,以一种非常简单和直观的方式绘制我们的数据。