I am saving my plot using matplotlib.pyplot.savefig
, but the resulting figure sometimes has bugs in it. These bugs are different in each image and can be the image with a transparent background, a fully transparent image, a fully white image or white bars on my image. Sometimes it also saves it correctly. I personally don't see a pattern in this. I'm certain the issue lies with saving the plot as an image because when I show the plot on my tkinter
UI before saving it, its flawless. Saving the same plot multiple times can introduce different bugs as well, so they aren't related to the plot itself.
Here is the portion of my python code that takes care of the plotting:
import serial
import serial.tools.list_ports
import tkinter as tk
from tkinter import filedialog
import pathlib
import csv
import threading
import pyvisa
import numpy as np
import os
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib
from PIL import Image, ImageTk
def Plot(self, pin, volts, currs):
self.plottedPin.set("Current Plotted Pin: " + pin)
fig,ax = plt.subplots()
fig.set_figheight(4)
fig.set_figwidth(6)
volts_split = np.array([s.split(' ') for s in volts])
volts_values = volts_split[:,0].astype(np.float16)
volts_values = np.round(volts_values,0)
rounded_volts = np.array([val for val in volts_values])
curr_split = np.array([s.split(' ') for s in currs])
curr_values = curr_split[:,0].astype(np.float16)
curr_units = curr_split[:,1]
for i in range(len(curr_values)):
if (curr_units[i]) == 'µA':
curr_values[i] = curr_values[i] / 1000
elif (curr_units[i]) == 'nA':
curr_values[i] = curr_values[i] / 1000000
curr_values = np.round(curr_values,2)
rounded_currs = np.array([val for val in curr_values])
ax.plot(rounded_volts,rounded_currs,marker='o')
ax.set_xlabel('Voltage V')
ax.set_ylabel('Current mA')
fig.subplots_adjust(left=0.15, bottom=0.18, right=0.97, top=0.95)
canvas = FigureCanvasTkAgg(fig, master=self.window)
canvas.draw()
canvas.get_tk_widget().place(x=130,y=340,height=400,width=700)
self.event.wait()
if self.Time.get() == 'After':
plt.savefig(os.path.join(self.pinDir,self.SampleName.get()+'_'+pin+'_'+self.Time.get()+'_'+self.Test.get()+'.png'),facecolor='white')
else:
plt.savefig(os.path.join(self.pinDir,self.SampleName.get()+'_'+pin+'_'+self.Time.get()+'.png'),facecolor='white')
self.event.clear()
The saving of the plot is done at the bottom of the function:
if self.Time.get() == 'After':
plt.savefig(os.path.join(self.pinDir,self.SampleName.get()+'_'+pin+'_'+self.Time.get()+'_'+self.Test.get()+'.png'),facecolor='white')
else:
plt.savefig(os.path.join(self.pinDir,self.SampleName.get()+'_'+pin+'_'+self.Time.get()+'.png'),facecolor='white')
This function is executed in a seperate thread if that is of relevance.
I tried playing with the savefig
parameters and saving the figure as a different format but it didn't change anything.
I figured it out. By saving the plot before displaying it on my UI, the resulting image is always correct.