Skip to content Skip to sidebar Skip to footer

Generate Pdf From Images And Including Text [python]

I need to make a PDF from four graphic plots, that are supposed to be displayed in landscape orientation and in a 2x2 matrix (preferably, with controlable sizes) . These plots fol

Solution 1:

To start, adding titles to each subplot is pretty easy and can be done using set_title("Title") after defining the axes: (This example is taken from matplotlib's reference)

import matplotlib.pyplot as plt
import numpy as np
# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Title One')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Title Two')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Title Three')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Title Four')
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)

To add the description below each subplot we have to do some tricky code, first, we need to add space below each subplot using subplots_adjust:

# Adjusting height between subplots and adding bottom space
plt.subplots_adjust(hspace = .4, bottom = .2)

To write the texts, text(x,y,s), notice we need each x, y coordinate, and to get them, the best I can think of is to get each axarr's coords by getting it's bbox's x0 and y0 parameters, so, something like this:

x0, y0 = axarr[0,0].get_position().x0, axarr[0,0].get_position().y0
f.text(x0,y0,description[0])

x0, y0 are just points of references, notice this code will plot the texts inside the main subplot's figure itself:enter image description here

However, even though this is not a general solution, in this case it seems offsetting y0 by .05 give us a pretty decent result: enter image description here

Full code:

import matplotlib.pyplot as plt
import numpy as np

# List of subplot's description
description = ['''Lorem ipsum dolor sit amet, consectetur adipisicing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.''','''
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
    nisi ut aliquip ex ea commodo consequat. ''', '''Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
    pariatur.''',''' Excepteur sint occaecat cupidatat non proident, sunt in
    culpa qui officia deserunt mollit anim id est laborum.''']

# Simple data to display in various forms
x = np.linspace(0, 2 * np.pi, 400)
y = np.sin(x ** 2)

# Four axes, returned as a 2-d array
f, axarr = plt.subplots(2, 2)
axarr[0, 0].plot(x, y)
axarr[0, 0].set_title('Title One')
axarr[0, 1].scatter(x, y)
axarr[0, 1].set_title('Title Two')
axarr[1, 0].plot(x, y ** 2)
axarr[1, 0].set_title('Title Three')
axarr[1, 1].scatter(x, y ** 2)
axarr[1, 1].set_title('Title Four')
# Fine-tune figure; hide x ticks for top plots and y ticks for right plots
plt.setp([a.get_xticklabels() for a in axarr[0, :]], visible=False)
plt.setp([a.get_yticklabels() for a in axarr[:, 1]], visible=False)
# Adjusting height between subplots and adding bottom space
plt.subplots_adjust(hspace = .4, bottom = .2)
# Print position and adding description manually
x0, y0 = axarr[0,0].get_position().x0, axarr[0,0].get_position().y0
f.text(x0,y0-.05,description[0])
x0, y0 = axarr[0,1].get_position().x0, axarr[0,1].get_position().y0
f.text(x0,y0-.05,description[1])
x0, y0 = axarr[1,0].get_position().x0, axarr[1,0].get_position().y0
f.text(x0,y0-.05,description[2])
x0, y0 = axarr[1,1].get_position().x0, axarr[1,1].get_position().y0
f.text(x0,y0-.05,description[3])

plt.show()

Post a Comment for "Generate Pdf From Images And Including Text [python]"