简体   繁体   English

使用 for 循环将子图添加到图形

[英]Adding subplots to figure using for loop

I am trying to add 8 heatmaps (subplots) to a figure but I can't seem to manage it.我正在尝试将 8 个热图(子图)添加到一个图中,但我似乎无法管理它。 Could you please help me?请你帮助我好吗?

# in order to modify the size
fig = plt.figure(figsize=(12,8))
# adding multiple Axes objects  
fig, ax_lst = plt.subplots(2, 4)  # a figure with a 2x4 grid of Axes

letter = "ABCDEFGH"

for character in letter:
    x = np.random.randn(4096)
    y = np.random.randn(4096)
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=(64,64))
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

    # Plot heatmap
    plt.clf()
    plt.title('Pythonspot.com heatmap example')
    plt.ylabel('y')
    plt.xlabel('x')
    plt.imshow(heatmap, extent=extent)
    plt.colorbar()
    plt.show()

Thank you!谢谢!

Similarly to this answer , you could do the following:此答案类似,您可以执行以下操作:

letter = "ABCDEFGH"

n_cols = 2
fig, axes = plt.subplots(nrows=int(np.ceil(len(letter)/n_cols)), 
                         ncols=n_cols, 
                         figsize=(15,15))

for _, ax in zip(letter, axes.flatten()):
    x = np.random.randn(4096)
    y = np.random.randn(4096)
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=(64,64))
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

    # Plot heatmap
    ax.set_title('Pythonspot.com heatmap example')
    ax.set_ylabel('y')
    ax.set_xlabel('x')
    ax.imshow(heatmap, extent=extent)
plt.tight_layout()  
plt.show()

在此处输入图像描述

First you were creating two separate figures by calling plt.figure() and then plt.subplots() (that last function creates both a figure and an array of axes)首先,您通过调用plt.figure()然后plt.subplots()创建两个单独的图形(最后一个 function 创建一个图形和一个轴数组)

Then you need to iterate over your axes, and plot on each one of these axes, instead of clearing the figure at each loop (which is what you were doing using plt.clf() )然后,您需要在每个轴上迭代您的轴和 plot,而不是在每个循环中清除图形(这是您使用plt.clf()所做的)

You can use the plt.XXXX() functions, but those only work on the "current" axes, so you have to change the current axes at each iteration.您可以使用plt.XXXX()函数,但这些函数仅适用于“当前”轴,因此您必须在每次迭代时更改当前轴。 Otherwise, you would be better off using the Axes.set_XXXX() functions, like in the other answer by @yatu.否则,最好使用Axes.set_XXXX()函数,就像@yatu 的另一个答案一样。 See here for more information .请参阅此处了解更多信息 fig, ax_lst = plt.subplots(2, 4, figsize=(12,8)) # a figure with a 2x4 grid of Axes fig, ax_lst = plt.subplots(2, 4, figsize=(12,8)) # 一个带有 2x4 轴网格的图形

letters = "ABCDEFGH"
for character,ax in zip(letters, ax_lst.flat):
    x = np.random.randn(4096)
    y = np.random.randn(4096)
    heatmap, xedges, yedges = np.histogram2d(x, y, bins=(64,64))
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

    # Plot heatmap
    plt.sca(ax) # make the ax object the "current axes"
    plt.title(character)
    plt.ylabel('y')
    plt.xlabel('x')
    plt.imshow(heatmap, extent=extent)
    plt.colorbar()
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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