如何增加 Matplotlib 中散点的大小?
先决条件: Matplotlib
散点图是 matplotlib 库中 x 轴和 y 轴之间图形上的数据点。图中的点看起来很分散,因此该图被命名为“散点图”。如果不使用语法中的可选参数,散点图中的点默认很小。可选参数 's' 用于增加 matplotlib 中散点的大小。下面讨论了可以设置 s 的各种方法。
Syntax :
matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None, marker=None, cmap=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None)
Parameters:
- x_axis_data- An array containing x-axis data
- y_axis_data- An array containing y-axis data
- s- marker size (can be scalar or array of size equal to size of x or y)
- c- color of sequence of colors for markers
- marker– marker style
- cmap- cmap name
- linewidths- width of marker border
- edgecolor- marker border color
- alpha- blending value, between 0 (transparent) and 1 (opaque)
方法
- 导入模块
- 创建数据
- 设置 s 的值
- 绘制散点图
- 显示图
参数s可以有多种设置方式,可以是固定值,也可以是变量。当 s 设置为变量值时,散点图上的数据点大小不同。下面给出实现:
示例 1:散点图中固定大小增加的数据点
Python3
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('seaborn')
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [8, 7, 6, 4, 5, 6, 7, 8, 9, 10]
plt.xticks(np.arange(11))
plt.yticks(np.arange(11))
plt.scatter(x, y, s=500, c='g')
plt.title("Scatter Plot", fontsize=25)
plt.xlabel('x-axis', fontsize=18)
plt.ylabel('y-axis', fontsize=18)
plt.show()
Python3
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('seaborn')
x = [1,2,3,4,5,6,7,8,9,10,11,12]
y = [1,2,3,4,5,6,7,8,9,10,11,12]
points_size = [100,200,300,400,500,600,700,800,900,1000,1100,1200]
plt.xticks(np.arange(13))
plt.yticks(np.arange(13))
plt.scatter(x,y,s=points_size,c='g')
plt.title("Scatter Plot with increase in size of scatter points ", fontsize=22)
plt.xlabel('x-axis',fontsize=20)
plt.ylabel('y-axis',fontsize=20)
plt.show()
Python3
import matplotlib.pyplot as plt
plt.style.use('seaborn')
plt.figure(figsize=(10, 10))
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [3*i+2 for i in x]
size = [n*100 for n in range(1, len(x)+1)]
# print(size)
plt.scatter(x, y, s=size, c='g')
plt.title("Scatter Plot with increase in size of scatter points ", fontsize=22)
plt.xlabel('X-axis', fontsize=20)
plt.ylabel('Y-axis', fontsize=20)
plt.xticks(x, fontsize=12)
plt.yticks(y, fontsize=12)
plt.show()
输出:
示例 2:散点图中大小可变的数据点
蟒蛇3
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('seaborn')
x = [1,2,3,4,5,6,7,8,9,10,11,12]
y = [1,2,3,4,5,6,7,8,9,10,11,12]
points_size = [100,200,300,400,500,600,700,800,900,1000,1100,1200]
plt.xticks(np.arange(13))
plt.yticks(np.arange(13))
plt.scatter(x,y,s=points_size,c='g')
plt.title("Scatter Plot with increase in size of scatter points ", fontsize=22)
plt.xlabel('x-axis',fontsize=20)
plt.ylabel('y-axis',fontsize=20)
plt.show()
输出:
示例 3:
蟒蛇3
import matplotlib.pyplot as plt
plt.style.use('seaborn')
plt.figure(figsize=(10, 10))
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [3*i+2 for i in x]
size = [n*100 for n in range(1, len(x)+1)]
# print(size)
plt.scatter(x, y, s=size, c='g')
plt.title("Scatter Plot with increase in size of scatter points ", fontsize=22)
plt.xlabel('X-axis', fontsize=20)
plt.ylabel('Y-axis', fontsize=20)
plt.xticks(x, fontsize=12)
plt.yticks(y, fontsize=12)
plt.show()
输出: