You need to enable JavaScript to run this app. 如何使用matplotlib读取多个CSV文件绘制x轴为对数刻度的折线图
优惠活动
大模型
产品
解决方案
定价
更多

如何使用matplotlib读取多个CSV文件绘制x轴为对数刻度的折线图

from numpy import genfromtxt
import matplotlib.pyplot as plt

# 替换为你自己的4个CSV文件路径、对应折线的显示名称
csv_list = [
    ("/content/drive/MyDrive/mse_loss_noisy_image.csv", "带噪图像MSE损失"),
    ("/content/drive/MyDrive/second_file.csv", "折线2"),
    ("/content/drive/MyDrive/third_file.csv", "折线3"),
    ("/content/drive/MyDrive/fourth_file.csv", "折线4")
]
# 如需自定义折线颜色可取消下面注释,否则matplotlib会自动分配不同颜色
# custom_colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728"]

for index, (file_path, line_label) in enumerate(csv_list):
    per_data = genfromtxt(file_path, delimiter=',')
    # 需要自定义颜色就添加c=custom_colors[index]参数
    plt.plot(per_data, label=line_label)

# 设置x轴为对数刻度
plt.xscale("log")

# 可选补充配置,按需开启
plt.xlabel("X轴说明")
plt.ylabel("Y轴说明")
plt.legend()
plt.title("图表标题")

plt.show()

实现逻辑说明:

  • 将4个CSV的路径和对应标识统一存入列表循环处理,避免重复编写读写代码
  • 调用plt.xscale("log")即可直接将x轴切换为对数刻度
  • 传入label参数后调用plt.legend()可以显示图例,区分不同折线

内容的提问来源于stack exchange,提问作者user309678

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 07:06:04