Python绘制多个颜色条

时间:2017-02-02 00:54:39

标签: python matplotlib plot pythonplotter

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

然而,在我绘制数据之后,我意识到我绘制的图像越多,我的图形上出现的颜色条就越多......以前的图中的颜色条似乎只是粘在一起而不会消失......

我有什么建议可以摆脱多余的色块?谢谢你们!

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)

带有两个颜色条的图像
Image with two colorbars

带有更多色条的图像
Image with even more colorbars

1 个答案:

答案 0 :(得分:0)

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

plt.clf()  
plt.close()  

如果未指定特定窗口,则plt.clf()将清除当前窗口中的绘图,但会使窗口保持打开状态以用于创建其他绘图。如果未指定特定窗口,则plt.close()将关闭当前窗口。这两个电话的帮助在这里找到:

  

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

在这里可以找到一个非常有用的帖子:

  

When to use cla(), clf() or close() for clearing a plot in matplotlib?

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