Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for adding figures via figure handles #387

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions pylatex/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def add_image(
filename,
*,
width=NoEscape(r"0.8\textwidth"),
placement=NoEscape(r"\centering")
placement=NoEscape(r"\centering"),
):
"""Add an image to the figure.

Expand Down Expand Up @@ -50,7 +50,7 @@ def add_image(
StandAloneGraphic(image_options=width, filename=fix_filename(filename))
)

def _save_plot(self, *args, extension="pdf", **kwargs):
def _save_plot(self, *args, figure=None, extension="pdf", **kwargs):
"""Save the plot.

Returns
Expand All @@ -64,11 +64,12 @@ def _save_plot(self, *args, extension="pdf", **kwargs):
filename = "{}.{}".format(str(uuid.uuid4()), extension.strip("."))
filepath = posixpath.join(tmp_path, filename)

plt.savefig(filepath, *args, **kwargs)
fig = figure or plt.gcf()
fig.savefig(filepath, *args, **kwargs)
return filepath

def add_plot(self, *args, extension="pdf", **kwargs):
"""Add the current Matplotlib plot to the figure.
def add_plot(self, *args, figure=None, extension="pdf", **kwargs):
"""Add a Matplotlib plot to the figure.

The plot that gets added is the one that would normally be shown when
using ``plt.show()``.
Expand All @@ -77,6 +78,8 @@ def add_plot(self, *args, extension="pdf", **kwargs):
----
args:
Arguments passed to plt.savefig for displaying the plot.
figure:
Optional matplotlib figure. If None add the current figure.
extension : str
extension of image file indicating figure file type
kwargs:
Expand All @@ -92,7 +95,7 @@ def add_plot(self, *args, extension="pdf", **kwargs):
if key in kwargs:
add_image_kwargs[key] = kwargs.pop(key)

filename = self._save_plot(*args, extension=extension, **kwargs)
filename = self._save_plot(*args, figure=figure, extension=extension, **kwargs)

self.add_image(filename, **add_image_kwargs)

Expand Down
27 changes: 24 additions & 3 deletions tests/test_pictures.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,37 @@

from pylatex import Document, Section
from pylatex.figure import Figure
import matplotlib.pyplot as plt


def test():
def test_add_image():
doc = Document()
section = Section("Multirow Test")
section = Section("Add image Test")
figure = Figure()
image_filename = os.path.join(os.path.dirname(__file__), "../examples/kitten.jpg")
figure.add_image(image_filename)
figure.add_caption("Whoooo an imagage of a pdf")
figure.add_caption("Whoooo an image of a kitty")
section.append(figure)
doc.append(section)

doc.generate_pdf()


def test_add_plot():
doc = Document()
section = Section("Add plot Test")
mplfig = plt.figure()

figure = Figure()
figure.add_plot()
figure.add_caption("Whoooo current matplotlib fig")
section.append(figure)

figure = Figure()
figure.add_plot(figure=mplfig)
figure.add_caption("Whoooo image from figure handle")
section.append(figure)

doc.append(section)

doc.generate_pdf()
Loading