Matplotlib and ValueError: shape mismatch: objects cannot be broadcast to a single shape

Viewed 30

I am programming a class to draw the quality evolution of the solutions obtained by an algorithm using matplotlib.

    def __init__(self):
        # define and adjust figure
        self.fig = plt.figure(figsize=(12, 6), facecolor='#DEDEDE')
        self.ax_best = plt.subplot(121)
        self.ax_curr = plt.subplot(122)
        self.ax_best.set_title('Best solution')
        self.ax_curr.set_title('Current solution')
        self.ax_best.set_xlabel('Iteration')
        self.ax_best.set_ylabel('Cost')
        self.ax_curr.set_xlabel('Iteration')
        self.ax_curr.set_ylabel('Cost')
        self.ax_best.set_facecolor('#DEDEDE')
        self.ax_curr.set_facecolor('#DEDEDE')
        self.ax_best.grid(color='w', linestyle='-', linewidth=1)
        self.ax_curr.grid(color='w', linestyle='-', linewidth=1)

        # initialize variables
        self.best_cost = []
        self.curr_cost = []

        # initialize lines
        self.best_line, = self.ax_best.plot([1,2,3,4], color='b', label='Total cost')
        self.curr_line, = self.ax_curr.plot([1,2,3,4], color='b', label='Total cost')

        # initialize legend
        self.ax_best.legend()
        self.ax_curr.legend()

        # draw figure
        self.fig.canvas.draw()
        self.fig.canvas.flush_events()

        # show figure
        plt.show(block=False)

def update(self, best_sol, curr_sol): # best_sol and curr_sol is not used while debugging
    # update best solution
    self.best_cost.append(7)
    # update current solution
    self.curr_cost.append(7)

    # update lines
    self.best_line.set_ydata(self.best_cost)
    self.curr_line.set_ydata(self.curr_cost)


    # draw figure
    self.fig.canvas.draw()
    self.fig.canvas.flush_events()

Error: ValueError: shape mismatch: objects cannot be broadcast to a single shape. Mismatch is between arg 0 with shape (4,) and arg 1 with shape (2,).

The class is initialized correctly: enter image description here

but the update is not working. What am I missing?

0 Answers
Related