5

I have a large amount of plots and wish to draw them in the same figure using python. I'm currently using pylab for the plots, but since there are too many they are drawn one on top of the other. Is there a way to make the figure scrollable, such that graphs are large enough and still visible by using scrollbar?

I can use PyQT for this, but there might be a feature of pylab's figure object that I'm missing...

White Zebra
  • 309
  • 1
  • 5
  • 15
  • Are your curves already offset from each other? If not, you could try just adding an offset (`plot(x,y+offset)`) to each curve to stack them and end up with a tall, narrow graph. The figure window gui has a zoom and pan tools which might be able to do what you want in conjunction with the offset. – tacaswell Aug 29 '12 at 18:12
  • I want to be able to see the graphs in their proper scale and position, for comparison and analysis. – White Zebra Aug 30 '12 at 07:08
  • Can you explain in greater detail how you want the scrolling to work? If the data is already overlapping, and you don't want to shift it, I don't see how scrolling the window will help. – tacaswell Aug 30 '12 at 15:56
  • I have ~100 plots and I want to be able to put those plots in the same **window** and scroll between the plots. Each plot has different scales, so shifting the data won't help + since there is lots of data it will be impossible to understand anything if I shift it. – White Zebra Sep 02 '12 at 07:45
  • ah! Sorry, I was mis-reading your question. – tacaswell Sep 02 '12 at 16:16

1 Answers1

7

This matches the spirit of what you want, if not the letter. I think you want a window with a number of axes and then be able to scroll through the axes (but still only be able to see on average one at a time), the solution has a single axes and a slider that selects which data set to plot.

import numpy
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
# fake data
xdata = numpy.random.rand(100,100) 
ydata = numpy.random.rand(100,100) 
# set up figure
fig = plt.figure()
ax = fig.add_subplot(111)
ax.autoscale(True)
plt.subplots_adjust(left=0.25, bottom=0.25)

# plot first data set
frame = 0
ln, = ax.plot(xdata[frame],ydata[frame])

# make the slider
axframe = plt.axes([0.25, 0.1, 0.65, 0.03])
sframe = Slider(axframe, 'Frame', 0, 99, valinit=0,valfmt='%d')

# call back function
def update(val):
    frame = int(round(numpy.floor(sframe.val)))
    ln.set_xdata(xdata[frame])
    ln.set_ydata((frame+1)* ydata[frame])
    ax.set_title(frame)
    ax.relim()
    ax.autoscale_view()
    plt.draw()

# connect callback to slider   
sframe.on_changed(update)
plt.show()

This is adapted from the code in this question. You can add next/back buttons using the button widgets (doc).

steinmig
  • 45
  • 5
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • Nice! You probably meant to use "plt.axes" and "plt.show" instead of pylab. – White Zebra Sep 03 '12 at 11:52
  • I would like to be able to look at more than one graph at a time, but it's still a good enough solution for me. – White Zebra Sep 03 '12 at 11:57
  • yup, sorry about the typos. If you want to see more than one graph at once, maybe try using multiple axes both tied to the same slider? – tacaswell Sep 03 '12 at 15:14
  • I ended up using PyQt after all (it wasn't easy though). I used the code from [here](http://stackoverflow.com/questions/12234917/show-several-plots-in-a-scrollable-widget-with-pyqt-and-matplotlib/12239663#12239663), but I'll definitely try to use the slider you suggested in the future. It makes the app look so much better! – White Zebra Sep 03 '12 at 16:23
  • @tcaswell, would this approach work for frames which include fill_between() and vlines() or is this only suitable for plot() ? – themachinist Nov 19 '15 at 23:09
  • Thanks @tcaswell, could you pls recommend a link for reading up on how to do this ? – themachinist Nov 20 '15 at 00:23
  • Either the api docs or just play with love objects in IPython. Tab complete is a great way to explore apis. – tacaswell Nov 20 '15 at 00:24
  • @tcaswell I've now submitted a question on this topic. http://stackoverflow.com/questions/33828260/how-to-update-artists-in-scrollable-matplotlib-multiplot – themachinist Nov 21 '15 at 20:49