如何调整Matplotlib散点图标记点大小并改为十字形
Matplotlib散点图:调整标记大小与替换标记样式
调整散点标记大小
在ax.scatter()函数中,通过**s参数**控制标记大小,数值越大标记面积越大(默认值通常在20左右,可根据需求调整)。
替换为十字形标记
通过**marker参数**可替换标记样式,常用的十字形标记有两种:
marker='+':实心十字marker='x':叉形十字
修改后的完整代码
import numpy as np import matplotlib.pyplot as plt # 输入点的x、y坐标和颜色 xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50] ys = [64, 72, 47, 38, 55, 30, 47, 64, 73, 30, 55, 64, 38, 47, 73, 55, 38, 30, 72, 47, 63, 73, 47, 44, 38, 55, 30, 38, 55, 72, 47, 38, 65, 47, 38, 63, 30, 55, 72, 55, 47, 53, 73, 30, 38, 55, 30, 38, 64, 73] colors = ['g'] # 设置坐标轴范围和刻度间隔 xmin, xmax, ymin, ymax = 0, 50, 0, 80 ticks_frequency = 5 # 绘制散点 - 添加s和marker参数调整标记 fig, ax = plt.subplots(figsize=(10, 10)) ax.scatter(xs, ys, c=colors, s=100, marker='+') # 设置坐标轴等比例 ax.set(xlim=(xmin-1, xmax+1), ylim=(ymin-1, ymax+1), aspect='equal') # 隐藏顶部和右侧边框 ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) # 设置坐标轴标签位置 ax.set_xlabel('n', size=14, labelpad=-24, x=1.03) ax.set_ylabel('t in s', size=14, labelpad=-21, y=1.02, rotation=0) # 设置主刻度 x_ticks = np.arange(xmin, xmax+1, ticks_frequency) y_ticks = np.arange(ymin, ymax+1, ticks_frequency) ax.set_xticks(x_ticks[x_ticks != 0]) ax.set_yticks(y_ticks[y_ticks != 0]) # 设置次刻度用于网格线 ax.set_xticks(np.arange(xmin, xmax+1), minor=True) ax.set_yticks(np.arange(ymin, ymax+1), minor=True) # 绘制网格线 ax.grid(which='both', color='grey', linewidth=1, linestyle='-', alpha=0.2) plt.show()
补充说明
s参数:控制标记的面积,可根据可视化效果自行调整数值(如50、150等)marker参数:Matplotlib支持数十种标记样式,除十字形外,还可选择's'(正方形)、'^'(三角形)等,按需使用即可。
内容的提问来源于stack exchange,提问作者justcallmepaul
相关产品推荐
相关产品推荐

