Skip to content

Commit

Permalink
Added updating start position when advancing to a new page (#9)
Browse files Browse the repository at this point in the history
* Added updating start position when advancing to a new page
  • Loading branch information
ebenp authored Nov 10, 2020
1 parent 2e96fa2 commit 171d781
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 7 deletions.
27 changes: 21 additions & 6 deletions figpager/figpager.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ def __init__(
# transform storage
self.transform = None

# subplt counter storage
self.subplotcounter = 0

# update from layout
self._update_from_layout()
# draw the initial page
Expand Down Expand Up @@ -1071,11 +1074,16 @@ def add_subplot(self, direction="left-to-right", pos=None, gs=None, **kwargs):
pos = [pos[0] + 1, 0]

else:
# reset the starting position
# save the starting position
pos = self.subplotstartindex

self.fig, self.ax, self.gs, self.transform = self.add_page(
subplotstartindex=self.subplotstartindex
)
# reset the
# self.subplotstartindex = pos
self.currentsubplotindex = pos


if self.direction == "top-to-bottom":
if self.subplotstartindex is None:
Expand All @@ -1100,15 +1108,21 @@ def add_subplot(self, direction="left-to-right", pos=None, gs=None, **kwargs):
self.fig, self.ax, self.gs, self.transform = self.add_page(
subplotstartindex=self.subplotstartindex
)

# use the existing column and add a row
self.subplotstartindex = [pos[0] + 1, pos[1]]

else:
self.subplotstartindex = pos

self.currentsubplotindex = pos
# advance the subplot counter. Makes a unique subplot label
self.subplotcounter = self.subplotcounter + 1

if gs is None:
return self.fig.add_subplot(
self.gs[pos[0], pos[1]],
label="({},{})".format(pos[0], pos[1]),
label="({},{}, {})".format(pos[0], pos[1], self.subplotcounter),
**kwargs
)
else:
Expand All @@ -1119,9 +1133,9 @@ def add_subplot(self, direction="left-to-right", pos=None, gs=None, **kwargs):

return self.fig.add_subplot(
gs,
label="({},{})".format(
self.currentsubplotindex[0], self.currentsubplotindex[1]
),
label="({},{}, {})".format(
self.currentsubplotindex[0], self.currentsubplotindex[1],
self.subplotcounter),
**kwargs
)

Expand Down Expand Up @@ -1172,6 +1186,7 @@ def add_page(
defaults depend on the image format and backend:
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
Returns: fig; figure instance, ax; axes instances, gs; GridSpec, self.transform; Figure Transform
Expand Down Expand Up @@ -1267,7 +1282,7 @@ def add_page(
if metadata is not None:
self.metadata = metadata

if subplotstartindex == [0, 0] or subplotstartindex is None:
if subplotstartindex is None:
self.subplotstartindex = None
else:
self.subplotstartindex = subplotstartindex
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.26",
version="0.27",
author="Eben Pendleton",
author_email="4080051+ebenp@users.noreply.github.com",
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 added tests/out_6.pdf
Binary file not shown.
101 changes: 101 additions & 0 deletions tests/test_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Test for multipage 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_6.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,
)

for r in range(2):
#if r > 0:
# fp.add_page(
# nrows=3, ncols=2, orientation="portrait", height_ratios=[1, 1, 2]
# )
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 171d781

Please sign in to comment.