1

我试图绘制一些数据并创建了一个循环,以便更有效地加载图像。

然而,在我绘制数据之后,我意识到我绘制的图像越多,我的图表上出现的颜色条就越多......以前绘图中的颜色条似乎只是停留在周围并且不会消失......

有什么建议可以摆脱多余的颜色条吗?多谢你们!

import tifffile as tiff
import numpy as np 
from tkinter import filedialog 
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

#------------------------------------------------------------------
# Calculate the net OD data from the pre-scanned and irradiated films using calibration curve
# Calibration Film Area x -> 250:350  ;  y -> 140:250 
refpvpre = 35000
refpvpost = 35000
def polynomial_2_function(x, a, b, c):
    return a*x**2 + b*x + c
#-------------------------------------------------------------------
nfilm = int(input("Please type in the number of films you would like to analyse: "))
for x in range(nfilm): # Start a loop for reading nfilm number of films (nfilm = 11)
    pre_scan_filepath = filedialog.askopenfilename(title = "Please select a pre-scanned film")
    blank_image = tiff.imread(pre_scan_filepath)
    bgdpv = np.mean(blank_image[50:150, 100:200, 1]) # Calculate the background pixel value
    pre_aoi = blank_image[140:250, 250:350, 1] + (refpvpre - bgdpv) # Correct the pixel value using reference
    post_scan_filepath = filedialog.askopenfilename(title = "Please select an irradiated film")
    image = tiff.imread(post_scan_filepath)
    bgdpv = np.mean(image[50:150, 100:200, 1]) # Calculate the background pixel value
    post_aoi = image[140:250, 250:350, 1] + refpvpost - bgdpv # Correct the pixel value using reference
    data = np.log10(pre_aoi/post_aoi)
    data = np.flipud(data.transpose()) # Set the image in the right orientation
    # define 2nd order polynomial fitting function
    dose_data = polynomial_2_function(data, 43.411, 6.518, 0.6428)
    savename = input("Please type in the file name you would like to save your image as: ") + '.tif'
    imgplot = plt.imshow(dose_data, extent = [0,4,0,6], aspect = 'auto')
    imgplot.set_cmap('nipy_spectral')
    plt.colorbar()
    plt.savefig(savename)

带有两个颜色条的图像
带有两个颜色条的图像

带有更多颜色条的图像
带有更多颜色条的图像

4

1 回答 1

1

您是否尝试过从当前窗口清除绘图,或关闭绘图窗口?通用语法如下:

plt.clf()  
plt.close()  

plt.clf() 如果未指定特定窗口,则从当前窗口清除绘图,但它使窗口保持打开状态以用于创建其他绘图。如果未另外指定特定窗口,则 plt.close() 关闭当前窗口。可以在此处找到有关这两个调用的帮助:

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.clf

在这里可以找到对您的问题非常有用的帖子:

何时使用 cla()、clf() 或 close() 清除 matplotlib 中的绘图?

插入这些调用的正确位置就在将图像写入文件的调用下方:plt.savefig(savename)。我希望这有帮助。

于 2017-02-02T02:29:16.883 回答