Skip to content

Commit eba40f1

Browse files
headtr1ckJelteF
authored andcommitted
support for adding figures via figure handles
1 parent f40c55b commit eba40f1

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

pylatex/figure.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def add_image(
2222
filename,
2323
*,
2424
width=NoEscape(r"0.8\textwidth"),
25-
placement=NoEscape(r"\centering")
25+
placement=NoEscape(r"\centering"),
2626
):
2727
"""Add an image to the figure.
2828
@@ -50,7 +50,7 @@ def add_image(
5050
StandAloneGraphic(image_options=width, filename=fix_filename(filename))
5151
)
5252

53-
def _save_plot(self, *args, extension="pdf", **kwargs):
53+
def _save_plot(self, *args, figure=None, extension="pdf", **kwargs):
5454
"""Save the plot.
5555
5656
Returns
@@ -64,11 +64,12 @@ def _save_plot(self, *args, extension="pdf", **kwargs):
6464
filename = "{}.{}".format(str(uuid.uuid4()), extension.strip("."))
6565
filepath = posixpath.join(tmp_path, filename)
6666

67-
plt.savefig(filepath, *args, **kwargs)
67+
fig = figure or plt.gcf()
68+
fig.savefig(filepath, *args, **kwargs)
6869
return filepath
6970

70-
def add_plot(self, *args, extension="pdf", **kwargs):
71-
"""Add the current Matplotlib plot to the figure.
71+
def add_plot(self, *args, figure=None, extension="pdf", **kwargs):
72+
"""Add a Matplotlib plot to the figure.
7273
7374
The plot that gets added is the one that would normally be shown when
7475
using ``plt.show()``.
@@ -77,6 +78,8 @@ def add_plot(self, *args, extension="pdf", **kwargs):
7778
----
7879
args:
7980
Arguments passed to plt.savefig for displaying the plot.
81+
figure:
82+
Optional matplotlib figure. If None add the current figure.
8083
extension : str
8184
extension of image file indicating figure file type
8285
kwargs:
@@ -92,7 +95,7 @@ def add_plot(self, *args, extension="pdf", **kwargs):
9295
if key in kwargs:
9396
add_image_kwargs[key] = kwargs.pop(key)
9497

95-
filename = self._save_plot(*args, extension=extension, **kwargs)
98+
filename = self._save_plot(*args, figure=figure, extension=extension, **kwargs)
9699

97100
self.add_image(filename, **add_image_kwargs)
98101

tests/test_pictures.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,37 @@
44

55
from pylatex import Document, Section
66
from pylatex.figure import Figure
7+
import matplotlib.pyplot as plt
78

89

9-
def test():
10+
def test_add_image():
1011
doc = Document()
11-
section = Section("Multirow Test")
12+
section = Section("Add image Test")
1213
figure = Figure()
1314
image_filename = os.path.join(os.path.dirname(__file__), "../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)