'Blurry Matplotlib figure in Tkinter on a Mac retina display

I embedded a Matplotlib figure in a tkinter window running on macOS. However, my MacBook Pro has a retina (high resolution) display which makes the Matplotlib figure look blurry. 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? Is it possible to embed a Matplotlib figure as a vector graphic (SVG, PDF) to avoid resolution issues?

tkinter matplotlib figure

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

I tried to use the Matplotlib macosx backend but it reports an error related to NSButton and terminates the app.

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


Solution 1:[1]

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?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 impilcature