如何创建带有负轴和正轴的散点图?
先决条件: Matplotlib
在散点图中,两个不同数值变量的值由点或圆圈表示。它也被称为散点图或又名散点图。散点图中的单个数据点由横轴和纵轴上的每个点表示。
通常,散点图绘制在正值上,但是当数据集同时具有负值和正值时会发生什么。因此,在本文中,我们将创建具有负轴和正轴的散点图。
安装:
要安装 matplotlib 库,请在命令提示符中编写以下命令。
pip install matplotlib
借助一些函数,我们可以绘制负轴和正轴的散点图如下:
plt.scatter(x, y, c, cmap)
plt.axvline(x, ymin, ymax, c, ls)
plt.axhline(y, xmin, xmax, c, ls)
Function | Description |
scatter() | Creates the scatter plot on the given data. |
axvline() | Add a vertical line in data coordinates. |
axhline() | Add a horizontal line in data coordinates. |
循序渐进的方法:
- 导入必要的库。
- 创建或导入数据集以创建绘图。
- 使用plt.scatter()创建散点图,其中 pas x 和 ya 参数。
- 由于绘图具有负轴和正轴坐标,因此使用plt.axvline()和plt.axhline()函数在绘图中添加一条垂直和水平线,并将原点作为 0 并根据您和线条样式传递颜色,即 ls 作为您想。
- 绘图现在分为正轴和负轴。
- 为了使绘图更具吸引力和易于理解,使用plt.xlabel() 、 plt.ylabel()和plt.title()函数为绘图提供 x 标签、y 标签和标题,其中将字符串作为参数传递。
- 在上面讨论的 scatter()函数添加配色方案和网格样式传递颜色和 cmap 即颜色映射,对于网格样式在程序代码中创建散点图之前添加plt.style.use()并传递样式你想作为参数。
- 添加一个颜色条,用于使用plt.colormap()函数将数值映射到颜色进行可视化。
- 现在用于可视化绘图使用plt.show()函数。
示例 1:使用 Matplotlib 库创建带有负轴和正轴的默认散点图。
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20, 15,
30, 20, 2, 4, 3, -7, -8, -13, -16, 16, -15, 32,
-12, -19, 25, -25, 30, -6, -18, -11, -14, -21,
27, -21, -14, -4, -1, 0, 17]
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y)
# visualizing the plot using plt.show() function
plt.show()
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20, 15, 30, 20,
2, 4, 3, -7, -8, -13, -16, 16, -15, 32, -12, -19, 25,
-25, 30, -6, -18, -11, -14, -21, 27, -21, -14, -4, -1, 0, 17]
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y)
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')
# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
# visualizing the plot using plt.show() function
plt.show()
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20,
15, 30, 20, 2, 4, 3, -7, -8, -13, -16, 16,
-15, 32, -12, -19, 25, -25, 30, -6, -18,
-11, -14, -21, 27, -21, -14, -4, -1, 0, 17]
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y, c=y, cmap='plasma')
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')
# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
# giving X and Y label
plt.xlabel("X axis")
plt.ylabel("Y axis")
# giving title to the plot
plt.title("Scatter Plot with both negative and positive axes")
# visualizing the mapping from values to colors
plt.colorbar()
# visualizing the plot using plt.show() function
plt.show()
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20,
15, 30, 20, 2, 4, 3, -7, -8, -13, -16, 16,
-15, 32, -12, -19, 25, -25, 30, -6, -18,
-11, -14, -21, 27, -21, -14, -4, -1, 0, 17]
# adding style theme in scatter plot
plt.style.use('seaborn')
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y, c=y, cmap='plasma')
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')
# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
# giving x label to the plot
plt.xlabel("X axis")
# giving y label to the plot
plt.ylabel("Y axis")
# giving title to the plot
plt.title("Scatter Plot with both negative and positive axes")
# visualizing the mapping from values to colors
plt.colorbar()
# visualizing the plot using plt.show() function
plt.show()
输出:
上面的输出显示了带有负轴和正轴的散点图,但在此图中,很难分析点,因为有些在负轴上,有些在正轴上。
所以让我们让这个情节更容易理解。
示例 2:创建具有负轴和正轴的散点图。
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20, 15, 30, 20,
2, 4, 3, -7, -8, -13, -16, 16, -15, 32, -12, -19, 25,
-25, 30, -6, -18, -11, -14, -21, 27, -21, -14, -4, -1, 0, 17]
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y)
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')
# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
# visualizing the plot using plt.show() function
plt.show()
输出:
在上面的输出中,我们在plt.axvline()函数和plt.axhline()函数的帮助下,通过将 0 作为指定线的参数,从数据坐标中心绘制了垂直线和水平线将在 0 坐标处绘制,颜色参数指定线条和 ls 的颜色,即线条样式。
示例 3:通过添加配色方案创建具有负轴和正轴的散点图。
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20,
15, 30, 20, 2, 4, 3, -7, -8, -13, -16, 16,
-15, 32, -12, -19, 25, -25, 30, -6, -18,
-11, -14, -21, 27, -21, -14, -4, -1, 0, 17]
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y, c=y, cmap='plasma')
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')
# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
# giving X and Y label
plt.xlabel("X axis")
plt.ylabel("Y axis")
# giving title to the plot
plt.title("Scatter Plot with both negative and positive axes")
# visualizing the mapping from values to colors
plt.colorbar()
# visualizing the plot using plt.show() function
plt.show()
输出:
在上面的输出中,我们将配色方案添加到了等离子中,并给出了相对于 y 值的颜色,并添加了颜色条来可视化从值到颜色的映射,并给出了图的 x 标签、y 标签和标题,所以该图看起来更具交互性且易于理解。
为了添加配色方案,我们传递了参数 c,它指的是给出颜色,cmap 代表具有寄存器颜色图列表的颜色图,使用plt.colormap()函数添加颜色图并添加 X 标签、Y 标签和标题分别使用 plt.xlabel()函数plt.ylabel()函数和 plt.title() 。
示例 4:通过添加主题创建带有负轴和正轴的散点图。
Python
# importing libraries
import matplotlib.pyplot as plt
import numpy as np
# creating two array for plotting
x = np.arange(-20, 20, 1)
y = [-30, -20, -10, -3, 1, 11, 10, 5, -20, 20,
15, 30, 20, 2, 4, 3, -7, -8, -13, -16, 16,
-15, 32, -12, -19, 25, -25, 30, -6, -18,
-11, -14, -21, 27, -21, -14, -4, -1, 0, 17]
# adding style theme in scatter plot
plt.style.use('seaborn')
# creating scatter plot with both negative
# and positive axes
plt.scatter(x, y, c=y, cmap='plasma')
# adding vertical line in data co-ordinates
plt.axvline(0, c='black', ls='--')
# adding horizontal line in data co-ordinates
plt.axhline(0, c='black', ls='--')
# giving x label to the plot
plt.xlabel("X axis")
# giving y label to the plot
plt.ylabel("Y axis")
# giving title to the plot
plt.title("Scatter Plot with both negative and positive axes")
# visualizing the mapping from values to colors
plt.colorbar()
# visualizing the plot using plt.show() function
plt.show()
输出:
在上面的输出中,我们在散点图中添加了类似网格的样式主题,使该图看起来更具交互性且易于理解。要添加样式主题,请在程序代码中创建散点图之前添加plt.style.use()函数。