matplotlib 如何使用tkinter app plot功能正确保存绘图?

ilmyapht  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(128)

我使用matplotlib.pyplot.savefig保存我的图,但是生成的图有时会有错误。这些错误在每个图像中是不同的,可以是具有透明背景的图像,完全透明的图像,完全白色的图像或图像上的白色条。有时也会正确保存。我个人不认为这有什么规律。我确信问题在于将图保存为图像,因为当我在保存前在tkinter UI上显示图时,它是完美无瑕的。多次保存相同的图也会引入不同的错误,因此它们与图本身无关。
下面是我的python代码中负责绘图的部分:

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()

在函数的底部保存绘图:

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')

如果相关的话,这个函数在一个单独的线程中执行。

保存良好

白色保存

无后台BUG

透明

我尝试使用savefig参数并将图形保存为不同的格式,但它没有改变任何东西。

e5njpo68

e5njpo681#

Matplotlib对于多线程不安全。如果你同时保存多个图,这会导致bug,因为matplotlib不知道要保存哪个图。
不妨试试这个:

if self.Time.get() == 'After':
            fig.savefig(os.path.join(self.pinDir,self.SampleName.get()+'_'+pin+'_'+self.Time.get()+'_'+self.Test.get()+'.png'),facecolor='white')
        else:
            fig.savefig(os.path.join(self.pinDir,self.SampleName.get()+'_'+pin+'_'+self.Time.get()+'.png'),facecolor='white')

相关问题