Skip to content

Commit 07763c3

Browse files
committed
Added ability to plot single layers
1 parent 8023dd5 commit 07763c3

File tree

7 files changed

+167
-82
lines changed

7 files changed

+167
-82
lines changed

src/maxplotlib/canvas/canvas.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ def __init__(self, **kwargs):
3838

3939
self._subplot_matrix = [[None] * self.ncols for _ in range(self.nrows)]
4040

41+
@property
42+
def layers(self):
43+
layers = []
44+
for (row, col), subplot in self.subplots.items():
45+
layers.extend(subplot.layers)
46+
return list(set(layers))
4147
def generate_new_rowcol(self, row, col):
4248
if row is None:
4349
for irow in range(self.nrows):
@@ -109,20 +115,32 @@ def add_subplot(self, **kwargs):
109115
self.subplots[label] = line_plot
110116
return line_plot
111117

112-
def savefig(self, filename, backend="matplotlib"):
118+
def savefig(self, filename, extension='pdf', backend="matplotlib", layers = None, layer_by_layer=False):
113119
if backend == "matplotlib":
114-
fig, axs = self.plot(show=False, backend="matplotlib", savefig=True)
115-
fig.savefig(filename)
120+
if layer_by_layer:
121+
layers = []
122+
for layer in self.layers:
123+
layers.append(layer)
124+
fig, axs = self.plot(show=False, backend="matplotlib", savefig=True, layers=layers)
125+
fig.savefig(f"{filename}_{layers}.{extension}")
126+
else:
127+
if layers is None:
128+
layers = self.layers
129+
full_filepath = f"{filename}.{extension}"
130+
else:
131+
full_filepath = f"{filename}_{layers}.{extension}"
132+
fig, axs = self.plot(show=False, backend="matplotlib", savefig=True, layers=layers)
133+
fig.savefig(full_filepath)
116134

117135
# def add_line(self, label, x_data, y_data, **kwargs):
118136

119-
def plot(self, backend="matplotlib", show=True, savefig=False):
137+
def plot(self, backend="matplotlib", show=True, savefig=False, layers=None):
120138
if backend == "matplotlib":
121-
return self.plot_matplotlib(show=show, savefig=savefig)
139+
return self.plot_matplotlib(show=show, savefig=savefig, layers=layers)
122140
elif backend == "plotly":
123141
self.plot_plotly(show=show, savefig=savefig)
124142

