Skip to content

Commit d944768

Browse files
committed
Update plot directly from canvas
1 parent ea4234b commit d944768

File tree

4 files changed

+73
-13
lines changed

4 files changed

+73
-13
lines changed

src/maxplotlib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from maxplotlib.canvas.canvas import Canvas
22

3-
__all__ = ["Canvas"]
3+
__all__ = ["Canvas"]

src/maxplotlib/canvas/canvas.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from plotly.subplots import make_subplots
66

77
import maxplotlib.backends.matplotlib.utils as plt_utils
8-
import maxplotlib.subfigure.line_plot as lp
9-
import maxplotlib.subfigure.tikz_figure as tf
8+
from maxplotlib.subfigure.line_plot import LinePlot
9+
from maxplotlib.subfigure.tikz_figure import TikzFigure
1010

1111

1212
class Canvas:
@@ -70,6 +70,38 @@ def generate_new_rowcol(self, row, col):
7070
assert col is not None, "Not enough columns!"
7171
return row, col
7272

73+
def add_line(
74+
self,
75+
x_data,
76+
y_data,
77+
layer=0,
78+
subplot: LinePlot | None = None,
79+
row: int | None = None,
80+
col: int | None = None,
81+
plot_type="plot",
82+
**kwargs,
83+
):
84+
if row is not None and col is not None:
85+
try:
86+
subplot = self._subplot_matrix[row][col]
87+
except KeyError:
88+
raise ValueError("Invalid subplot position.")
89+
else:
90+
row, col = 0, 0
91+
subplot = self._subplot_matrix[row][col]
92+
93+
if subplot is None:
94+
row, col = self.generate_new_rowcol(row, col)
95+
subplot = self.add_subplot(col=col, row=row)
96+
97+
subplot.add_line(
98+
x_data=x_data,
99+
y_data=y_data,
100+
layer=layer,
101+
plot_type=plot_type,
102+
**kwargs,
103+
)
104+
73105
def add_tikzfigure(self, **kwargs):
74106
"""
75107
Adds a subplot to the figure.
@@ -87,7 +119,7 @@ def add_tikzfigure(self, **kwargs):
87119
row, col = self.generate_new_rowcol(row, col)
88120

89121
# Initialize the LinePlot for the given subplot position
90-
tikz_figure = tf.TikzFigure(**kwargs)
122+
tikz_figure = TikzFigure(**kwargs)
91123
self._subplot_matrix[row][col] = tikz_figure
92124

93125
# Store the LinePlot instance by its position for easy access
@@ -97,7 +129,13 @@ def add_tikzfigure(self, **kwargs):
97129
self._subplots[label] = tikz_figure
98130
return tikz_figure
99131

100-
def add_subplot(self, col: int | None = None, row: int | None = None, label: str | None = None, **kwargs):
132+
def add_subplot(
133+
self,
134+
col: int | None = None,
135+
row: int | None = None,
136+
label: str | None = None,
137+
**kwargs,
138+
):
101139
"""
102140
Adds a subplot to the figure.
103141
@@ -108,11 +146,10 @@ def add_subplot(self, col: int | None = None, row: int | None = None, label: str
108146
- label (str): Label to identify the subplot.
109147
"""
110148

111-
112149
row, col = self.generate_new_rowcol(row, col)
113150

114151
# Initialize the LinePlot for the given subplot position
115-
line_plot = lp.LinePlot(col=col, row=row, label=label, **kwargs)
152+
line_plot = LinePlot(col=col, row=row, label=label, **kwargs)
116153
self._subplot_matrix[row][col] = line_plot
117154

118155
# Store the LinePlot instance by its position for easy access
@@ -171,10 +208,8 @@ def plot(self, backend="matplotlib", savefig=False, layers=None):
171208

172209
def show(self, backend="matplotlib"):
173210
if backend == "matplotlib":
174-
fig, axs = self.plot(backend="matplotlib", savefig=False, layers=None)
175-
print('hmm')
211+
self.plot(backend="matplotlib", savefig=False, layers=None)
176212
self._matplotlib_fig.show()
177-
plt.show()
178213
elif backend == "plotly":
179214
plot = self.plot_plotly(savefig=False)
180215
else:

src/maxplotlib/subfigure/line_plot.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ def _add(self, obj, layer):
8686
else:
8787
self.layered_line_data[layer] = [obj]
8888

89-
def add_line(self, x_data, y_data, layer=0, plot_type="plot", **kwargs):
89+
def add_line(
90+
self,
91+
x_data,
92+
y_data,
93+
layer=0,
94+
plot_type="plot",
95+
**kwargs,
96+
):
9097
"""
9198
Add a line to the plot.
9299

tutorials/tutorial_01.ipynb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,26 @@
2929
"metadata": {},
3030
"outputs": [],
3131
"source": [
32+
"c = Canvas(width=\"17cm\", ratio=0.5, fontsize=12)\n",
33+
"c.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\")\n",
34+
"c.add_line([0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\")\n",
35+
"c.show()"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"id": "3",
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"# You can also explicitly create a subplot and add lines to it\n",
46+
"\n",
3247
"c = Canvas(width=\"17cm\", ratio=0.5, fontsize=12)\n",
3348
"sp = c.add_subplot(\n",
3449
" grid=True, xlabel=\"(x - 10) * 0.1\", ylabel=\"10y\", yscale=10, xshift=-10, xscale=0.1\n",
3550
")\n",
51+
"\n",
3652
"sp.add_line([0, 1, 2, 3], [0, 1, 4, 9], label=\"Line 1\")\n",
3753
"sp.add_line([0, 1, 2, 3], [0, 2, 3, 4], linestyle=\"dashed\", color=\"red\", label=\"Line 2\")\n",
3854
"c.show()"
@@ -41,10 +57,12 @@
4157
{
4258
"cell_type": "code",
4359
"execution_count": null,
44-
"id": "3",
60+
"id": "4",
4561
"metadata": {},
4662
"outputs": [],
4763
"source": [
64+
"# Example with multiple subplots\n",
65+
"\n",
4866
"c = Canvas(width=\"17cm\", ncols=2, nrows=2, ratio=0.5)\n",
4967
"sp = c.add_subplot(grid=True)\n",
5068
"c.add_subplot(row=1)\n",
@@ -59,7 +77,7 @@
5977
{
6078
"cell_type": "code",
6179
"execution_count": null,
62-
"id": "4",
80+
"id": "5",
6381
"metadata": {},
6482
"outputs": [],
6583
"source": [

0 commit comments

Comments
 (0)