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!

2
  • you want the plots to be scrollable in a pdf? don't think that is possible Commented Nov 18, 2015 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? Commented Nov 18, 2015 at 16:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
Try and check this answer stackoverflow.com/questions/12179893/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.