{"id":36905,"date":"2022-11-19T09:41:29","date_gmt":"2022-11-19T09:41:29","guid":{"rendered":"https:\/\/www.askpython.com\/?p=36905"},"modified":"2022-11-19T09:41:30","modified_gmt":"2022-11-19T09:41:30","slug":"data-visualization-using-streamlit","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/data-visualization-using-streamlit","title":{"rendered":"Data Visualization using Streamlit &#8211; A Complete Guide"},"content":{"rendered":"\n<p>Data visualisation is the representation of data in a graphical format. It helps us to understand the data better and in an easier way. Data visualisation can be performed with the help of visual elements like charts, graphs, maps, etc.<\/p>\n\n\n\n<p>In this tutorial, we will learn how to plot different charts using Streamlit. So let us begin!<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/introduction-to-streamlit\" data-type=\"post\" data-id=\"36143\">An Introduction to Streamlit<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Line Chart in Streamlit<\/h2>\n\n\n\n<p>It is a graphical representation of information performed by connecting that connects a series of data points as a continuous line. It is one of the simplest forms of data visualization techniques.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport streamlit as st\nimport pandas as pd\n\nst.header(&quot;Line Chart&quot;)\n\ndata = {&quot;a&quot;:&#x5B;23, 12, 78, 4, 54], &quot;b&quot;:&#x5B;0, 13 ,88, 1, 3], \n&quot;c&quot;:&#x5B;45, 2, 546, 67, 56]}\n\ndf = pd.DataFrame(data)\ndf\nst.line_chart(data=df)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"990\" height=\"726\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-line-chart.png\" alt=\"Streamlit Line Chart\" class=\"wp-image-36912\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-line-chart.png 990w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-line-chart-300x220.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-line-chart-768x563.png 768w\" sizes=\"auto, (max-width: 990px) 100vw, 990px\" \/><figcaption class=\"wp-element-caption\">Streamlit Line Chart<\/figcaption><\/figure>\n\n\n\n<p>Here, we have taken some arbitrary data and created a data frame first using the <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\" target=\"_blank\" rel=\"noreferrer noopener\">Pandas<\/a> library and then plotted the chart using the <code>line_chart<\/code> function. We have also displayed the data frame before plotting the graph for quick reference.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/calculator-app-using-streamlit\" data-type=\"post\" data-id=\"36285\">Calculator App using Streamlit \u2013 Easy Step-by-Step Method<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Bar Chart in Streamlit<\/h2>\n\n\n\n<p>A bar chart represents data in the form of vertical or horizontal rectangular bars. The height of each bar is proportional to the value that it represents. Streamlit provide a <code>bar_chart<\/code> function to plot bar charts for data visualization.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport streamlit as st\nimport pandas as pd\n\nst.header(&quot;Bar Chart&quot;)\n\ndata = {&quot;a&quot;:&#x5B;23, 12, 78, 4, 54], &quot;b&quot;:&#x5B;0, 13 ,88, 1, 3], \n&quot;c&quot;:&#x5B;45, 2, 546, 67, 56]}\n\ndf = pd.DataFrame(data)\ndf\nst.bar_chart(data=df)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"980\" height=\"722\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-bar-chart.png\" alt=\"Streamlit Bar Chart\" class=\"wp-image-36911\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-bar-chart.png 980w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-bar-chart-300x221.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-bar-chart-768x566.png 768w\" sizes=\"auto, (max-width: 980px) 100vw, 980px\" \/><figcaption class=\"wp-element-caption\">Streamlit Bar Chart<\/figcaption><\/figure>\n\n\n\n<p>Since the data frame contains 5 rows, there are bars corresponding to each row in the output. These bars contain the distribution of each column&#8217;s value in that specific row and are denoted by different colours for each column. <\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/machine-learning-model-streamlit-house-price-prediction-gui\" data-type=\"post\" data-id=\"36774\">Deploying a Machine Learning Model using Streamlit \u2013 House Price Prediction GUI<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Area Chart in Streamlit<\/h2>\n\n\n\n<p>An area chart is a combination of both a line chart and a bar chart. The data points are plotted and then connected by lines. Then, the area below the line is coloured. Similarly, the other values are also plotted and the area is then coloured resulting in a layered chart. We can plot such a chart using the <code>area_chart<\/code> function from Streamlit.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport streamlit as st\nimport pandas as pd\n\nst.header(&quot;Area Chart&quot;)\n\ndata = {&quot;a&quot;:&#x5B;23, 12, 78, 4, 54], &quot;b&quot;:&#x5B;0, 13 ,88, 51, 3], \n&quot;c&quot;:&#x5B;45, 2, 46, 67, 56]}\n\ndf = pd.DataFrame(data)\ndf\nst.area_chart(df)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"970\" height=\"713\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-area-chart.png\" alt=\"Streamlit Area Chart\" class=\"wp-image-36910\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-area-chart.png 970w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-area-chart-300x221.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-area-chart-768x565.png 768w\" sizes=\"auto, (max-width: 970px) 100vw, 970px\" \/><figcaption class=\"wp-element-caption\">Streamlit Area Chart<\/figcaption><\/figure>\n\n\n\n<p>Here the area of each column in the data frame is represented by a different colour as can be seen in the figure above. Some of the areas overlap as well depending on the data provided.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/streamlit-theming\" data-type=\"post\" data-id=\"36217\">Theming in Streamlit \u2013 2 Methods to Customize The Look and Feel of Streamlit apps<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Maps in Streamlit<\/h2>\n\n\n\n<p>We can also display maps in a Streamlit web app as a part of data visualization. The <code>map<\/code> function in Streamlit helps you display a map with data points on it. We can specify the data using the column names <strong>&#8220;lat&#8221;<\/strong> or <strong>&#8220;latitude&#8221;<\/strong> to denote the latitude and <strong>&#8220;lon&#8221;<\/strong> or <strong>&#8220;longitude&#8221;<\/strong> to denote the longitude. It is necessary to add these columns in the data to be plotted to plot the map.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport streamlit as st\nimport pandas as pd\n\nst.header(&quot;Map&quot;)\n\ndata = {&quot;lat&quot;:&#x5B;28.704060, 28.459497, 13.082680, 17.385044, 23.0225],\n        &quot;lon&quot;:&#x5B;77.102493, 77.026634, 80.270721, 78.486671, 72.5714],\n        &quot;City&quot;: &#x5B;&quot;Delhi&quot;, &quot;Gurgaon&quot;, &quot;Chennai&quot;, &quot;Hyderabad&quot;, &quot;Ahemdabad&quot;]}\n\ndf = pd.DataFrame(data)\ndf\nst.map(data=df)\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"252\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-map-data.png\" alt=\"Streamlit Map Data\" class=\"wp-image-36913\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-map-data.png 402w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-map-data-300x188.png 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><figcaption class=\"wp-element-caption\">Streamlit Map Data<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1016\" height=\"790\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-map.png\" alt=\"Streamlit Map\" class=\"wp-image-36914\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-map.png 1016w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-map-300x233.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/11\/streamlit-map-768x597.png 768w\" sizes=\"auto, (max-width: 1016px) 100vw, 1016px\" \/><figcaption class=\"wp-element-caption\">Streamlit Map<\/figcaption><\/figure>\n\n\n\n<p>The data in this example contains locations of some cities in India as displayed in the figure above. The map denotes these specified cities by a red dot. Here, the maps can be zoomed in and out as well when we run the Streamlit app.<\/p>\n\n\n\n<p>Note that Streamlit allows us to save these charts as &#8216;SVG&#8217; or &#8216;PNG&#8217; and edit them using the Vega editor. Also, all the visual elements can be zoomed in and out as well.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>That&#8217;s all! We have learnt how to perform data visualization using Python and Streamlit. Another way of data visualization using Streamlit is to use Python libraries like <a href=\"https:\/\/www.askpython.com\/python-modules\/matplotlib\" target=\"_blank\" rel=\"noreferrer noopener\">Matplotlib<\/a>, <a href=\"https:\/\/www.askpython.com\/python-modules\/python-plotly-tutorial\" target=\"_blank\" rel=\"noreferrer noopener\">Plotly<\/a>, <a href=\"https:\/\/www.askpython.com\/python-modules\/data-visualization-using-python-bokeh\" target=\"_blank\" rel=\"noreferrer noopener\">Bokeh<\/a>, etc. You can check out our tutorials on the same for learning about them.  <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.streamlit.io\/library\/api-reference\/charts\" target=\"_blank\" rel=\"noreferrer noopener\">Streamlit Official Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Data visualisation is the representation of data in a graphical format. It helps us to understand the data better and in an easier way. Data visualisation can be performed with the help of visual elements like charts, graphs, maps, etc. In this tutorial, we will learn how to plot different charts using Streamlit. So let [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":36915,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-36905","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\/36905","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\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=36905"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36905\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/36915"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=36905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=36905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=36905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}