python - Pyomo 使用 matplotlib 可视化数据
问题描述
我已经解决了一个模型并尝试使用 matplolib 可视化结果。
该模型可以正常运行。我正在尝试修复变量并重新求解此模型并输出三个不同的条形图以便与其他条形图进行比较。但是我应用matplotlib画柱状图的时候,只画了最后一张(成本20171.984)
如何输出三个不同的条形图?
到目前为止我所做的是这样的:
for s in [800, 1000, 1200]:
instance.demand = s
opt = SolverFactory('Ipopt')
results = opt.solve(instance)
print('Cost: ', value(instance.cost))
# cost printed
Cost: 13139.706753161874
Cost: 16460.857089915964
Cost: 20171.984469196814
# Matplotlib
plt.figure(figsize=(8, 6))
x = [0.1]
plt.bar(x, value(instance.cost), alpha=0.7, width=0.015).........Code omitted in this part
你能帮我吗?谢谢!
解决方案
3 个成本,1 个图表:
import matplotlib.pyplot as plt
%matplotlib inline
xlabels = ["bar1","bar2", "bar3"]
costs = [13139, 16460, 33333]
bar = plt.bar(xlabels, costs, width=0.8, color=['blue', 'red', 'green'])
plt.xticks(range(len(xlabels)), xlabels)
plt.ylabel("Costs")
plt.xlabel("X label")
plt.legend((bar[0], bar[1], bar[2]), xlabels, loc="upper right")
plt.title("My chart")
plt.show()
推荐阅读
- php - 将下拉值插入数据库
- python - 即使经过大量检查和重新映射,if/else 语句也没有运行嵌入式代码
- php - 徽标的架构标记 [application/ld+json]
- r - 如何在R中按字母顺序对单个列进行排序?
- scroll - 水平滚动站点中的本机延迟加载 (loading="lazy")
- gcc - 我需要什么扩展才能从 VSCode 启动 gcc?
- python - 当我没有要求时,Pandas 将列的数据类型从浮点数更改为整数
- javascript - Apps 脚本 - OOP - 在 HTML 侧边栏中运行类方法
- reactjs - 在 Paypal(奥地利)中禁用替代方法
- reactjs - 反应原生上下文和不推荐使用的超级用法