{"id":2515,"date":"2021-10-15T08:57:07","date_gmt":"2021-10-15T08:57:07","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=2515"},"modified":"2022-08-05T03:55:53","modified_gmt":"2022-08-05T03:55:53","slug":"tkinter-matplotlib","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-matplotlib\/","title":{"rendered":"Tkinter Matplotlib"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to display a graph from the Matplotlib library on a Tkinter application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='display-a-bar-chart-from-matplotlib-in-tkinter-applications'>Display a bar chart from matplotlib in Tkinter applications <a href=\"#display-a-bar-chart-from-matplotlib-in-tkinter-applications\" class=\"anchor\" id=\"display-a-bar-chart-from-matplotlib-in-tkinter-applications\" title=\"Anchor for Display a bar chart from matplotlib in Tkinter applications\">#<\/a><\/h2>\n\n\n\n<p>Matplotlib is a third-party library for creating professional visualizations in Python. Since <a href=\"https:\/\/matplotlib.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Matplotlib<\/a> is a third-party library, you need to install it before use. <\/p>\n\n\n\n<p>To install the matplotlib package, you can use the following <code>pip<\/code> command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">pip install matplotlib<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following program uses the <code>matplotlib<\/code> to create a bar chart that shows the top five programming languages by popularity.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">import<\/span> matplotlib\n\nmatplotlib.use(<span class=\"hljs-string\">'TkAgg'<\/span>)\n\n<span class=\"hljs-keyword\">from<\/span> matplotlib.figure <span class=\"hljs-keyword\">import<\/span> Figure\n<span class=\"hljs-keyword\">from<\/span> matplotlib.backends.backend_tkagg <span class=\"hljs-keyword\">import<\/span> (\n    FigureCanvasTkAgg,\n    NavigationToolbar2Tk\n)\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">App<\/span><span class=\"hljs-params\">(tk.Tk)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        super().__init__()\n\n        self.title(<span class=\"hljs-string\">'Tkinter Matplotlib Demo'<\/span>)\n\n        <span class=\"hljs-comment\"># prepare data<\/span>\n        data = {\n            <span class=\"hljs-string\">'Python'<\/span>: <span class=\"hljs-number\">11.27<\/span>,\n            <span class=\"hljs-string\">'C'<\/span>: <span class=\"hljs-number\">11.16<\/span>,\n            <span class=\"hljs-string\">'Java'<\/span>: <span class=\"hljs-number\">10.46<\/span>,\n            <span class=\"hljs-string\">'C++'<\/span>: <span class=\"hljs-number\">7.5<\/span>,\n            <span class=\"hljs-string\">'C#'<\/span>: <span class=\"hljs-number\">5.26<\/span>\n        }\n        languages = data.keys()\n        popularity = data.values()\n\n        <span class=\"hljs-comment\"># create a figure<\/span>\n        figure = Figure(figsize=(<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">4<\/span>), dpi=<span class=\"hljs-number\">100<\/span>)\n\n        <span class=\"hljs-comment\"># create FigureCanvasTkAgg object<\/span>\n        figure_canvas = FigureCanvasTkAgg(figure, self)\n\n        <span class=\"hljs-comment\"># create the toolbar<\/span>\n        NavigationToolbar2Tk(figure_canvas, self)\n\n        <span class=\"hljs-comment\"># create axes<\/span>\n        axes = figure.add_subplot()\n\n        <span class=\"hljs-comment\"># create the barchart<\/span>\n        axes.bar(languages, popularity)\n        axes.set_title(<span class=\"hljs-string\">'Top 5 Programming Languages'<\/span>)\n        axes.set_ylabel(<span class=\"hljs-string\">'Popularity'<\/span>)\n\n        figure_canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=<span class=\"hljs-number\">1<\/span>)\n\n\n<span class=\"hljs-keyword\">if<\/span> __name__ == <span class=\"hljs-string\">'__main__'<\/span>:\n    app = App()\n    app.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"504\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/10\/Tkinter-matplotlib.png\" alt=\"Tkinter Matplotlib\" class=\"wp-image-2516\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/10\/Tkinter-matplotlib.png 600w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/10\/Tkinter-matplotlib-300x252.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, import the <code>matplotlib<\/code> module<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> matplotlib<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>and call the <code>use()<\/code> function to tell which backend the <code>matplotlib<\/code> will use:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">matplotlib.use(<span class=\"hljs-string\">'TkAgg'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this case, we use <code>TkAgg<\/code> backend, which is made to integrate into Tkinter.<\/p>\n\n\n\n<p>Second, import the following <code>Figure<\/code>, <code>FigureCanvasTkAgg<\/code>, and <code>NavigationToolbar2Tk<\/code> classes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> matplotlib.figure <span class=\"hljs-keyword\">import<\/span> Figure\n<span class=\"hljs-keyword\">from<\/span> matplotlib.backends.backend_tkagg <span class=\"hljs-keyword\">import<\/span> (\n    FigureCanvasTkAgg,\n    NavigationToolbar2Tk\n)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Figure<\/code> class represents the drawing area on which <code>matplotlib<\/code> charts will be drawn.<\/p>\n\n\n\n<p>The <code>FigureCanvasTkAgg<\/code> class is an interface between the <code>Figure<\/code> and Tkinter <code>Canvas<\/code>.<\/p>\n\n\n\n<p>The <code>NavigationToolbar2Tk<\/code> is a built-in toolbar for the figure on the graph.<\/p>\n\n\n\n<p>Third, prepare the data for showing on the bar chart:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">data = {\n    <span class=\"hljs-string\">'Python'<\/span>: <span class=\"hljs-number\">11.27<\/span>,\n    <span class=\"hljs-string\">'C'<\/span>: <span class=\"hljs-number\">11.16<\/span>,\n    <span class=\"hljs-string\">'Java'<\/span>: <span class=\"hljs-number\">10.46<\/span>,\n    <span class=\"hljs-string\">'C++'<\/span>: <span class=\"hljs-number\">7.5<\/span>,\n    <span class=\"hljs-string\">'C#'<\/span>: <span class=\"hljs-number\">5.26<\/span>\n}\nlanguages = data.keys()\npopularity = data.values()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The data is a dictionary with the keys are the programming languages and values are their popularity in percentage.<\/p>\n\n\n\n<p>Fourth, create a <code>Figure<\/code> to hold the chart:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">figure = Figure(figsize=(<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">4<\/span>), dpi=<span class=\"hljs-number\">100<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Figure<\/code> object takes two arguments: size in inches and dots per inch (dpi). In this example, it creates a 600&#215;400 pixel figure.<\/p>\n\n\n\n<p>Fifth, create a <code>FigureCanvasTkAgg<\/code> object that connects the <code>Figure<\/code> object with a Tkinter&#8217;s <code>Canvas<\/code> object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">figure_canvas = FigureCanvasTkAgg(figure, self)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the <code>FigureCanvasTkAgg<\/code> object is not a <code>Canvas<\/code> object but contains a <code>Canvas<\/code> object.<\/p>\n\n\n\n<p>Sixth, create a <code>matplotlib<\/code>&#8216;s built-in toolbar:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">NavigationToolbar2Tk(figure_canvas, self)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Seventh, add a subplot to the figure and return the axes of the subplot:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">axes = figure.add_subplot()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Eighth, create a bar chart by calling the <code>bar()<\/code> method of the axes and passing the languages and popularity into it. Also, set the title and the label of the y-axis:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">axes.bar(languages, popularity)\naxes.set_title(<span class=\"hljs-string\">'Top 5 Programming Languages'<\/span>)\naxes.set_ylabel(<span class=\"hljs-string\">'Popularity'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, place the chart on the Tkinter&#8217;s <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/\">root window<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">figure_canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=<span class=\"hljs-number\">1<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Use <code>matplotlib<\/code> library to create professional-quality visualization in the Tkinter application.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2515\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-matplotlib\/\"\n\t\t\t\tdata-post-title=\"Tkinter Matplotlib\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2515\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-matplotlib\/\"\n\t\t\t\tdata-post-title=\"Tkinter Matplotlib\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>in this tutorial, you&#8217;ll learn how to display a graph from the Matplotlib library on a Tkinter application.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":60,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2515","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=2515"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/2515\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=2515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}