0

So now I can get unique files, hurrah! but it seems the second file is plotting both the first and the second plot, the third is plotting all three, fourth is plotting all four, etc. here is the new code:

for j in range(2):

    dhulist=pyfits.open('test.fits')
    row=5
    colum=j

    ax=[]
    val=[]
    for i in range(1600,3040):
        val.append((dhulist[0].data[i,row,colum]))
        ax.append(((((dhulist[0].header['CRPIX3'] -i)*(dhulist[0].header['CDELT3']))+5000)/1000))

    plt.plot(ax,val)
    #plt.show()
    plt.savefig("5_{0}.png".format(j))
4

1 回答 1

0

更新当前图形的plot功能,matplotlib如果没有当前图形,则创建一个新图形。这是一个很好地显式跟踪图形对象从创建到关闭的示例。

import numpy as np
from matplotlib import pyplot as plt

x = np.linspace(0, 10)

for j in range(3):
    y = x ** j
    f = plt.figure()
    plt.plot(x, y, figure=f)
    f.savefig("test_{}.png".format(j))
    plt.close(f)

请注意,涉及图形、打开、绘图、保存和关闭的每个操作都明确引用图形对象。恕我直言,这是一种非常好的编码风格,如果您需要一次处理多个数字,这将非常有用。Matplotlib 还允许您隐式使用“当前图形”,如果您正在做一些简单的事情,这很好。那看起来更像这样:

import numpy as np
from matplotlib import pyplot as plt

x = np.linspace(0, 10)
for j in range(3):
    y = x ** j
    plt.figure()
    plt.plot(x, y)
    plt.savefig("test2_{}.png".format(j))
    plt.close()
于 2013-10-15T02:42:23.930 回答