forked from JelteF/PyLaTeX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.py
142 lines (97 loc) · 3.61 KB
/
graphics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
"""
This module implements the class that deals with graphics.
.. :copyright: (c) 2014 by Jelte Fennema.
:license: MIT, see License for more details.
"""
import os.path
from .utils import fix_filename, make_temp_dir, _merge_packages_into_kwargs
from .base_classes import Command, Float
from .package import Package
import uuid
class Figure(Float):
"""A class that represents a Figure environment."""
def __init__(self, *args, **kwargs):
packages = [Package('graphicx')]
_merge_packages_into_kwargs(packages, kwargs)
super().__init__(*args, **kwargs)
def add_image(self, filename, width=r'0.8\textwidth',
placement=r'\centering'):
"""Add an image.to the figure.
:param filename:
:param width:
:param placement:
:type filename: str
:type width: str
:type placement: str
"""
if placement is not None:
self.append(placement)
if width is not None:
width = 'width=' + str(width)
self.append(Command('includegraphics', options=width,
arguments=fix_filename(filename)))
class SubFigure(Figure):
"""A class that represents a subfigure from the subcaption package.
:param data:
:param position:
:type data: list
:type position: str
:param data:
:param position:
:param seperate_paragraph:
:type data: list
:type position: str
:type seperate_paragraph: bool
"""
def __init__(self, data=None, position=None, width=r'0.45\linewidth',
seperate_paragraph=False, **kwargs):
packages = [Package('subcaption')]
super().__init__(data=data, packages=packages,
position=position,
argument=width,
seperate_paragraph=seperate_paragraph, **kwargs)
def add_image(self, filename, width=r'\linewidth',
placement=None):
"""Add an image to the subfigure.
:param filename:
:param width:
:param placement:
:type filename: str
:type width: str
:type placement: str
"""
super().add_image(filename, width=width, placement=placement)
class MatplotlibFigure(Figure):
"""A class that represents a plot created with matplotlib."""
# TODO: Make an equivalent class for subfigure plots
container_name = 'figure'
def __init__(self, *args, **kwargs):
import matplotlib.pyplot as plt
self._plt = plt
super().__init__(*args, **kwargs)
def _save_plot(self, *args, **kwargs):
"""Save the plot.
:param plt: The matplotlib.pyplot module
:type plt: matplotlib.pyplot
:return: The basename with which the plot has been saved.
:rtype: str
"""
tmp_path = make_temp_dir()
filename = os.path.join(tmp_path, str(uuid.uuid4()) + '.pdf')
self._plt.savefig(filename, *args, **kwargs)
return filename
def add_plot(self, width=r'0.8\textwidth',
placement=r'\centering', *args, **kwargs):
"""Add a plot.
:param plt: The matplotlib.pyplot module
:param width: The width of the plot.
:param placement: The placement of the plot.
:type plt: matplotlib.pyplot
:type width: str
:type placement: str
"""
# TODO: Make default width and placement linked to the figure class
# TODO: Add args and kwargs explanation
filename = self._save_plot(*args, **kwargs)
self.add_image(filename, width, placement)