Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/tikzfigure/core/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ def to_tikz(self, output_unit: str | None = None) -> str:
if isinstance(self._grid, str):
axis_opts.append(f"grid={self._grid}")
else:
axis_opts.append(f"grid={'true' if self._grid else 'false'}")
axis_opts.append(f"grid={'major' if self._grid else 'none'}")

# Add width and height if specified
if self._width:
Expand Down
20 changes: 15 additions & 5 deletions src/tikzfigure/core/figure_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@


class FigureLayoutMixin:
GROUPPLOT_HORIZONTAL_SEP_CM = 1.5
GROUPPLOT_VERTICAL_SEP_CM = 2.0

_subfigure_rows: int | None
_subfigure_cols: int | None
_subfigure_position: int
Expand All @@ -25,10 +28,16 @@ def subfigure_axis(
grid: bool = True,
width: float = 0.45,
height: str | int | float | None = None,
axis_width: str | int | float | None = None,
comment: str | None = None,
**kwargs: Any,
) -> Axis2D:
"""Create a 2D axis for side-by-side subfigure layout."""
"""Create a 2D axis for side-by-side subfigure layout.

The ``width`` argument controls the layout fraction used for subfigure
placement. Use ``axis_width`` and ``height`` to set explicit pgfplots
dimensions on the rendered axis itself.
"""
if not (0 < width <= 1.0):
raise ValueError(
f"subfigure width must be in range (0.0, 1.0], got {width}"
Expand All @@ -40,6 +49,7 @@ def subfigure_axis(
xlim=xlim,
ylim=ylim,
grid=grid,
width=axis_width,
height=height,
label="",
comment=comment,
Expand Down Expand Up @@ -140,8 +150,8 @@ def _render_groupplot_grid(self, num_rows: int, num_cols: int) -> str:
group_opts = (
f"group style={{"
f"group size={num_cols} by {num_rows}, "
f"horizontal sep=1.5cm, "
f"vertical sep=2cm"
f"horizontal sep={self.GROUPPLOT_HORIZONTAL_SEP_CM}cm, "
f"vertical sep={self.GROUPPLOT_VERTICAL_SEP_CM}cm"
f"}}"
)
result = f"\\begin{{groupplot}}[{group_opts}]\n"
Expand All @@ -158,8 +168,8 @@ def _render_groupplot_grid(self, num_rows: int, num_cols: int) -> str:

def _render_mixed_grid(self, num_rows: int, num_cols: int) -> str:
textwidth_cm = 14.0
h_sep = 2.0
v_sep = 2.0
h_sep = self.GROUPPLOT_HORIZONTAL_SEP_CM
v_sep = self.GROUPPLOT_VERTICAL_SEP_CM
default_height = 6.0

from tikzfigure.core.figure import TikzFigure
Expand Down
7 changes: 6 additions & 1 deletion src/tikzfigure/core/figure_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@


class FigureRenderMixin:
GROUPPLOT_HORIZONTAL_SEP_CM: float
GROUPPLOT_VERTICAL_SEP_CM: float
_extra_packages: list[str] | None
_tikz_libraries: list[str]
_named_styles: list[dict[str, Any]]
Expand Down Expand Up @@ -286,7 +288,10 @@ def generate_tikz(
else:
num_axes = len(self._subfigure_axes)
group_opts = (
f"group style={{group size={num_axes} by 1, horizontal sep=1.5cm}}"
"group style={"
f"group size={num_axes} by 1, "
f"horizontal sep={self.GROUPPLOT_HORIZONTAL_SEP_CM}cm"
"}"
)
subfig_tikz += f"\\begin{{groupplot}}[{group_opts}]\n"
for item_tuple in self._subfigure_axes:
Expand Down
17 changes: 10 additions & 7 deletions src/tikzfigure/tests/test_axis2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,12 @@ def test_axis2d_to_tikz_with_grid(self):
axis = Axis2D(grid=True)
axis.add_plot([0, 1], [0, 1])
tikz_with_grid = axis.to_tikz()
assert "grid=true" in tikz_with_grid
assert "grid=major" in tikz_with_grid

axis2 = Axis2D(grid=False)
axis2.add_plot([0, 1], [0, 1])
tikz_no_grid = axis2.to_tikz()
assert "grid=false" in tikz_no_grid
assert "grid=none" in tikz_no_grid

def test_axis2d_to_tikz_with_log_axes(self):
axis = Axis2D(xlog=True, ylog=True)
Expand Down Expand Up @@ -844,9 +844,10 @@ class TestSubfigures:
def test_subfigure_axis_with_height(self):
"""Test that subfigure_axis() accepts height parameter."""
fig = TikzFigure()
ax1 = fig.subfigure_axis(xlabel="X", width=0.45, height=4)
ax1 = fig.subfigure_axis(xlabel="X", width=0.45, axis_width=3, height=4)
ax2 = fig.subfigure_axis(xlabel="Y", width=0.45, height=6)

assert ax1.width == "3cm"
assert ax1.height == "4cm"
assert ax2.height == "6cm"

Expand Down Expand Up @@ -971,15 +972,17 @@ def test_subfigure_axis_dimensions_in_output(self):
"""Test that subfigure dimensions appear correctly in TikZ output."""
fig = TikzFigure()

ax1 = fig.subfigure_axis(xlabel="Sin", width=0.45, height=4)
ax1 = fig.subfigure_axis(xlabel="Sin", width=0.45, axis_width=3, height=4)
ax1.add_plot(func="sin(x)", label="sin(x)")

ax2 = fig.subfigure_axis(xlabel="Cos", width=0.45, height=5)
ax2 = fig.subfigure_axis(xlabel="Cos", width=0.45, axis_width="5cm", height=5)
ax2.add_plot(func="cos(x)", label="cos(x)")

tikz = fig.generate_tikz()

# Verify both subfigures have their heights in output
# Verify both subfigures keep explicit dimensions in groupplot output.
assert "width=3cm" in tikz
assert "width=5cm" in tikz
assert "height=4cm" in tikz
assert "height=5cm" in tikz
assert "\\begin{groupplot}" in tikz
Expand Down Expand Up @@ -1103,7 +1106,7 @@ def test_full_workflow(self):
assert "xmax=10" in tikz
assert "ymin=-1" in tikz
assert "ymax=1" in tikz
assert "grid=true" in tikz
assert "grid=major" in tikz
assert "legend pos=north east" in tikz

# Verify plots
Expand Down
Loading