{"id":25078,"date":"2021-12-31T06:13:19","date_gmt":"2021-12-31T06:13:19","guid":{"rendered":"https:\/\/www.askpython.com\/?p=25078"},"modified":"2021-12-31T06:13:20","modified_gmt":"2021-12-31T06:13:20","slug":"plotting-libraries-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/plotting-libraries-python","title":{"rendered":"4 Easy Plotting Libraries for Python With Examples"},"content":{"rendered":"\n<p>Python offers a lot of interactive plotting packages through which we can make some of the most beautiful and customizable graphs and charts available out there. In this article, we will be looking at some of the python modules that are used for plotting and how basic charts are coded with them. These are some of the most widely used python packages and are available for all platforms (like &#8211; Windows, Linux Mac).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Matplotlib &#8211; Oldest Plotting Library<\/h2>\n\n\n\n<p>If you are accustomed to Python, you must have heard about Matplotlib. It is one of the oldest Python libraries used for plotting, built 18 years ago by Michael Droettboom and originally authored by John D. Hunter, but still remains very popular among python learners and data analysts. It offers an object-oriented application interface that makes matplotlib plots easier to run on a variety of applications.<\/p>\n\n\n\n<p><strong>Let&#8217;s look at some codes to plot charts using matplotlib:<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.askpython.com\/python\/plot-customize-pie-chart-in-python\" data-type=\"post\" data-id=\"9342\">Line Chart<\/a><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nfrom numpy import random\n\nvar1=random.randint(100, size=(100))\nvar2=random.randint(100, size=(100))\nvar1.sort()\nvar2.sort()\n\nplt.plot(var1,var2)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-linechart.png-1024x512.png\" alt=\"matplotlib-linechart.png\" class=\"wp-image-25171\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-linechart.png-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-linechart.png-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-linechart.png-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-linechart.png.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Line chart<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.askpython.com\/python\/examples\/animated-histograms\" data-type=\"post\" data-id=\"22132\">Histogram<\/a><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom numpy import random\nhist_var = np.random.normal(170, 10, 250)\nplt.hist(hist_var)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-histogram.png-1024x512.png\" alt=\"matplotlib-histogram.png\" class=\"wp-image-25172\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-histogram.png-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-histogram.png-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-histogram.png-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/matplotlib-histogram.png.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Histogram<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2. Seaborn<\/h2>\n\n\n\n<p>It is a submodule built on matplotlib for creating graphs out of statistical data. Seaborn allows programmers to extract data directly from arrays and data frames and let them visualize a graph out of that statistical data. To allow visualizations, it works under the Matplotlib framework, and for data integration, it relies heavily on pandas. <\/p>\n\n\n\n<p>To understand how seaborn works, we will look into an example code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scatter<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pand\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\n\nscores = pand.read_csv(&#039;scores.csv&#039;, encoding=&#039;unicode_escape&#039;, index_col=0)\n\ndef scatter_plot():\n    sns.lmplot(x=&#039;Attack&#039;, y=&#039;Defense&#039;, data=scores,\n           fit_reg=False,  # It removes a diagonal line that remains by default\n           hue=&#039;Stage&#039;\n           )\n    plt.show()\n\nscatter_plot()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-scatter.png-1024x512.png\" alt=\"seaborn-scatter.png\" class=\"wp-image-25173\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-scatter.png-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-scatter.png-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-scatter.png-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-scatter.png.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Seaborn Scatter<\/figcaption><\/figure><\/div>\n\n\n\n<p>The code above plots a scatter chart of the attack and defense values we took from the data frame &#8211; &#8216;scores.csv&#8217;. The method      &#8216;scatter_plot( )&#8217; contains the seaborn function &#8216;sns.lmplot&#8217; which plots the scatter by taking &#8216;Attack&#8217; as the x-axis and &#8216;Defense&#8217; as y-axis.<\/p>\n\n\n\n<p>Let&#8217;s look at another example code. We will be plotting a boxplot by using seaborn, with the same set of values we used in the last example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.askpython.com\/python\/examples\/boxplots\" data-type=\"post\" data-id=\"10106\">Boxplot<\/a><\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pandas as pand\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\n\nscores = pand.read_csv(&#039;scores.csv&#039;, encoding=&#039;unicode_escape&#039;, index_col=0)\nsns.boxplot(data=scores)\nplt.show()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-boxplot.png-1024x512.png\" alt=\"Seaborn-boxplot.png\" class=\"wp-image-25174\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-boxplot.png-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-boxplot.png-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-boxplot.png-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/seaborn-boxplot.png.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Seaborn Boxplot<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Plotly<\/h2>\n\n\n\n<p>Plotly is a data visualization tool created in 2012. In this article, we will be learning about a sub-module of Plotly, known as <strong>Plotly Express<\/strong>. This sub-module is a Python library with the purpose to create graphic visualizations with a single function call. On the other hand, it also provides a good base to create custom-tailored graphics for media and communication.<\/p>\n\n\n\n<p>Let&#8217;s look at a Plotly code example demonstrating how to create simple charts through a single function call.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport plotly.express as px\n\ndef linechart():\n    df_india = px.data.gapminder().query(&quot;country==&#039;India&#039;&quot;)\n    fig = px.line(df_india, x=&quot;year&quot;, y=&quot;lifeExp&quot;, title=&#039;Average life span in India:&#039;)\n    fig.show()\n\ndef scatter():\n    # x and y given as array_like objects\n    import plotly.express as px\n    fig = px.scatter(x=&#x5B;5, 1, 3, 4, 3], y=&#x5B;1, 5, 4, 13, 19])\n    fig.show()\n\ndef barplot():\n    import plotly.express as px\n    data_Japan = px.data.gapminder().query(&quot;country == &#039;Japan&#039;&quot;)\n    fig = px.bar(data_Japan, x=&#039;year&#039;, y=&#039;pop&#039;)\n    fig.show()\n\nlinechart()\nbarplot()\nscatter()\n<\/pre><\/div>\n\n\n<p>In the code above, the program has three different method functions that are called together. Each method function when called plots a chart for the user. If we observe closely, each method function has a different input method. The first function loads data from a Plotly express database. The second function visualizes a scatter chart from values taken from two different arrays. The third function is similar to the first function, it loads data from the Plotly express database and then plots a bar chart.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-linechart-1024x512.png\" alt=\"Plotly-linechart.png\" class=\"wp-image-25176\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-linechart-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-linechart-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-linechart-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-linechart.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Plotly Line chart<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-barchart-1024x512.png\" alt=\"Plotly-barchart.png\" class=\"wp-image-25177\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-barchart-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-barchart-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-barchart-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-barchart.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Plotly Barchart<\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-scatterchart-1024x512.png\" alt=\"Plotly-scatter_chart.png\" class=\"wp-image-25178\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-scatterchart-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-scatterchart-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-scatterchart-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/plotly-scatterchart.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Plotly Scatter chart<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. Dash<\/h2>\n\n\n\n<p>Dash is a Plotly framework that allows us to make web applications and allows us to link graphics, texts, and controls together. This sub-module basically helps in managing various aspects of the application&#8217;s frontend such as its layout and styling. The final result is a flask application, which can be easily deployed to various web hosting platforms.<\/p>\n\n\n\n<p>Let&#8217;s look at a few of its codes to develop an understanding. The first program plots a life expectancy line chart from the Plotly gapminder database. It plots the life expectancy of all the countries present in the chosen continent.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Line Chart<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport dash\nfrom dash import dcc\nfrom dash import html\nfrom dash.dependencies import Input, Output\nimport plotly.express as px\n\nframe_data = px.data.gapminder()\nevery_continent = frame_data.continent.unique()\n\napp = dash.Dash(__name__)\n\napp.layout = html.Div(&#x5B;\n    dcc.Checklist(\n        id=&quot;checklist&quot;,\n        options=&#x5B;{&quot;label&quot;: x, &quot;value&quot;: x}\n                 for x in every_continent],\n        value=every_continent&#x5B;3:],\n        labelStyle={&#039;display&#039;: &#039;inline-block&#039;}\n    ),\n    dcc.Graph(id=&quot;lineChart&quot;),\n])\n\n@app.callback(\n    Output(&quot;lineChart&quot;, &quot;figure&quot;),\n    &#x5B;Input(&quot;checklist&quot;, &quot;value&quot;)])\ndef update_line_chart(continents):\n    data_mask = frame_data.continent.isin(continents)\n    figure = px.line(frame_data&#x5B;data_mask],\n        x=&quot;year&quot;, y=&quot;lifeExp&quot;, color=&#039;country&#039;)\n    return figure\n\napp.run_server(debug=True)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-linechart-1024x512.png\" alt=\"Dash-linechart.png\" class=\"wp-image-25179\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-linechart-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-linechart-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-linechart-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-linechart.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Dash &#8211; Line chart<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Scatter Chart<\/h3>\n\n\n\n<p>The code below demonstrates how a scatter chart can be plotted using dash in Python. Here, we used the iris database as our input data frame. The iris database is a pattern recognition dataset containing petal sizes of three different classes of flowers. This program will plot a scatter chart of the petal sizes of the data provided as input.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport dash\nfrom dash import dcc\nfrom dash import html\nfrom dash.dependencies import Input, Output\nimport plotly.express as px\n\nframe_data = px.data.iris()\n\napp = dash.Dash(__name__)\n\napp.layout = html.Div(&#x5B;\n    dcc.Graph(id=&quot;plotis_scatter&quot;),\n    html.P(&quot;Width of Petal:&quot;),\n    dcc.RangeSlider(\n        id=&#039;range-slider&#039;,\n        min=0, max=2.5, step=0.1,\n        marks={0: &#039;0&#039;, 2.5: &#039;2.5&#039;},\n        value=&#x5B;0.5, 2]\n    ),\n])\n\n@app.callback(\n    Output(&quot;plotis_scatter&quot;, &quot;figure&quot;),\n    &#x5B;Input(&quot;range-slider&quot;, &quot;value&quot;)])\ndef update_bar_chart(slider_range):\n    low, high = slider_range\n    damask = (frame_data&#x5B;&#039;petal_width&#039;] &gt; low) &amp; (frame_data&#x5B;&#039;petal_width&#039;] &lt; high)\n    figure = px.scatter(\n        frame_data&#x5B;damask], x=&quot;sepal_width&quot;, y=&quot;sepal_length&quot;,\n        color=&quot;species&quot;, size=&#039;petal_length&#039;,\n        hover_data=&#x5B;&#039;petal_width&#039;])\n    return figure\n\napp.run_server(debug=True)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"512\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-scatterchart-1024x512.png\" alt=\"Dash-scatter_chart.png\" class=\"wp-image-25180\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-scatterchart-1024x512.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-scatterchart-300x150.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-scatterchart-768x384.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/12\/dash-scatterchart.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Dash Scatter chart<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This article aimed to explain the important plotting tools available for Python. Though these python libraries are extensively used in the data science domain, we tried to provide the concepts and codes in an easy-to-learn manner so that even beginners can pick them up. It is hoped that this article has helped you to understand the basic concepts of all the libraries explained in this article &#8211; Matplotlib, Seaborn, Plotly, Dash. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python offers a lot of interactive plotting packages through which we can make some of the most beautiful and customizable graphs and charts available out there. In this article, we will be looking at some of the python modules that are used for plotting and how basic charts are coded with them. These are some [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":25352,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-25078","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/25078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=25078"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/25078\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/25352"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=25078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=25078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=25078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}