0

I'm need to plot a graphic using matlib.plot in python like this:

enter image description here

actually I'm using a code that plots all bars

bars = ('Optimum CPLEX', 'eUCB', 'ONETS', 'e-greedy')
    y_pos = [1, 3, 5, 7]
    y_pos_2 = [2, 4, 6, 8]

    plt.ylabel('Average Reward \u03B7_i')
    plt.suptitle('Optimal Model versus Heuristics')
    name = 'grafics/11_reward_utilization.png'
         
    # Create bars
    plt.bar(y_pos, reward, color=['blue', 'blue', 'blue', 'blue'])
    plt.bar(y_pos_2, utilization, color=['yellow', 'yellow', 'yellow', 'yellow'])
         
    # Create names on the x-axis
    plt.xticks(y_pos, bars)
         
    # Show graphic
    plt.savefig(name, transparent = True)
    plt.show()

but as a result I receive a graphics with all bars but in the same dimensions, like this:

enter image description here

someone knows how I can plot this time of graphics which two differents dimensions of data in the y axis?

thank you.

Henrique Lima
  • 57
  • 1
  • 5

1 Answers1

1

have you defined the reward and utilization???

your example with these definitions works for me:

import matplotlib.pyplot as plt

bars = ('Optimum CPLEX', 'eUCB', 'ONETS', 'e-greedy')
y_pos = [1, 3, 5, 7]
reward = [1,2,3,4]

y_pos_2 = [2, 4, 6, 8]
utilization = [4,3,2,1]

plt.ylabel('Average Reward \u03B7_i')
plt.suptitle('Optimal Model versus Heuristics')
name = '11_reward_utilization.png'

# Create bars
plt.bar(y_pos, reward, color=['blue', 'blue', 'blue', 'blue'])
plt.bar(y_pos_2, utilization, color=['yellow', 'yellow', 'yellow', 'yellow'])

# Create names on the x-axis
plt.xticks(y_pos, bars)
# Show graphic
plt.savefig(name, transparent=True)
plt.show()
ktp
  • 126
  • 8
  • yes, I have. This codes works, but in this case I have very different dimensions of data between reward and utilization. And this make the graphics not so good to read the information. If you observe in the figure we have a very low data of utilization in yellow. And, in the reference graphic we have on left a scale to reward and on right a scale to utilization. Two different scales in y axes in the same graphic. Is exactly this that I want to do. – Henrique Lima Jan 03 '21 at 11:42
  • maybe this [article](https://samchaaa.medium.com/how-to-plot-two-different-scales-on-one-plot-in-matplotlib-with-legend-46554ba5915a) will offer some help – ktp Jan 03 '21 at 12:42
  • one more, probably useful [link](https://stackoverflow.com/questions/24183101/pandas-bar-plot-with-two-bars-and-two-y-axis) – ktp Jan 03 '21 at 13:35