Search code examples
pythonmatplotlibplotsubploterrorbar

How to plot multiple data usind add_axes without over writing existing


I try to create a figure with zoomed inset graphs, where the data to the entire figure (all subplots and insets) is plotted at different positions of the code.

To mimic the plotting at different positions in the code, the minimum (NOT working) example loops over the plotting routine.

The subplots work, but their insets are overwritten each time. The inset axes are created using add_axes.

I have tried, not to create the subaxes (add_axes) each time, but only create them, if not already present by:

try:
    subax1
except NameError:
    subax1 = fig666.add_axes([0.5,0.71,0.35,0.16]) 

this also didn't help!

How can I fix the problem/ what is my conceptual misunderstanding?

Thanks for your help!!!

import matplotlib.pyplot as plt
import numpy

x_data=numpy.array([0,    1,   1.85,   1.9,  1.95,   2, 2.1, 2.5,   5,  10, 25])
y_data=numpy.array([0,  2.5,    1.8,   0.5,   0.2,  11, 1.2, 0.5, 0.15, 10, 25])
y_data_err=y_data*0.1

number_of_runs=3
for iterator in range(number_of_runs):
    fig666=plt.figure(666,figsize=(22.0/2.54,18.0/2.54))
#############################
    # subplot
    ax = plt.subplot(3,1,1)
    #ax.plot(x_data,y_data+iterator*0.4,marker=None)              
    ax.errorbar(x_data,y_data+iterator*0.4,yerr=y_data_err)
    #plt.semilogy()
    plt.xlim(1.87,2.25)
    plt.ylim(0,3.7)   

    #####################
    #  zoomed inset to subplot ##
    subax1 = fig666.add_axes([0.5,0.71,0.35,0.16])
      #subax1.plot(x_data,y_data+iterator*0.2+0.1,marker=None)              
    subax1.errorbar(x_data,y_data+iterator*0.4,yerr=y_data_err)

    plt.xlim(1.87,2.25)
    plt.ylim(0,3.7)

Solution

  • The problem seems to be caused by the subplot command: as the inset plot/axes is entirely within the subplot, the inset axes/plot is removed as subplot is called again. Using add_axes to create the surrounding axes instead of subplot(3,1,1) fixes the issue:

    import matplotlib.pyplot as plt
    import numpy
    
    x_data=numpy.array([0,    1,   1.85,   1.9,  1.95,   2, 2.1, 2.5,   5,  10, 25])
    y_data=numpy.array([0,  2.5,    1.8,   0.5,   0.2,  11, 1.2, 0.5, 0.15, 10, 25])
    y_data_err=y_data*0.1
    
    
    
    number_of_runs=3
    index_lim=2
    
    for iterator in range(number_of_runs):
        fig666=plt.figure(666,figsize=(22.0/2.54,18.0/2.54))
    #############################
        # subplot
        #ax = plt.subplot(3,1,3)
        ax=fig666.add_axes([0.125,0.666,0.775,0.235])
        ax.plot(x_data,y_data+iterator*0.2,marker=None)              
        ax.errorbar(x_data,y_data+iterator*0.2,yerr=y_data_err)
        plt.semilogy()
    
    
        #####################
        #  zoomed inset to subplot ##
        subax1 = fig666.add_axes([0.5,0.71,0.35,0.16])
          #subax1.plot(x_data,y_data+iterator*0.2+0.1,marker=None)              
        subax1.errorbar(x_data,y_data+iterator*0.5,yerr=y_data_err)
    
        plt.xlim(1.87,2.25)
        plt.ylim(0,3.7)
    plt.show()
    ax = plt.subplot(3,1,2)