0

I would like to have a window that is divided in 4 sectors: in the (0,0) a imshow image (ax1); (1,0) a subplot image that uses twinx() image that divides the window(ax2 & ax3); (1,1) a regular plot image (ax4); and an iterative section (0,1) of plots that should give "number_of_subplots" plots one above the other (ax5). Hopefully with no xticklabels but the last one.

This is how the frame should look like before the iterative subplot creation.

My problem: when iterating to create the subplots on the top right space of the window, the subplots span away from that space and eliminate the ax4

This is how the window looks after the "for" cyle for the subplot creation

Below you'll find a simplification of the code I am using, just so you can see it better. I have replaced my experimental data with random numbers so you can replicate this easily.

Could you give me a hint on what am I doing wrong? I still do not dominate all the handlers in python. I used to do similar things in matlab a few years ago.

    import matplotlib.pyplot as plt
    from matplotlib.gridspec import GridSpec
    import numpy as np
    import pdb

    pos = [1,2,3,4,5]
    N = 50
    x = np.random.rand(N)
    y = np.random.rand(N)

    xx = np.linspace(0, 20, 1000)

    fig1 = plt.figure()
    number_of_subplots = len(pos) #number between 1-7
    ax1 = plt.subplot2grid((number_of_subplots+1,2),(0,0),rowspan = number_of_subplots-1) # Here the idea is to "dinamically" create the division of the grid, making space at the bottom of it for the image in the bottom left.
    ax1.scatter(x,y)

    ax2 = plt.subplot2grid((number_of_subplots+1,2),(number_of_subplots-1,0), rowspan = 2)
    ax2.plot(xx,np.sin(xx),label = 'sin(x)',color = 'b')
    ax3 = ax2.twinx()
    ax3.plot(xx,np.cos(xx), label = 'cos(x)', color = 'r')

    ax4 = plt.subplot2grid((number_of_subplots+1,2),(number_of_subplots-1,1), rowspan = 2)
    ax4.plot(xx,np.tan(xx), label = 'tan(x)', color = 'g')

    for i,v in enumerate(xrange(number_of_subplots)):
            v = v+1
            ax5 = plt.subplot2grid((number_of_subplots+1,2),(v-1,1))
            ax5.plot(np.sin(xx+3.1416*v/2)) # Grafica los perfiles, asociandoles el mismo color que para los cortes en la imagen 2D
            if (i % 2 == 0): #Even
                    ax5.yaxis.tick_left()
            else:
                    ax5.yaxis.tick_right()
    plt.draw()
    plt.show()

1 Answer 1

0

Solved the issue by using GridSpec as it is supposed to be used. Below is the implementation of the code that gives the following solution.

This is the correct way the image should look like and the implementation is below on the code.

    import matplotlib.pyplot as plt
    import matplotlib.gridspec as gridspec
    import numpy as np
    import pdb

    pos = [1,2,3,4,5]

    N = 50
    x = np.random.rand(N)
    y = np.random.rand(N)

    xx = np.linspace(0, 20, 1000)

    number_of_subplots = len(pos) #number between 1-7

    fig1 = plt.figure()

    gs0 = gridspec.GridSpec(2,2,height_ratios=[3,1],hspace=0.1)
    ax1 = plt.subplot(gs0[0,0])
    ax2 = plt.subplot(gs0[-1,0])
    ax4 = plt.subplot(gs0[-1,-1])

    gs2 = gridspec.GridSpecFromSubplotSpec(number_of_subplots, 1, subplot_spec=gs0[1],wspace=0.0, hspace=0.0)

    ax1.scatter(x,y)

    ax2.plot(xx,np.sin(xx),label = 'sin(x)',color = 'b')
    ax3 = ax2.twinx()
    ax3.plot(xx,np.cos(xx), label = 'cos(x)', color = 'r')

    ax4.plot(xx,np.tan(xx), label = 'tan(x)', color = 'g')

    for i in enumerate(xrange(number_of_subplots)):
            ax5 = plt.subplot(gs2[i,:])
            ax5.plot(np.sin(xx+3.1416*i/2)) 
            if (i % 2 == 0): #Even
                    ax5.yaxis.tick_left()
            else:
                    ax5.yaxis.tick_right()
    plt.draw()
    plt.show()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.