简体   繁体   English

Mac 视网膜显示屏上 Tkinter 中的模糊 Matplotlib 图

[英]Blurry Matplotlib figure in Tkinter on a Mac retina display

I embedded a Matplotlib figure in a tkinter window running on macOS.我在 macOS 上运行的 tkinter window 中嵌入了一个 Matplotlib 图。 However, my MacBook Pro has a retina (high resolution) display which makes the Matplotlib figure look blurry.但是,我的 MacBook Pro 有视网膜(高分辨率)显示屏,这使得 Matplotlib 数字看起来很模糊。 I tried to scale the app using app.tk.call('tk', 'scaling', 2.0) but it made the plot figure too large to display in the window. How do I embed a Matplotlib figure at high resolution to support the retina display on a Mac?我尝试使用app.tk.call('tk', 'scaling', 2.0)应用程序,但它使 plot 图太大而无法在 window 中显示。如何以高分辨率嵌入 Matplotlib 图以支持Mac 上的视网膜显示器? Is it possible to embed a Matplotlib figure as a vector graphic (SVG, PDF) to avoid resolution issues?是否可以将 Matplotlib 图形嵌入矢量图形(SVG、PDF)以避免分辨率问题?

tkinter matplotlib 图

import tkinter
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)

class App(tkinter.Tk):

    def __init__(self):
        super().__init__()
        self.title('Tkinter Matplotlib Demo')

        # Data to plot
        data = [3, 4, 2, 8, 7, 10]

        # Create plot and figure
        fig, ax = plt.subplots(dpi=100)
        ax.plot(data)
        ax.set_xlabel('X axis')
        ax.set_ylabel('Y axis')

        # Create FigureCanvasTkAgg object
        figure_canvas = FigureCanvasTkAgg(fig, self)

        # Create the Matplotlib toolbar
        NavigationToolbar2Tk(figure_canvas, self)

        figure_canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

if __name__ == '__main__':
    app = App()
    app.geometry('600x400')
    app.tk.call('tk', 'scaling', 2.0)
    app.mainloop()

Approach #2方法#2

I tried to use the Matplotlib macosx backend but it reports an error related to NSButton and terminates the app.我尝试使用 Matplotlib macosx 后端,但它报告了与 NSButton 相关的错误并终止了该应用程序。

import tkinter
import matplotlib.pyplot as plt
from matplotlib.backends.backend_macosx import (FigureCanvasMac, NavigationToolbar2Mac)

class App(tkinter.Tk):

    def __init__(self):
        super().__init__()
        self.title('Tkinter Matplotlib Demo')

        # Data to plot
        data = [3, 4, 2, 8, 7, 10]

        # Create plot and figure
        fig, ax = plt.subplots(dpi=100)
        ax.plot(data)
        ax.set_xlabel('X axis')
        ax.set_ylabel('Y axis')

        # Create FigureCanvasTkAgg object
        figure_canvas = FigureCanvasMac(fig)

        # Create the Matplotlib toolbar
        NavigationToolbar2Mac(figure_canvas)

        figure_canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

if __name__ == '__main__':
    app = App()
    app.mainloop()

It looks like your dpi is still set to 100. Try moving that up to 1200. There was a similar problem here: Matplotlib - How to plot a high resolution graph?看起来你的 dpi 仍然设置为 100。尝试将其提高到 1200。这里有一个类似的问题: Matplotlib - How to plot a high resolution graph?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM