Eastsheng's Wiki

Python奇淫巧计-3

2024-06-22 18:37:28

[toc]

单独画色棒

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
import matplotlib.pyplot as plt
import numpy as np
import fastdataing as fd

def colorbar(orientation="vertical"):
if orientation=="vertical":
fig = fd.add_fig(figsize=(1.5, 6))
ax = fig.add_axes([0.45, 0.2, 0.10, 0.6])
else:
fig = fd.add_fig(figsize=(6, 1.5))
ax = fig.add_axes([0.2, 0.45, 0.6, 0.20])

cmap = plt.get_cmap('jet')
norm = plt.Normalize(vmin=0, vmax=1)
cb = plt.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), cax=ax, orientation=orientation)
cb.set_ticks([0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.savefig("./colorbar.png",dpi=600,transparent=True)
# 显示图像
plt.show()


def colorbar_T(orientation="vertical"):
if orientation=="vertical":
fig = fd.add_fig(figsize=(1.5, 6))
ax = fig.add_axes([0.45, 0.2, 0.10, 0.6])
else:
fig = fd.add_fig(figsize=(6, 1.5))
ax = fig.add_axes([0.2, 0.45, 0.6, 0.20])

cmap = plt.get_cmap('jet')
norm = plt.Normalize(vmin=257, vmax=317)
cb = plt.colorbar(plt.cm.ScalarMappable(norm=norm, cmap=cmap), cax=ax, orientation=orientation)
cb.set_ticks([257, 272 , 287, 302, 317])
plt.savefig("./colorbar_temp.png",dpi=600,transparent=True)
# 显示图像
plt.show()

if __name__ == "__main__":
# 调用函数以显示色棒
orientation = "horizontal" # vertical horizontal
# colorbar(orientation=orientation)
colorbar_T(orientation=orientation)

修改legend顺序

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
import matplotlib.pyplot as plt

# 创建示例数据
x = [1, 2, 3, 4]
y1 = [1, 4, 9, 16]
y2 = [1, 2, 3, 4]
y3 = [2, 3, 5, 7]

# 创建绘图对象
fig, ax = plt.subplots()

# 绘制数据曲线
line1, = ax.plot(x, y1, label='y = x^2')
line2, = ax.plot(x, y2, label='y = x')
line3, = ax.plot(x, y3, label='y = prime')

# 获取图例句柄和标签
handles, labels = ax.get_legend_handles_labels()

# 重新排列图例顺序
order = [2, 0, 1] # 按照你想要的顺序排列
ax.legend([handles[idx] for idx in order], [labels[idx] for idx in order])

# 显示图形
plt.show()

批量读取前缀相同的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import glob
import numpy as np

# 使用glob找到所有前缀相同的文件
file_prefix = "your_file_prefix"
files = glob.glob(f"{file_prefix}*")

# 初始化一个空的列表来存储所有文件的数据
all_data = []

# 遍历所有找到的文件并使用loadtxt读取数据
for file in files:
data = np.loadtxt(file)
all_data.append(data)

# 如果需要将所有数据合并成一个数组,可以使用np.vstack或np.hstack
# 例如,假设每个文件的结构相同
combined_data = np.vstack(all_data)

print(combined_data)

一行一列叉乘为矩阵

1
2
3
4
5
6
7
8
9
10
11
import numpy as np

# 假设你的一行向量和一列向量如下
row_vector = np.array([1, 2, 3]) # 1行
column_vector = np.array([4, 5, 6]) # 1列

# 使用外积生成 m行n列 矩阵
result_matrix = np.outer(column_vector, row_vector)

print(result_matrix)

将一组很多的legend分为两个部分分别放置在Figure上

并将框框设置透明

1
2
3
first_legend = plt.legend(handles=plt.gca().get_lines()[:3], loc='upper center')
plt.gca().add_artist(first_legend).get_frame().set_alpha(0)
second_legend = plt.legend(handles=plt.gca().get_lines()[3:], loc='upper right').get_frame().set_alpha(0)

利用pathlib库创建文件夹

1
2
from pathlib import Path
Path(PATH+"msd/").mkdir(parents=True,exist_ok=True)