grid-strategy icon indicating copy to clipboard operation
grid-strategy copied to clipboard

Allow user to insert existing figure into specific grid

Open smeng10 opened this issue 5 years ago • 3 comments

Can we add an insert function to let users insert their premade figures into specific grids?

smeng10 avatar Feb 26 '19 05:02 smeng10

Can you clarify what you mean by this? If I understand correctly, that seems out of scope for this project, which is really more intended to handle the subplot geometry, and you can do with that as you wish using the normal matplotlib API.

What kind of use case were you thinking of, though? Even if it's out of scope for this project, it might be worth including in the documentation.

pganssle avatar Feb 26 '19 14:02 pganssle

What I was thinking was when we trying to make the presentation, we had a lot of pain on how to insert pre-generated figures (sin functions to the power of number of grids) into the specs. We can only plot the figures on the go. i.e

t1 = np.arange(-5.0, 5.0, 0.2)
specs1 = strategies.SquareStrategy("left").get_grid(5)
for i, sub in enumerate(specs1):
    plt.subplot(sub)
    plt.plot(t1, np.sin(t1) ** i)
plt.show()

In my view, it is hard for me to find a way inserting a figure into specific grid, what I want to do is:

#pre-generated figures sin_0 through sin_4 are the same as 
#plt.plot(t1, np.sin(t1) ** i) in the previous code
images = [sin_0, sin_1, sin_2, sin_3, sin_4]
specs1 = strategies.SquareStrategy("left").get_grid(5)
for i, sub in enumerate(specs1):
    # This is wrong but what I was tring to do is
    # if I can have another feature to just insert any image I want in any order
    # this might be helpful because I can have images = [sin_3, sin_0, sin_2, sin_1, sin_4]
    # and still insert into the grids using my order. 
    # But it is harder to do in the previous example

    # i means which grid is the target grid, and
    # image[i] means which image to insert into the target grid.
    sub.insert(i,images[i])
plt.show()

Or maybe I can figure out a matplotlib way and do a documentation example.

smeng10 avatar Feb 26 '19 17:02 smeng10

The way Matplotlib is designed we don't really have the concept of embedding a Figure an another Figure. I suggest having a look at https://matplotlib.org/tutorials/introductory/usage.html#coding-styles for how we suggest to write helper functions for this usage. Something like:

t1 = np.arange(-5.0, 5.0, 0.2)
def helper(ax, i):
    return ax.plot(t1, np.sin(t1)**i)

specs1 = strategies.SquareStrategy("left").get_grid(5)
fig = plt.figure()
for i, sub in enumerate(specs1):
    ax = fig.add_subplot(sub)
    helper(ax, i)

which for this case is a bit of overkill, but starts to be worth it as helper gets longer.

tacaswell avatar Feb 28 '19 14:02 tacaswell