Python科学绘图

Python科学绘图

Python科学绘图

常见python主流绘图工具库

matplotlib

seraborn

proplot

SciencePlots

matplotlib

安装

散点图

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 输入文件位置,红点坐标,输出图
def sandian1(file_path: str,
red_point_por: float,
red_point_K: float,
red_point_name: str,
):
df = pd.read_csv(file_path, header=None, names=['img_name', 'por', 'K'])

df['por'] = pd.to_numeric(df['por'], errors='coerce')
df['por'] = df['por'] * 100
df['K'] = pd.to_numeric(df['K'], errors='coerce')
por = df['por']
K = df['K']
por2 = red_point_por
K2 = red_point_K
name2 = red_point_name

# 创建图形和坐标轴 1in=2.54 figsize=(宽度, 高度) 10 , 7
fig, ax = plt.subplots(figsize=(3.9, 2.7), dpi=300)

# 设置全局字体为 Times New Roman
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.serif'] = ['Times New Roman'] + plt.rcParams['font.serif']

# 绘制数据组1(黑色方块)
ax.scatter(por, K, marker='s', color='black', facecolors='none', s=50, label=img_name)
# 绘制数据组2(红色圆点)
ax.scatter(por2, K2, marker='o', color='red', s=80, label=name2)

# 设置为对数y轴
ax.set_yscale('log')
# 取消 y 轴的科学计数法
formatter = ScalarFormatter(useOffset=False, useMathText=False)
ax.yaxis.set_major_formatter(formatter)
# 设置坐标轴范围
ax.set_xlim(10, 30)
ax.set_ylim(1, 1000)
# 设置主刻度和次刻度的位置和格式
ax.xaxis.set_major_locator(ticker.MultipleLocator(5)) # 主刻度间隔为5
#ax.xaxis.set_minor_locator(ticker.MultipleLocator(1)) # 次刻度间隔为1

# 分别设置 x 轴和 y 轴的刻度标签大小
ax.tick_params(axis='x', labelsize=10) # 设置 x 轴刻度标签大小
ax.tick_params(axis='y', labelsize=10) # 设置 y 轴刻度标签大小
# 获取当前刻度标签
x_labels = ax.get_xticklabels()
y_labels = ax.get_yticklabels()
# 设置字体属性
for label in x_labels:
label.set_fontname('Times New Roman') # 设置字体
label.set_fontweight('bold') # 设置粗体
for label in y_labels:
label.set_fontname('Times New Roman') # 设置字体
label.set_fontweight('bold') # 设置粗体

# 设置刻度长度和宽度方向
ax.tick_params(axis='x', which='major', length=4, width=1, direction='in')
ax.tick_params(axis='x', which='minor', length=3, width=1, direction='in')
ax.tick_params(axis='y', which='major', length=4, width=1, direction='in')
ax.tick_params(axis='y', which='minor', length=3, width=1, direction='in')

# 设置坐标轴线条样式
ax.spines['bottom'].set_linewidth(1) # 底部轴线宽度
ax.spines['left'].set_linewidth(1) # 左侧轴线宽度
ax.spines['top'].set_linewidth(1) # 顶部轴线宽度
ax.spines['right'].set_linewidth(1) # 右侧轴线宽度

# 坐标轴标题
ax.set_xlabel('POR(%)', fontsize=11, fontname='Times New Roman', fontweight='bold')
ax.set_ylabel('Permeability(mD)', fontsize=11, fontname='Times New Roman', fontweight='bold')

# 添加图例
legend = ax.legend(fontsize=8)
# 去掉框线和背景
legend.get_frame().set_linewidth(0.0) # 设置框线宽度为0
legend.get_frame().set_facecolor('none') # 设置背景为透明

# 添加网格
ax.grid(True, which='both', linestyle='-', linewidth=0.5, alpha=0.5)

# 调整布局
plt.tight_layout()

return plt

箱型图

1

小提琴图

1

文章作者: HibisciDai
文章链接: http://hibiscidai.com/2025/02/27/Python科学绘图/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 HibisciDai
好用、实惠、稳定的梯子,点击这里