plot a large number of axis objects using one command through a loop

后端 未结 3 815
深忆病人
深忆病人 2021-01-23 21:35

Say I have a bunch of ax1,ax2,ax3... and I want to run them through a plotting function:

def plotxy(ax,x,y):
    x = np.array(x)
    y = np.array(y)
    ax.plot(         


        
相关标签:
3条回答
  • 2021-01-23 22:06

    you can try something like this:

    import matplotlib.pyplot as plt
    fig, axs = plt.subplots(nrows=3, ncols=2)
    
    for ax in axs.flat:
        plotxy(ax,x,y)
    

    If you use plt.subplot or plt.axes you can create a list/array of axes by hand

    0 讨论(0)
  • 2021-01-23 22:06

    I'm probably missing something in your question, but why don't you just use a simple loop?

    for ax in ( ax1, ax2, ..., axn ):
        plotxy(ax,x,y)
    

    You can also avoid the explicit listing all variables:

    axes = ( locals()['ax%d'%i] for i in range(1, n+1) )
    for ax in axes:
        plotxy(ax,x,y)
    
    0 讨论(0)
  • 2021-01-23 22:17

    you can also use map, x and y being name space variables and axx some iterable over your axes objects:

    x = ...
    y = ...
    
    def plotxy(ax):
        ax.plot(x,y)
    
    map(plotxy,axx)
    
    0 讨论(0)
提交回复
热议问题