1

I would like to create a pdf file [by using plt.savefig("~~~.pdf")] containing lots of (about 20) subplots each of which is drawing timeseries data. I am using a matplotlib library with python language.

Each subplot may be long, and I want to put the subplots horizontally. Therefore, the figure should be very long (horizontally), so the horizontal scroll bar should be needed!

Is there any way to do this? some example code will be appreciated!

The following is my sample code. I just wanted to draw 10 sine graphs arranged horizontally and save it as pdf file. (but I'm not pretty good at this. so the code may looks to be weird to you.. :( )

from matplotlib import pyplot as plt
import numpy as np

x=np.linspace(0,100,1000)
y=np.sin(x)

numplots=10
nr=1
nc=numplots
size_x=nc*50
size_y=size_x*3/4

fig=plt.figure(1,figsize=(size_x,size_y))
for i in range(nc):
    ctr=i+1
    ax=fig.add_subplot(nr,nc,ctr)

    ax.plot(x,y)

plt.savefig("longplot.pdf")
plt.clf()

Thank you!

Minsoo Choy
  • 115
  • 3
  • 8
  • you want the plots to be scrollable in a pdf? don't think that is possible – nluigi Nov 18 '15 at 15:48
  • yeah, exactly.. Ah.. is it impossible? then may it be possible to draw a single very long figure (not subplots) with horizontal scroll in a pdf? – Minsoo Choy Nov 18 '15 at 16:16

2 Answers2

1

You should do that using the backend "matplotlib.backends.backend_pdf". This enables you to save matplotlib graphs in pdf format. I have simplified your code a bit, here is a working example:

from matplotlib import pyplot as plt
import numpy as np
from matplotlib.backends.backend_pdf import PdfPages

x = np.linspace(0,100,1000)
y = np.sin(x)

nr = 10
nc = 1

for i in range(nr):    
    plt.subplot(nr, nc, i + 1)
    plt.plot(x, y)    

pdf = PdfPages('longplot.pdf')
pdf.savefig()

pdf.close()

I hope this helps.

toti08
  • 2,448
  • 5
  • 24
  • 36
  • 1
    Thank you for your help. a pdf file created shows 10 subplots. But each one is a little small (is shrunk to be fitted in a one page.) Is it possible to make each subplot larger and make a vertical scroll bar? – Minsoo Choy Nov 18 '15 at 16:14
  • Try and check this answer http://stackoverflow.com/questions/12179893/creating-a-scrollable-multiplot-with-pythons-pylab. – toti08 Nov 18 '15 at 16:33
0

In the link below there is a solution, which can help you, since it was helpful to me either. Scrollbar on Matplotlib showing page

But if you have many subplots, I am afraid your problem won't be solved. Since it will shrink each graph anyway. In that case it will be better to break your graphs into smaller and separate parts.