Eastsheng's Wiki

Python奇淫巧计-3

2024-06-22 18:37:28

[toc]

给字体添加红色框ax.text()

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

# 创建一个简单的图
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [4, 5, 6])

# 添加带有红色边框的文本
ax.text(2, 5, 'Hello, World!', fontsize=12, color='black',
bbox=dict(facecolor='none', edgecolor='red', boxstyle='round,pad=0.5'))

# 显示图形
plt.show()

代码解释:

  • ax.text(x, y, text, ...):在图中的 (x, y) 位置添加文本 text

  • fontsize=12:设置字体大小为 12。

  • color='black':设置字体颜色为黑色。

  • bbox
    
    1
    2
    3

    :用于设置文本的边框。

    bbox
    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

    是一个字典,用于定义边框的属性。

    - `facecolor='none'`:边框的背景颜色设置为透明。
    - `edgecolor='red'`:边框线的颜色设置为红色。
    - `boxstyle='round,pad=0.5'`:边框样式为圆角矩形,并设置了一些内边距 `pad`。

    其他可调整的 `bbox` 参数:

    - `linewidth`:边框线的宽度。
    - `alpha`:边框的透明度。
    - `boxstyle`:边框样式,可以选择 `round`、`square` 等。

    这个代码会在图表中添加一个带有红色边框的文本“Hello, World!”,你可以根据需要调整这些参数来实现不同的效果。



    ### 单独画色棒

    ```python
    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)