Python

matplotlib

matplotlib 如何支持中文

1
2
3
4
5
6
7
8
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties # 字体

# version of matplotlib : 3.7.2

plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
plt.rcParams['font.size'] = 18 # 字号

matplotlib 如何配置其他全局字体

1
2
3
4
5
6
7
8
9
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm

# version of matplotlib : 3.7.1

font_path = "/mnt/path/to/newfont.ttf"
fm.fontManager.addfont(font_path)
newfont = fm.FontProperties(fname=font_path)
plt.rcParams['font.family'] = newfont.get_name()

matplotlib 绘图网格分区

可以使用 gridspec 对图表进行分区,分别绘图

1
2
3
4
5
6
7
8
9
10
11
12
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# version of matplotlib : 3.7.1

# 将整个图的区域分成 10×2 的网格
gs = gridspec.GridSpec(10, 2)

# 得到三个ax 分别绘图互不干扰
main_ax = plt.subplot(gs[0:7, :]) # 占据0-7行0-1列
legend1_ax = plt.subplot(gs[8:10, 0]) # 占据8-9行0列
legend2_ax = plt.subplot(gs[8:10, 1]) # 占据8-9行1列

例如,以下雷达图和两个legend分别是三个不同的 ax

PixPin_2025-02-11_01-52-34

环境配置

conda 环境迁移后, pip无法下载东西

首先切换到对应环境, 找到 pip 的位置, 得到 pip 的路径

1
2
3
conda activate env
which pip
>>> /mnt/new/path/to/anaconda3/envs/prec/bin/pip

进入编辑 /mnt/new/path/to/anaconda3/envs/prec/bin/pip 文件, 看到如下内容

1
2
3
4
5
6
7
8
9
10
11
12
#!/mnt/old/path/to/anaconda3/envs/prec/bin/python

# -*- coding: utf-8 -*-
import re
import sys

from pip._internal.cli.main import main

if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())

把第一行 conda 的旧路径换为新路径即可

1
2
3
4
5
6
7
8
9
10
11
12
#!/mnt/new/path/to/anaconda3/envs/prec/bin/python

# -*- coding: utf-8 -*-
import re
import sys

from pip._internal.cli.main import main

if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())

debug

debugpy

  • 在vscode(python插件一条龙)中使用debugpy包通过命令行带参数debug

    • 下载 debugpy

      1
      pip install debugpy
    • launch.json 中配置,挑一个本地空闲的端口

      image-20250313172332972
    • 在命令行中插入 -m debugpy --listen [端口号] --wait-for-client 例如:

      1
      python -m debugpy --listen [端口号] --wait-for-client xxx.py -arg1 ARG1 -arg2 ARG2
    • 最后开始 debug 即可

LaTeX

缩进

有时某些环境下无法自动缩进,这个可以派上用场

1
\hspace{\parindent} %水平方向按照默认缩进空出对应区域

有时某些环境下自动缩进,但我们不想要,可以

1
\noindent %本段无缩进

图片

子图

放置子图

1
2
3
4
5
6
7
8
9
10
\usepackage{subfig}
\usepackage{subcaption}

\begin{figure}[htbp]
\centering
\subfloat[...sub caption...]{\includegraphics[width=2.45in]{...path...}
\hfil %间距
\subfloat[...sub caption...]{\includegraphics[width=2.45in]{...path...}
\caption{...main caption...}
\end{figure}

表格

此网站可以 GUI 表格转 LaTeX https://www.tablesgenerator.com/

跨页表格

有的表格太长需要跨页,可以使用 \longtable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
\usepackage{longtable}

\setlength{\tabcolsep}{15pt}{ % 参数自己调
\begin{longtable}{ccc}
\label{...}\\
\caption{...}\\
\hline
A & B & C \\
\hline
\endfirsthead % 首页表头
\hline
A & B & C \\
\hline
\endhead % 其它页表头
\hline
\endfoot % 表尾
A & B & C \\
\hline
\end{longtable}
}

单元格合并

有时会出现合并单元格的需求,可以使用 \multicolumn

1
2
3
4
5
6
7
8
9
\begin{tabular}{|c|c|c|c|}
\hline
\multicolumn{4}{|c|}{Boundary value} \\
\hline
1 & 2 & 3 & 4 \\
\hline
1 & 2 & 3 & 4 \\
\hline
\end{tabular}

单元格绘制斜线

有时会有给单元格绘制斜线分割区域的需求,可以使用 \diagbox

1
2
3
4
5
6
7
\begin{tabularx}{\linewidth}{|c|X|X|X|}
\hline
\diagbox{参数}{accuracy}{优化器} & SGD & SGD-momentum & RMS Prop \\
\hline
0.0 & 0.8882 & 0.9710 & 0.9902 \\
\hline
\end{tabularx}

效果是这样的

image-20240221215708873

数学

常用命令

1
2
3
4
5
6
\renewcommand{\d}{\mathrm{d}}  % 微分符号
\renewcommand{\p}{\mathrm{\partial}} % 偏微分符号
\renewcommand{\v}[1]{\vec{#1}} % 向量符号
\renewcommand{\b}[1]{\bar{#1}} % 均值符号

\tilde{x} % x上波浪线

Linux

ZIP 分卷压缩

  • ubantu服务器上的巨型数据迁移时往往需要 分卷压缩 , 此时可以
1
zip -s [size:1g 250m] -r [.../target.zip] [path/to/zip/]

最终会得到 target.zip target.z01 target.z02 … ,在转移至目标服务器后, 使用如下命令 合卷并解压 即可

1
2
zip -FF target.zip --out target_all.zip
unzip target_all.zip
  • 有时某些子目录/文件需要 排除 掉, 可以把要排除的子目录/文件列一个exclude.txt 例如:
1
2
3
/mnt/.../path/to/zip/models/AAA/
/mnt/.../path/to/zip/models/BBB/ccc.pth
...

然后输入如下bash命令

1
2
3
4
5
exclude_args=()
while IFS= read -r line; do
exclude_args+=(-x "$line/*")
done < exclude.txt
zip -s [size:1g 250m] -r [.../target.zip] [path/to/zip/] "${exclude_args[@]}"

Linux终端输入回显

  • 在 Linux 设备上运行规模较大的命令后,终端有时会可以 输入执行但无法显示命令字符 ,遇到这种问题后,原本我一直是 关掉再开一个(好蠢) ,现在知道了原来是 stty 关闭了输入回显,在发生上述现象后,只需要盲打(因为看不见)以下命令:
1
stty echo # 开启输入回显
  • 如果想欺负一下小伙伴们(嘿嘿~
1
stty -echo # 关闭输入回显