python曲线图

  1. 安装 python

  2. 安装 uv:pip install uv

  3. 建立项目环境

    1
    2
    3
    4
    uv init py_demo_one
    cd py_demo_one
    uv sync
    uv add pandas openpyxl matplotlib scipy

添加处理 excel 的 python 代码,如下:

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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import CubicSpline # 使用三次样条插值

# 读取Excel数据(假设Excel文件名为data.xlsx)
df = pd.read_excel("data.xlsx", engine="openpyxl") # 确保安装openpyxl
x = df["scan"].values
y = df["ms"].values

# 生成插值后的密集点(使曲线光滑)
x_new = np.linspace(x.min(), x.max(), 500) # 500个点足够光滑
cs = CubicSpline(x, y) # 三次样条插值
y_smooth = cs(x_new)

# 绘制图形
plt.figure(figsize=(10, 6))
plt.plot(x_new, y_smooth, "b-", linewidth=2, label="qu xian")
plt.scatter(x, y, color="red", s=20, zorder=5, label="origin dot") # 显示原始点
plt.xlabel("scan", fontsize=12)
plt.ylabel("ms", fontsize=12)
plt.title("scan vs ms de guanghua quxian", fontsize=14)
plt.grid(True, linestyle="--", alpha=0.7)
plt.legend()
plt.tight_layout()
plt.show()