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

将Gurobi优化结果字典转DataFrame后如何按行绘制图表?

按行绘制DataFrame数据图表的实现方案

核心思路

你的DataFrame是96行1列结构,索引对应time1的元素,每行存储计算后的单值,直接借助Pandas内置绘图工具或Matplotlib就能轻松实现按行索引顺序的可视化。

具体代码实现

方法1:Pandas内置绘图(最简方案)

直接调用DataFrame的plot()方法,默认按行索引顺序渲染:

import pandas as pd
import matplotlib.pyplot as plt

# 你的原有数据处理代码
x = {}
for t in time1:
    x[t] = [price_energy[t-1] * EnergyResource[174, t].X] 

df = pd.DataFrame.from_dict(x, orient='index')

# 绘制折线图(适配时序类数据)
df.plot(kind='line', figsize=(12, 6))
plt.title('逐行结果可视化')
plt.xlabel('时间点')
plt.ylabel('计算值')
plt.show()

方法2:Matplotlib手动绘制(高自定义度)

若需自定义图表细节(如标签旋转、颜色、样式),直接使用Matplotlib:

import matplotlib.pyplot as plt

# 提取行索引和对应值
row_indices = df.index
row_values = df.iloc[:, 0].values

# 绘制折线图
plt.figure(figsize=(12, 6))
plt.plot(row_indices, row_values, color='#2ecc71', linewidth=2)
plt.title('逐行结果可视化')
plt.xlabel('时间点')
plt.ylabel('计算值')
plt.xticks(rotation=45)  # 若索引为长字符串,旋转避免重叠
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

可选图表类型

  • 若展示离散类数据,将plot()的kind参数改为'bar'绘制柱状图
  • 若分析数据分布,改为'hist'绘制直方图
  • 若需对比单个值占比,改为'pie'绘制饼图(需注意数据适配性)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 17:05:25