Skip to content

Commit

Permalink
Added wspace and hspace (#10)
Browse files Browse the repository at this point in the history
Added hspace and wspace to init and add_page.
Test_7 tests the usage
  • Loading branch information
ebenp authored Nov 10, 2020
1 parent 171d781 commit 6f4d925
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 1 deletion.
17 changes: 17 additions & 0 deletions figpager/figpager.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ def __init__(
sharex=False,
sharey=False,
draft=True,
wspace=0.2,
hspace=0.2,
):

"""
Expand Down Expand Up @@ -129,6 +131,8 @@ def __init__(
sharex: share x axes in subplots. Default is False
sharey:share y axes in subplots. Default is False
draft: Add draft stamp if available from ini. Default is True
wspace: Add height spacing for subplots adjust. Default is 0.2
hspace: Add horizontal spacing for subplots adjust. Default is 0.2
"""

# obtain the caller path
Expand Down Expand Up @@ -229,6 +233,8 @@ def __init__(
self.sharex = sharex
self.sharey = sharey
self.draft = draft
self.wspace = wspace
self.hspace = hspace

# figure attributes
self.fig = None
Expand Down Expand Up @@ -1007,6 +1013,7 @@ def draw_page(self):
bottom=(self.bottommargin + self.marginpad) / self.pageheight_inch,
top=(self.frameheight + self.bottommargin - self.marginpad)
/ self.pageheight_inch,
hspace=self.hspace, wspace=self.wspace,
)

# This resets the margins and other parameters to layout
Expand Down Expand Up @@ -1158,6 +1165,8 @@ def add_page(
metadata=None,
subplotstartindex=None,
direction="left-to-right",
wspace=None,
hspace=None,
):

"""
Expand Down Expand Up @@ -1187,6 +1196,8 @@ def add_page(
subplotstartindex: (list of ints) (optional) First subplot on a page row and column index.
direction: (string) (optional) subplot creation direction. Default is Left-to-right.
gs: (optional) GridSpec specification for more advanced positions
wspace: Add height spacing for subplots adjust. Default is object's init
hspace: Add horizontal spacing for subplots adjust. Default is object's init
Returns: fig; figure instance, ax; axes instances, gs; GridSpec, self.transform; Figure Transform
Expand Down Expand Up @@ -1289,6 +1300,12 @@ def add_page(

self.direction = direction

if wspace is not None:
self.wspace = wspace

if hspace is not None:
self.hspace = hspace

if layout is not None:
self.layout = layout
self._read_layout(layout)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def read(fname):

setup(
name="figpager",
version="0.27",
version="0.28",
author="Eben Pendleton",
author_email="[email protected]",
url="https://github.com/ebenp/figpager",
Expand Down
Binary file modified tests/out.pdf
Binary file not shown.
Binary file modified tests/out_2.pdf
Binary file not shown.
Binary file modified tests/out_3.pdf
Binary file not shown.
Binary file modified tests/out_4.pdf
Binary file not shown.
Binary file modified tests/out_6.pdf
Binary file not shown.
Binary file added tests/out_7.pdf
Binary file not shown.
103 changes: 103 additions & 0 deletions tests/test_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Test for multipage hspace and vspace support

# Plots from: https://matplotlib.org/3.2.1/gallery/images_contours_and_fields/plot_streamplot.html#sphx-glr-gallery-images-contours-and-fields-plot-streamplot-py
import os

import numpy as np

from figpager import FigPager

def test_main():
# Initalize with a configuration that controls page margins
# and plot spacing
# Initalize with a page size and number of plots

# Initalize with an output file
outfile = "./tests/out_7.pdf"

# plots an image from http://www.metmuseum.org/art/collection/search/334348 CC0 1.0 Public Domain
fp = FigPager(
"letter",
3,
2,
layout="report",
outfile=outfile,
orientation="portrait",
height_ratios=[1, 1, 2],
overwrite=True,
transparent=False,
wspace=0.1,
hspace=0.1,
)

for r in range(2):
if r > 0:
fp.add_page(
nrows=3, ncols=2, orientation="portrait", height_ratios=[1, 1, 2], wspace=0.3, hspace=0.3,
)
w = 3
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
U = -1 - X ** 2 + Y
V = 1 + X - Y ** 2
speed = np.sqrt(U ** 2 + V ** 2)

ax0 = fp.add_subplot()
ax0.streamplot(X, Y, U, V, density=[0.5, 1])
ax0.set_title("Varying Density")

fp.text_from_label("Figure Title", "Figure 1")

# Varying color along a streamline
ax1 = fp.add_subplot()
strm = ax1.streamplot(X, Y, U, V, color=U, linewidth=2, cmap="autumn")
fp.fig.colorbar(strm.lines)
ax1.set_title("Varying Color")

# Varying line width along a streamline
ax2 = fp.add_subplot()
lw = 5 * speed / speed.max()
ax2.streamplot(X, Y, U, V, density=0.6, color="k", linewidth=lw)
ax2.set_title("Varying Line Width")

# Controlling the starting points of the streamlines
seed_points = np.array([[-2, -1, 0, 1, 2, -1], [-2, -1, 0, 1, 2, 2]])

ax3 = fp.add_subplot()
strm = ax3.streamplot(
X, Y, U, V, color=U, linewidth=2, cmap="autumn", start_points=seed_points.T
)
fp.fig.colorbar(strm.lines)
ax3.set_title("Controlling Starting Points")

# Displaying the starting points with blue symbols.
ax3.plot(seed_points[0], seed_points[1], "bo")
ax3.set(xlim=(-w, w), ylim=(-w, w))

# Create a mask
mask = np.zeros(U.shape, dtype=bool)
mask[40:60, 40:60] = True
U[:20, :20] = np.nan
U = np.ma.array(U, mask=mask)

ax4 = fp.add_subplot(gs=fp.gs[2:, :])
ax4.streamplot(X, Y, U, V, color="r")
ax4.set_title("Streamplot with Masking")

ax4.imshow(
~mask,
extent=(-w, w, -w, w),
alpha=0.5,
interpolation="nearest",
cmap="gray",
aspect="auto",
)

# close the figure
fp.close()
print("outfile: " + fp.outfile)

print("--Done!--")


if __name__ == "__main__":
test_main()

0 comments on commit 6f4d925

Please sign in to comment.