Search code examples
pythonmatplotlibpyomo

Pyomo visualizing data with matplotlib


I've solved a model and trying to visualize the results by using matplolib.

This model can run properly. I am trying to fix the variables and re-solve this model and output three different bar charts in order to compare it with others. But when I applied matplotlib to draw the bar chart, only the last one was drawn (cost 20171.984)

How to output three different bar charts?

what I have done so far is like that:

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

Could you help me with it? Thanks!


Solution

  • You could try:

    costs = []
    for s in [800, 1000, 1200]:
        instance.demand = s
        opt = SolverFactory('Ipopt')
        results = opt.solve(instance)
        print('Cost: ', value(instance.cost))
        costs.append(value(instance.cost))
    

    You then have the list costs which you can plot as shown in the previous answer.