You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何绘制字典中选定键值对的独立折线图

为每个键值对绘制独立折线图的实现方案

实现思路

  • 遍历目标字典中的每一组键值对,将键作为对应折线图的标题,值作为折线图的纵坐标数据
  • 使用matplotlib库为每组数据创建独立的绘图窗口,完成折线绘制并添加坐标轴标签、标题等元素

完整代码示例

import matplotlib.pyplot as plt

# 目标数据字典
pd_plot = {
    'MinMaxScalerLogisticRegression': [0,0,1,6,150,200],
    'StandardScalerHoeffdingTreeClassifier': [2,0,50,100],
    'MaxAbsScalerKNNClassifier': [23,45,56,0,0],
    'MinMaxScalerGaussianNB': [43,56,76,87,35],
    'MinMaxScalerKNNClassifier': [50,2,78,135,74,0,1,5,0],
    'StandardScalerKNNClassifier': [1,200],
    'MinMaxScalerHoeffdingTreeClassifier': [34,35,76,87,90,98,43,32,32,4,5],
    'StandardScalerLogisticRegression': [6,7,0,1,2,5],
    'MaxAbsScalerHoeffdingTreeClassifier': [2]
}

# 遍历每个键值对绘制独立折线图
for title, data in pd_plot.items():
    # 创建独立画布
    plt.figure(figsize=(8, 4))
    # 生成横坐标(数据的索引)
    x = range(len(data))
    # 绘制折线
    plt.plot(x, data, marker='o', linestyle='-', color='b')
    # 设置图表标题
    plt.title(title)
    # 设置坐标轴标签
    plt.xlabel('数据点索引')
    plt.ylabel('数值')
    # 显示网格
    plt.grid(True, alpha=0.3)
    # 显示图表
    plt.show()

代码说明

  • 横坐标使用数据列表的索引值,确保每个数据点都有对应的位置
  • 添加marker='o'标记每个数据点,提升折线图的可读性
  • 通过figsize设置统一画布大小,保证图表布局美观
  • 启用网格线辅助观察数据变化趋势

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 17:20:41