Search code examples
pythonpython-2.7matplotlibmatplotlib-widget

Matplotlib multicursor.py for loop-generated subplots


I'm trying to use a multicursor in matplotlib as in the example here. The problem is that my subplots are loop-generated, which means I don't have an ax1, ax2,... But a code is worth a thousands words :

t = 0
fig = plt.figure()
while t < 16 :
     ax = fig.add_subplot(4,4,t+1)
     p1 = plot(...)
     p2 = plot(...)
     p3 = plot(...)
     p4 = plot(...)
     t = t+1
show()

Does anyone have an idea ? Thanks !


Solution

  • Why not make a list of axes and pass this to the multicursor?

    t = 0
    fig = plt.figure()
    axes_list = []
    while t < 16 :
         ax = fig.add_subplot(4,4,t+1)
         axes_list.append(ax)
         p1 = plot(...)
         p2 = plot(...)
         p3 = plot(...)
         p4 = plot(...)
         t = t+1
    multi = MultiCursor(fig.canvas, axes_list, color='r', lw=1)
    show()