I'm trying to create a horizontal bar plot that can dynamically change with a slider. I've followed the recipe on the matplotib website and it works well for line
data.
The code:
def interactive_pyramid(ages_matrix, year_labels):
fig, axs = plt.subplots()
plt.subplots_adjust(bottom=0.25)
l, = axs.plot(range(len(ages_matrix[0, :])), ages_matrix[0, :])
axs.annotate(year_labels[0], xy=(0.85, 0.85), xycoords="axes fraction")
axs.set_ylim([0,800])
pprint (dir(axs))
axcolor = 'lightgoldenrodyellow'
axyear = plt.axes([0.25, 0.1, 0.5, 0.1], axisbg=axcolor)
syear = Slider(axyear, 'Year', 0, ages_matrix.shape[0] - 1, 0)
def update(val):
year = syear.val
# axs.barh(range(len(ages_matrix[0, :])), ages_matrix[val, :])
l.set_ydata(ages_matrix[val, :])
# axs.annotate(year_labels[year], xy=(0.85, 0.85), xycoords="axes fraction")
axs.Annotation.remove()
fig.canvas.draw()
syear.on_changed(update)
ages_matrix is a 2d ndarray
and year_labels is a 1d ndarray
two main questions:
- the
axs.barh()
does not return an object with aset_ydata()
method do I can't change the y data. if I just draw the data again on the axs object it doesn't erase the previous information, resulting in a clutter of charts. - the same happens with annotations - it does not erase the previous one.
Is there any way to efficiently erase the ax and draw it again? maybe some way to refresh the canvas?