简体   繁体   中英

Matplotlib axes: splitting an axes object into two

I've started working more with figures and axes, and at first blush it seems to be really nice: an axes object can be independently created and manipulated (either by adding plots to it, or changing scale/etc), however the issue I'm running into is that it appears that "Figure" is the only class that can control layout of axes objects.

I would like to do something like this:

def plot_side_by_side(lefts, rights, coupled=True, width_ratios=[2,1]):
    import matplotlib.gridspec as gridspec
    # lefts and rights are lists of functions that 
    # take axes objects as keywords, the length of this
    # object is the number of subplots we have:
    plots = list(zip(lefts, rights))
    y_size = len(plots)

    # create figure with a number of subplots:
    fig = plt.figure(figsize=(10,y_size * 4))
    gs = gridspec.GridSpec(y_size,2,width_ratios=width_ratios,height_ratios=[1 for _ in plots])

    #get axes on the left
    cleft_axes = [plt.subplot(gs[0,0])]
    if y_size > 1:
        cleft_axes += [plt.subplot(gs[i,0], sharex=cleft_axes[0]) for i in range(1,y_size)]
    [plt.setp(ax.get_xticklabels(), visible=False) for ax in cleft_axes[:-1]]

    # get axes on the right, if coupled we fix the yaxes
    # together, otherwise we don't
    if coupled:
        yaxes = cleft_axes
    else:
        yaxes = [None for _ in cleft_axes]
    cright_axes = [plt.subplot(gs[0,1], sharey=yaxes[0])]
    if y_size > 1:
        cright_axes += [plt.subplot(gs[i,1], sharey=yaxes[i], sharex=cright_axes[0]) for i in range(1,y_size)]
    [plt.setp(ax.get_xticklabels(), visible=False) for ax in cright_axes[:-1]]

    # for each plot in our list, give it an axes object if it is on
    # the left or right.  Now this function will plot on that axes

    for (pl, pr), l, r, name in zip(plots,cleft_axes,cright_axes,names):
        pl(ax=l)
        pr(ax=r)

    return fig

And I would like to be able to create a function that takes a axes object as a keyword and puts two plots on it:

def twoplots(ax=ax):
    # make a grid of axes, allow them to be plotted to, etc.
    # this is all within the space given me by `ax`.

Is this possible? How would I go about doing such a thing? I know that I can get the figure from the axes object that is passed, is it possible to modify the parent gridspec without messing up every other gridspec?

Hope I'm not committing a foul reviving a thread this old. I wanted to give some extra context on what I think the OP is trying to do. (At least I hope it is what he's trying to do, because I'm trying to do the same thing.)

Suppose I have a statistical model that is composed of K submodels of different types. I want the submodels to plot themselves. Most of the time, in the typical case, each submodel will plot itself on an axes object. Occasionally, a submodel might need multiple axes to plot itself.

For example: suppose a model is a time series model, and the submodels are showing trend, seasonality, regression effects, holiday effects, etc. If the seasonal effect is showing annual seasonality it will plot itself just like the trend model (its effect vs time). But if it is showing day of week seasonality the plot vs time will be ineffective, because the lines will wiggle too fast. It would be more effective to plot the time series of Mondays, then the time series of Tuesdays, etc. To fit in with the larger scheme you want this cluster of 7 plots to be "the seasonality plot."

With K submodels you can often start off with fig, ax = plt.submodels(K) and then pass ax[k] to the submodel as model.submodel[k].plot(ax[k]) . The question is what to do when you'd like to plot the day-of-week seasonality effect described above on ax[k] .

One answer might be "don't use this mechanism: use GridSpec or something else." But that's what I think the question is asking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM