如何在python中为子图运行智能循环 [英] How to run a smart loop for subplots in python

查看:43
本文介绍了如何在python中为子图运行智能循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试创建一个由9个图形组成的3x3框,尽管我设法通过手动为每个框编写代码来做到这一点,但我想学习如何使用循环.我似乎无法弄清楚.目前,我正在使用以下内容:

So I'm trying to create a 3x3 box of 9 graphs, and while I've managed to do it by manually writing the code for each individual box, I want to learn how to do it using a loop. I can't seem to figure it out. Currently, I'm using the following:

from matplotlib import gridspec

f, ((ax1, ax2, ax3), (ax4, ax5, ax6), (ax7, ax8, ax9)) = plt.subplots(3, 3, sharex='col', sharey='row')

gs = gridspec.GridSpec(3, 3)
fig = plt.figure(figsize=(20,20))

fig.text(0.5, .95, 'Constant Slope for [O/Fe]/[Fe/H] for Various R and Z', ha='center', va='center', size = 50)
fig.text(0.5, 0.08, '[Fe/H]', ha='center', va='center', size = 60)
fig.text(0.09, 0.5, '[O/Fe]', ha='center', va='center', rotation='vertical', size = 60)

ax1 = plt.subplot(gs[0])
histogram1 = ax1.hist2d(fehsc, ofesc, bins=nbins, range=[[-1,.5],[0.2,0.4]])
counts = histogram1[0]
xpos = histogram1[1]
ypos = histogram1[2]
image = histogram1[3]
newcounts = counts #we're going to iterate over this

for i in range (nbins):
    xin = xpos[i]
    yin = ypos
    yline = m*xin + b
    reset = np.where(yin < yline) #anything less than yline we want to be 0
    #index = index[0:len(index)-1]  
    countout = counts[i]
    countout[reset] = 0
    newcounts[i] = countout
ax1.plot(xarr2, yarr2, color='w', linewidth='5', alpha = 0.3)
ax1.plot(xarr, yarr, color='r')
ax1.set_title('R in [5,7] kpc | Z in [1,2] kpc', size = 20)

ax2 = plt.subplot(gs[1])
histogram2 = ax2.hist2d(fehsc2, ofesc2, bins=nbins, range=[[-1,.5],[0.2,0.4]])
counts = histogram2[0]
xpos = histogram2[1]
ypos = histogram2[2]
image = histogram2[3]
newcounts = counts #we're going to iterate over this

for i in range (nbins):
    xin = xpos[i]
    yin = ypos
    yline = m*xin + b
    reset = np.where(yin < yline) #anything less than yline we want to be 0
    #index = index[0:len(index)-1]  
    countout = counts[i]
    countout[reset] = 0
    newcounts[i] = countout
ax2.plot(xarr2, yarr2, color='w', linewidth='5', alpha = 0.3)
ax2.plot(xarr, yarr, color='r')
ax2.set_title('R in [7,9] kpc | Z in [1,2] kpc', size = 20)

依此类推,直到 ax9.

And so on and so forth, until ax9.

我试图做的是以下事情:

What I've tried to do is the following:

for k in range(1,10):
    ax[k] = plt.subplot(gs[0])
    histogram1 = ax[k].hist2d(fehsc, ofesc, bins=nbins, range=[[-1,.5],[0.2,0.4]])
    counts = histogram1[0]
    xpos = histogram1[1]
    ypos = histogram1[2]
    image = histogram1[3]
    newcounts = counts #we're going to iterate over this

    for i in range (nbins):
        xin = xpos[i]
        yin = ypos
        yline = m*xin + b
        reset = np.where(yin < yline) #anything less than yline we want to be 0
        countout = counts[i]
        countout[reset] = 0
        newcounts[i] = countout
    ax[k].plot(xarr2, yarr2, color='w', linewidth='5', alpha = 0.3)
    ax[k].plot(xarr, yarr, color='r')
    ax[k].set_title('R in [5,7] kpc | Z in [1,2] kpc', size = 20)

因为我想循环使用ax(k)并一次运行所有九次迭代.但这显然不是这样做的方法,或者就是行不通.是否可以将ax_并在调用时从1循环迭代到9?

Because I want to take ax(k) in a loop and run all nine iterations at once. But obviously this isn't the way to do it, or just doesn't work. Is it possible to take ax_ and iterate it from 1 to 9 in a loop while calling it?

推荐答案

一种迭代许多子图的简单方法是通过

An easy way to iterate over many subplots is to create them via

fig, axes = plt.subplots(nrows=n, ncols=m)

那么

axes 是一个 n 行和 m 列的数组.然后,可以独立地遍历行和列,或遍历轴数组的展平版本.

axes is then an array of n rows and m columns. One could then iterate over the rows and columns independently, or over the flattened version of the axes array.

在此示例中显示了后者:

The latter is shown in this example:

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(11)
y = np.random.rand(len(x), 9)*10

fig, axes = plt.subplots(3,3, sharex=True, sharey=True)

for i, ax in enumerate(axes.flatten()):
    ax.bar(x, y[:,i], color=plt.cm.Paired(i/10.))

plt.show()

这篇关于如何在python中为子图运行智能循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