Skip to content

Commit db63f98

Browse files
committed
support for adding figures via figure handles
1 parent 1a73261 commit db63f98

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

pylatex/figure.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def add_image(self, filename, *, width=NoEscape(r'0.8\textwidth'),
4444
self.append(StandAloneGraphic(image_options=width,
4545
filename=fix_filename(filename)))
4646

47-
def _save_plot(self, *args, extension='pdf', **kwargs):
47+
def _save_plot(self, *args, figure=None, extension='pdf', **kwargs):
4848
"""Save the plot.
4949
5050
Returns
@@ -58,11 +58,12 @@ def _save_plot(self, *args, extension='pdf', **kwargs):
5858
filename = '{}.{}'.format(str(uuid.uuid4()), extension.strip('.'))
5959
filepath = posixpath.join(tmp_path, filename)
6060

61-
plt.savefig(filepath, *args, **kwargs)
61+
fig = figure or plt.gcf()
62+
fig.savefig(filepath, *args, **kwargs)
6263
return filepath
6364

64-
def add_plot(self, *args, extension='pdf', **kwargs):
65-
"""Add the current Matplotlib plot to the figure.
65+
def add_plot(self, *args, figure=None, extension='pdf', **kwargs):
66+
"""Add a Matplotlib plot to the figure.
6667
6768
The plot that gets added is the one that would normally be shown when
6869
using ``plt.show()``.
@@ -71,6 +72,8 @@ def add_plot(self, *args, extension='pdf', **kwargs):
7172
----
7273
args:
7374
Arguments passed to plt.savefig for displaying the plot.
75+
figure:
76+
Optional matplotlib figure. If None add the current figure.
7477
extension : str
7578
extension of image file indicating figure file type
7679
kwargs:
@@ -86,7 +89,9 @@ def add_plot(self, *args, extension='pdf', **kwargs):
8689
if key in kwargs:
8790
add_image_kwargs[key] = kwargs.pop(key)
8891

89-
filename = self._save_plot(*args, extension=extension, **kwargs)
92+
filename = self._save_plot(
93+
*args, figure=figure, extension=extension, **kwargs
94+
)
9095

9196
self.add_image(filename, **add_image_kwargs)
9297

tests/test_pictures.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,38 @@
33
from pylatex import Document, Section
44
from pylatex.figure import Figure
55
import os
6+
import matplotlib.pyplot as plt
67

78

8-
def test():
9+
def test_add_image():
910
doc = Document()
10-
section = Section('Multirow Test')
11+
section = Section('Add image Test')
1112
figure = Figure()
1213
image_filename = os.path.join(os.path.dirname(__file__),
1314
'../examples/kitten.jpg')
1415
figure.add_image(image_filename)
15-
figure.add_caption('Whoooo an imagage of a pdf')
16+
figure.add_caption('Whoooo an image of a kitty')
1617
section.append(figure)
1718
doc.append(section)
1819

1920
doc.generate_pdf()
21+
22+
23+
def test_add_plot():
24+
doc = Document()
25+
section = Section('Add plot Test')
26+
mplfig = plt.figure()
27+
28+
figure = Figure()
29+
figure.add_plot()
30+
figure.add_caption('Whoooo current matplotlib fig')
31+
section.append(figure)
32+
33+
figure = Figure()
34+
figure.add_plot(figure=mplfig)
35+
figure.add_caption('Whoooo image from figure handle')
36+
section.append(figure)
37+
38+
doc.append(section)
39+
40+
doc.generate_pdf()

0 commit comments

Comments
 (0)