125-
def plot_matplotlib(self, show=True, savefig=False):
143+
def plot_matplotlib(self, show=True, savefig=False, layers=None):
126144
"""
127145
Generate and optionally display the subplots.
128146
@@ -134,7 +152,7 @@ def plot_matplotlib(self, show=True, savefig=False):
134152
tex_fonts = plt_utils.setup_tex_fonts(fontsize=fontsize)
135153
plt_utils.setup_plotstyle(
136154
tex_fonts=tex_fonts,
137-
axes_grid=False,
155+
axes_grid=True,
138156
axes_grid_which="major",
139157
grid_alpha=1.0,
140158
grid_linestyle="dotted",
@@ -157,16 +175,16 @@ def plot_matplotlib(self, show=True, savefig=False):
157175

158176
for (row, col), subplot in self.subplots.items():
159177
ax = axes[row][col]
160-
subplot.plot_matplotlib(ax)
178+
subplot.plot_matplotlib(ax, layers=layers)
161179
# ax.set_title(f"Subplot ({row}, {col})")
162-
180+
ax.grid()
163181
# Set caption, labels, etc., if needed
164182
plt.tight_layout()
165183

166184
if show:
167185
plt.show()
168-
else:
169-
plt.close()
186+
# else:
187+
# plt.close()
170188
return fig, axes
171189

172190
def plot_plotly(self, show=True, savefig=None):

src/maxplotlib/subfigure/line_plot.py

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __init__(self, **kwargs):
2828
self._ylabel = kwargs.get("ylabel", None)
2929
# List to store line data, each entry contains x and y data, label, and plot kwargs
3030
self.line_data = []
31+
self.layered_line_data = {}
3132

3233
# Scaling
3334
self._xscale = kwargs.get("xscale", 1.0)
@@ -38,7 +39,7 @@ def __init__(self, **kwargs):
3839
def add_caption(self, caption):
3940
self._caption = caption
4041

41-
def add_line(self, x_data, y_data, **kwargs):
42+
def add_line(self, x_data, y_data, layer = 0, **kwargs):
4243
"""
4344
Add a line to the plot.
4445
@@ -48,35 +49,47 @@ def add_line(self, x_data, y_data, **kwargs):
4849
y_data (list): Y-axis data.
4950
**kwargs: Additional keyword arguments for the plot (e.g., color, linestyle).
5051
"""
51-
self.line_data.append(
52-
{"x": np.array(x_data), "y": np.array(y_data), "kwargs": kwargs}
53-
)
52+
ld = {"x": np.array(x_data), "y": np.array(y_data), "layer": layer, "kwargs": kwargs}
53+
self.line_data.append(ld)
54+
if layer in self.layered_line_data:
55+
self.layered_line_data[layer].append(ld)
56+
else:
57+
self.layered_line_data[layer] = [ld]
5458

55-
def plot_matplotlib(self, ax):
59+
@property
60+
def layers(self):
61+
layers = []
62+
for layer_name, layer_lines in self.layered_line_data.items():
63+
layers.append(layer_name)
64+
return layers
65+
def plot_matplotlib(self, ax, layers=None):
5666
"""
5767
Plot all lines on the provided axis.
5868
5969
Parameters:
6070
ax (matplotlib.axes.Axes): Axis on which to plot the lines.
6171
"""
62-
for line in self.line_data:
63-
ax.plot(
64-
(line["x"] + self._xshift) * self._xscale,
65-
(line["y"] + self._yshift) * self._yscale,
66-
**line["kwargs"],
67-
)
68-
if self._caption:
69-
ax.set_title(self._caption)
70-
if self._label:
71-
ax.set_ylabel(self._label)
72-
if self._xlabel:
73-
ax.set_xlabel(self._xlabel)
74-
if self._ylabel:
75-
ax.set_ylabel(self._ylabel)
76-
if self._legend and len(self.line_data) > 0:
77-
ax.legend()
78-
if self._grid:
79-
ax.grid()
72+
for layer_name, layer_lines in self.layered_line_data.items():
73+
if layers and layer_name not in layers:
74+
continue
75+
for line in layer_lines:
76+
ax.plot(
77+
(line["x"] + self._xshift) * self._xscale,
78+
(line["y"] + self._yshift) * self._yscale,
79+
**line["kwargs"],
80+
)
81+
if self._caption:
82+
ax.set_title(self._caption)
83+
if self._label:
84+
ax.set_ylabel(self._label)
85+
if self._xlabel:
86+
ax.set_xlabel(self._xlabel)
87+
if self._ylabel:
88+
ax.set_ylabel(self._ylabel)
89+
if self._legend and len(self.line_data) > 0:
90+
ax.legend()
91+
if self._grid:
92+
ax.grid()
8093

8194
def plot_plotly(self):
8295
"""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Subfigure:
2+
def __init__(self, **kwargs):
3+
self.kwargs = kwargs

src/maxplotlib/subfigure/tikz_figure.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from maxplotlib.linestyle.linestyle import Linestyle
1212

1313

14-
class Layer:
14+
class Tikzlayer:
1515
def __init__(self, label):
1616
self.label = label
1717
self.items = []
@@ -102,7 +102,7 @@ def to_tikz(self):
102102
options = ", ".join(self.path_actions) + ", " + options
103103
if options:
104104
options = f"[{options}]"
105-
path_str = " -- ".join(f"({node.label}.center)" for node in self.nodes)
105+
path_str = " to ".join(f"({node.label}.center)" for node in self.nodes)
106106
if self.cycle:
107107
path_str += " -- cycle"
108108
return f"\\draw{options} {path_str};\n"
@@ -157,7 +157,7 @@ def add_node(self, x, y, label=None, content="", layer=0, **kwargs):
157157
if layer in self.layers:
158158
self.layers[layer].add(node)
159159
else:
160-
self.layers[layer] = Layer(layer)
160+
self.layers[layer] = Tikzlayer(layer)
161161
self.layers[layer].add(node)
162162
self._node_counter += 1
163163
return node
@@ -194,7 +194,7 @@ def add_path(self, nodes, layer=0, **kwargs):
194194
if layer in self.layers:
195195
self.layers[layer].add(path)
196196
else:
197-
self.layers[layer] = Layer(layer)
197+
self.layers[layer] = Tikzlayer(layer)
198198
self.layers[layer].add(path)
199199
return path
200200

0 commit comments

Comments
 (0)