How to autoscale matplotlib X&Y axis after set_data() call

After calling set_data() or set_ydata() in matplotlib the axis scale is not updated automatically. Use

ax.relim()
ax.autoscale_view(True,True,True)

to update both the X and the Y scale.

Full example:

line.set_data(x, y)
# Autoscale view
ax.relim()
ax.autoscale_view(True,True,True)
# Redraw
figure.canvas.draw()
figure.canvas.flush_events()