Zed
Zed

Reputation: 121

Add overlapping plots to a pdf multipage

I want to add overlapping plots to one subplot of a pdf page with the data: x1,x2,y1,y2 etc. So far this is my current state but it is not working clearly yet to display the subplots in the pdf:

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

mp = PdfPages('multipage.pdf')

plt.grid()
plt.scatter(x1,y1)
plt.step([0,0],[-5,5], linewidth = 3,color ='orange')
plt.step([50,50],[45,55], linewidth = 3,color ='orange')
plt.step([100,100],[95,105], linewidth = 3,color ='orange')
plt.xlabel('x')
plt.ylabel('y')

fig = plt.figure()
ax1 = fig.add_subplot(211)

plt.show()

plt.grid()
plt.scatter(x2,y2)
plt.step([0,0],[-5,5], linewidth = 3,color ='orange')
plt.step([50,50],[45,55], linewidth = 3,color ='orange')
plt.step([100,100],[95,105], linewidth = 3,color ='orange')
plt.xlabel('x')
plt.ylabel('y')

fig = plt.figure()
ax2=fig.add_subplot(212)

plt.show()

mp.savefig(fig)

mp.close()

Upvotes: 0

Views: 93

Answers (1)

William Miller
William Miller

Reputation: 10320

I'm not sure if this is what you're looking for, but perhaps it will offer some help. This is an adaptation of the example referenced in your comment to use PdfPages:


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

x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

fig, axs = plt.subplots(2, 2)
axs[0, 0].plot(x, y)
axs[0, 1].plot(x, y, 'tab:orange')
axs[1, 0].plot(x, -y, 'tab:green')
axs[1, 1].plot(x, -y, 'tab:red')

mp = PdfPages('multipage.pdf')

mp.savefig(fig)
mp.close()

Upvotes: 1

Related Questions