2

I was trying to plot some data and I created a loop so that I can load the image more efficiently.

However after I plotted the data, I realised that the more images I plot, the more colorbars appeared on my graph... The colorbars from previous plots just seem to stick around and don't go away...

Any suggestions how I can get rid of the extra colorbars? Thanks guys!

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 two colorbars

Image with even more colorbars
Image with even more colorbars

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Alan Yin
  • 21
  • 1
  • 3
  • You may want to create a separate figure for each plot. See [this tutorial](http://matplotlib.org/users/pyplot_tutorial.html#working-with-multiple-figures-and-axes). – a_guest Feb 02 '17 at 01:10
  • Thanks! I've had a look at the tutorial but I thought I would have already created a separate figure by using savefig()? – Alan Yin Feb 02 '17 at 07:03
  • `pyplot.savefig` just saves the current figure. You can create a new figure with `pyplot.figure()`. All subsequent calls to `pyplot.plot` (or similar plotting methods) will be realized on the most recent figure you've created. As @SurfProc already mentioned in his answer `plt.clf()` (meaning "clear figure") clears the current figure. For you calling `plt.clf()` before plotting each image seems to be a suitable solution as well. – a_guest Feb 02 '17 at 11:22
  • Thanks bro! I'll do as you suggested :) – Alan Yin Feb 03 '17 at 06:40

1 Answers1

1

Have you tried clearing the plot from the current window, or closing the plot window? The generic syntax is as follows:

plt.clf()  
plt.close()  

The plt.clf() clears the plot from the current window, if a specific window is not specified, but it leaves the window open for use in creating additional plots. The plt.close() closes the current window, if a specific window is not otherwise specified. Help on these two calls is found here:

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

And a very useful post to your question is found here:

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

The correct place to insert either of these calls is just below your call to write the image to file: plt.savefig(savename). I hope this helps.

Community
  • 1
  • 1
SurfProc
  • 105
  • 9