Dynamically add subplots in matplotlib with more than one column

前端 未结 3 1297
囚心锁ツ
囚心锁ツ 2020-12-30 13:26

How can I dynamically add a new plot to bunch of subplots if I\'m using more than one column to display my subplots? This answers this question for one column, but I cant se

3条回答
  •  不知归路
    2020-12-30 13:49

    Assuming at least 1 dimension of your grid and the total number of plots is known, I would use the gridspec module and a little bit of math.

    import math
    
    import matplotlib.pyplot as plt
    from matplotlib import gridspec
    
    def do_plot(ax):
        ax.plot([1,2,3], [4,5,6], 'k.')
    
    
    N = 11
    cols = 3
    rows = int(math.ceil(N / cols))
    
    gs = gridspec.GridSpec(rows, cols)
    fig = plt.figure()
    for n in range(N):
        ax = fig.add_subplot(gs[n])
        do_plot(ax)
    
    fig.tight_layout()
    

提交回复
热议问题