{"id":2101,"date":"2021-01-05T16:00:48","date_gmt":"2021-01-05T16:00:48","guid":{"rendered":"https:\/\/thenextweb.com\/?p=1333091"},"modified":"2021-01-05T16:00:48","modified_gmt":"2021-01-05T16:00:48","slug":"a-beginners-guide-to-data-visualization-with-python-and-seaborn","status":"publish","type":"post","link":"https:\/\/www.londonchiropracter.com\/?p=2101","title":{"rendered":"A beginner\u2019s guide to data visualization with Python and Seaborn"},"content":{"rendered":"\n<p>Data visualization is a technique that allows data scientists to convert raw data into charts and plots that generate valuable insights. Charts reduce the complexity of the data and make it easier to understand for any user.<\/p>\n<p>There are many tools to perform data visualization, such as Tableau, Power BI, ChartBlocks, and more, which are no-code tools. They are very powerful tools, and they have their audience. However, when working with raw data that requires transformation and a good playground for data, Python is an excellent choice.<\/p>\n<p>Though more complicated as it requires programming knowledge, Python allows you to perform any manipulation, transformation, and visualization of your data. It is ideal for data scientists.<\/p>\n<p>There are many reasons why Python is the best choice for data science, but one of the most important ones is its ecosystem of libraries. Many great libraries are available for Python to work with data like<span>&nbsp;<\/span><code>numpy<\/code>,<span>&nbsp;<\/span><code>pandas<\/code>,<span>&nbsp;<\/span><code>matplotlib<\/code>,<span>&nbsp;<\/span><code>tensorflow<\/code>.<\/p>\n<p><code>Matplotlib<\/code><span>&nbsp;<\/span>is probably the most recognized plotting library out there, available for Python and other programming languages like<span>&nbsp;<\/span><code>R<\/code>. It is its level of customization and operability that set it in the first place. However, some actions or customizations can be hard to deal with when using it.<\/p>\n<p>Developers created a new library based on matplotlib called<span>&nbsp;<\/span><code>seaborn<\/code>.<span>&nbsp;<\/span><code>Seaborn<\/code><span>&nbsp;<\/span>is as powerful as<span>&nbsp;<\/span><code>matplotlib<\/code><span>&nbsp;<\/span>while also providing an abstraction to simplify plots and bring some unique features.<\/p>\n<p>In this article, we will focus on how to work with Seaborn to create best-in-class plots. If you want to follow along you can create your own project or simply check out my<span>&nbsp;<\/span><a href=\"https:\/\/github.com\/bajcmartinez\/python-seaborn-guide\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">seaborn guide project<\/a><span>&nbsp;<\/span>on GitHub.<\/p>\n<h2 id=\"what-is-seaborn\">What is Seaborn?<\/h2>\n<p><a href=\"https:\/\/seaborn.pydata.org\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Seaborn<\/a><span>&nbsp;<\/span>is a library for making statistical graphics in Python. It builds on top of<span>&nbsp;<\/span><a href=\"https:\/\/matplotlib.org\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">matplotlib<\/a><span>&nbsp;<\/span>and integrates closely with<span>&nbsp;<\/span><a href=\"https:\/\/livecodestream.dev\/post\/how-to-work-with-pandas-in-python\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">pandas data structures<\/a><span>&nbsp;<\/span>.<\/p>\n<p>Seaborn design allows you to explore and understand your data quickly. Seaborn works by capturing entire data frames or arrays containing all your data and performing all the internal functions necessary for semantic mapping and statistical aggregation to convert data into informative plots.<\/p>\n<p>It abstracts complexity while allowing you to design your plots to your requirements.<\/p>\n<p><em>[Read:&nbsp;<span dir=\"auto\"><a class=\"c-link c-message_attachment__title_link\" href=\"https:\/\/thenextweb.com\/dutch-disruptors\/2020\/12\/15\/meet-the-4-scale-ups-using-data-to-save-the-planet\/\" target=\"_blank\" rel=\"noreferrer noopener\" data-qa=\"message_attachment_title_link\">Meet the 4 scale-ups using data to save the planet<\/a>]<\/span><\/em><\/p>\n<h2 id=\"installing-seaborn\">Installing Seaborn<\/h2>\n<p>Installing<span>&nbsp;<\/span><code>seaborn<\/code><span>&nbsp;<\/span>is as easy as installing one library using your favorite Python package manager. When installing<span>&nbsp;<\/span><code>seaborn<\/code>, the library will install its dependencies, including<span>&nbsp;<\/span><code>matplotlib<\/code>,<span>&nbsp;<\/span><code>pandas<\/code>,<span>&nbsp;<\/span><code>numpy<\/code>, and<span>&nbsp;<\/span><code>scipy<\/code>.<\/p>\n<p>Let\u2019s then install Seaborn, and of course, also the package<span>&nbsp;<\/span><a href=\"https:\/\/jupyter.org\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">notebook<\/a><span>&nbsp;<\/span>to get access to our data playground.<\/p>\n<div class=\"highlight\" readability=\"7\">\n<pre><code class=\"language-text\" data-lang=\"text\">pipenv install seaborn notebook <\/code><\/pre>\n<\/div>\n<p>Additionally, we are going to import a few modules before we get started.<\/p>\n<div class=\"highlight\" readability=\"7\">\n<pre><code class=\"language-python\" data-lang=\"python\"><span>import<\/span> seaborn <span>as<\/span> sns <span>import<\/span> pandas <span>as<\/span> pd <span>import<\/span> numpy <span>as<\/span> np <span>import<\/span> matplotlib <\/code><\/pre>\n<\/div>\n<h2 id=\"building-your-first-plots\">Building your first plots<\/h2>\n<p>Before we can start plotting anything, we need data. The beauty of<span>&nbsp;<\/span><code>seaborn<\/code><span>&nbsp;<\/span>is that it works directly with<span>&nbsp;<\/span><code>pandas<\/code><span>&nbsp;<\/span>dataframes, making it super convenient. Even more so, the library comes with some built-in datasets that you can now load from code, no need to manually downloading files.<\/p>\n<p>Let\u2019s see how that works by loading a dataset that contains information about flights.<\/p>\n<p><figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1333099 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.57.22.png\" alt width=\"737\" height=\"440\" sizes=\"(max-width: 737px) 100vw, 737px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.57.22.png 928w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.57.22-280x167.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.57.22-452x270.png 452w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.57.22-226x135.png 226w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.57.22-796x475.png 796w\"><\/figure>\n<\/p>\n<h3 id=\"scatter-plot\">Scatter Plot<\/h3>\n<p>A scatter plot is a diagram that displays points based on two dimensions of the dataset. Creating a scatter plot in the Seaborn library is so simple and with just one line of code.<\/p>\n<pre><code>sns.scatterplot(data=flights_data, x=\"year\", y=\"passengers\") <\/code><\/pre>\n<figure class data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/scatter_hu8c145c17714d63fee15d2a018c066908_16242_700x0_resize_box_2.png\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/scatter_hu8c145c17714d63fee15d2a018c066908_16242_700x0_resize_box_2.png\" alt width=\"643\" height=\"449\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Sample scatter plot\" data-title=\"Share Sample scatter plot on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Sample scatter plot on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Sample scatter plot<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>Very easy, right? The function<span>&nbsp;<\/span><code>scatterplot<\/code><span>&nbsp;<\/span>expects the dataset we want to plot and the columns representing the<span>&nbsp;<\/span><code>x<\/code><span>&nbsp;<\/span>and<span>&nbsp;<\/span><code>y<\/code><span>&nbsp;<\/span>axis.<\/p>\n<h3 id=\"line-plot\">Line Plot<\/h3>\n<p>This plot draws a line that represents the revolution of continuous or categorical data. It is a popular and known type of chart, and it\u2019s super easy to produce. Similarly to before, we use the function<span>&nbsp;<\/span><code>lineplot<\/code><span>&nbsp;<\/span>with the dataset and the columns representing the<span>&nbsp;<\/span><code>x<\/code><span>&nbsp;<\/span>and<span>&nbsp;<\/span><code>y<\/code><span>&nbsp;<\/span>axis.<span>&nbsp;<\/span><code>Seaborn<\/code><span>&nbsp;<\/span>will do the rest.<\/p>\n<div class=\"highlight\" readability=\"9\">\n<pre><code class=\"language-python\" data-lang=\"python\">sns<span>.<\/span>lineplot(data<span>=flights<\/span>_data, x<span>=<\/span><span>\"year\"<\/span>, y<span>=<\/span><span>\"passengers\"<\/span>) <\/code><\/pre>\n<\/div>\n<figure class data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/line_hu8d87bc669c476373da06acfb76abbc49_28570_700x0_resize_box_2.png\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/line_hu8d87bc669c476373da06acfb76abbc49_28570_700x0_resize_box_2.png\" alt width=\"700\" height=\"489\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Sample line plot\" data-title=\"Share Sample line plot on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Sample line plot on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Sample line plot<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<h3 id=\"bar-plot\">Bar Plot<\/h3>\n<p>It is probably the best-known type of chart, and as you may have predicted, we can plot this type of plot with<span>&nbsp;<\/span><code>seaborn<\/code><span>&nbsp;<\/span>in the same way we do for lines and scatter plots by using the function<span>&nbsp;<\/span><code>barplot<\/code>.<\/p>\n<div class=\"highlight\" readability=\"9\">\n<pre><code class=\"language-python\" data-lang=\"python\">sns<span>.<\/span>barplot(data<span>=flights<\/span>_data, x<span>=<\/span><span>\"year\"<\/span>, y<span>=<\/span><span>\"passengers\"<\/span>) <\/code><\/pre>\n<\/div>\n<figure class=\"lph\" data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/bar_hu582125c43761c6433dd9c8fa01845a48_8748_700x0_resize_box_2.png\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/bar_hu582125c43761c6433dd9c8fa01845a48_8748_700x0_resize_box_2.png\" alt width=\"700\" height=\"489\" class=\" lazy\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Sample bar plot\" data-title=\"Share Sample bar plot on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Sample bar plot on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Sample bar plot<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>It\u2019s very colorful, I know, we will learn how to customize it later on in the guide.<\/p>\n<h2 id=\"extending-with-matplotlib\">Extending with matplotlib<\/h2>\n<p>Seaborn builds on top of<span>&nbsp;<\/span><code>matplotlib<\/code>, extending its functionality and abstracting complexity. With that said, it does not limit its capabilities. Any<span>&nbsp;<\/span><code>seaborn<\/code><span>&nbsp;<\/span>chart can be customized using functions from the<span>&nbsp;<\/span><code>matplotlib<\/code><span>&nbsp;<\/span>library. It can come in handy for specific operations and allows seaborn to leverage the power of<span>&nbsp;<\/span><code>matplotlib<\/code><span>&nbsp;<\/span>without having to rewrite all its functions.<\/p>\n<p>Let\u2019s say that you, for example, want to plot multiple graphs simultaneously using<span>&nbsp;<\/span><code>seaborn<\/code>; then you could use the<span>&nbsp;<\/span><code>subplot<\/code><span>&nbsp;<\/span>function from<span>&nbsp;<\/span><code>matplotlib<\/code>.<\/p>\n<div class=\"highlight\" readability=\"14\">\n<pre><code class=\"language-python\" data-lang=\"python\">diamonds_data <span>=<\/span> sns<span>.<\/span>load_dataset(<span>'diamonds'<\/span>) plt<span>.<\/span>subplot(<span>1<\/span>, <span>2<\/span>, <span>1<\/span>) sns<span>.<\/span>countplot(x<span>=<\/span><span>'carat'<\/span>, data<span>=<\/span>diamonds_data) plt<span>.<\/span>subplot(<span>1<\/span>, <span>2<\/span>, <span>2<\/span>) sns<span>.<\/span>countplot(x<span>=<\/span><span>'depth'<\/span>, data<span>=<\/span>diamonds_data) <\/code><\/pre>\n<\/div>\n<figure class data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/extending_hudb3c7d41b2118b98fbd37c34ef916ea4_14342_700x0_resize_box_2.png\" readability=\"2\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/extending_hudb3c7d41b2118b98fbd37c34ef916ea4_14342_700x0_resize_box_2.png\" alt width=\"700\" height=\"478\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Sample plot with sub-plots\" data-title=\"Share Sample plot with sub-plots on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Sample plot with sub-plots on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Sample plot with sub-plots<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>Using the<span>&nbsp;<\/span><code>subplot<\/code><span>&nbsp;<\/span>function, we can draw more than one chart on a single plot. The function takes three parameters, the first is the number of rows, the second is the number of columns, and the last one is the plot number.<\/p>\n<p>We are rendering a<span>&nbsp;<\/span><code>seaborn<\/code><span>&nbsp;<\/span>chart in each subplot, mixing<span>&nbsp;<\/span><code>matplotlib<\/code><span>&nbsp;<\/span>with<span>&nbsp;<\/span><code>seaborn<\/code><span>&nbsp;<\/span>functions.<\/p>\n<h2 id=\"seaborn-loves-pandas\">Seaborn loves Pandas<\/h2>\n<p>We already talked about this, but<span>&nbsp;<\/span><code>seaborn<\/code><span>&nbsp;<\/span>loves<span>&nbsp;<\/span><code>pandas<\/code><span>&nbsp;<\/span>to such an extent that all its functions build on top of the<span>&nbsp;<\/span><code>pandas<\/code><span>&nbsp;<\/span>dataframe. So far, we saw examples of using<span>&nbsp;<\/span><code>seaborn<\/code><span>&nbsp;<\/span>with pre-loaded data, but what if we want to draw a plot from data we already have loaded using<span>&nbsp;<\/span><code>pandas<\/code>?<\/p>\n<div class=\"highlight\" readability=\"10\">\n<pre><code class=\"language-python\" data-lang=\"python\">drinks_df <span>=<\/span> pd<span>.<\/span>read_csv(<span>\"data\/drinks.csv\"<\/span>) sns<span>.<\/span>barplot(x<span>=<\/span><span>\"country\"<\/span>, y<span>=<\/span><span>\"beer_servings\"<\/span>, data<span>=<\/span>drinks_df) <\/code><\/pre>\n<\/div>\n<figure class data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/pandas_hu829852d1c87d1926dc22a5c412120685_15392_700x0_resize_box_2.png\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/pandas_hu829852d1c87d1926dc22a5c412120685_15392_700x0_resize_box_2.png\" alt width=\"700\" height=\"472\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Sample plot with pandas\" data-title=\"Share Sample plot with pandas on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Sample plot with pandas on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Sample plot with pandas<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<h2 id=\"making-beautiful-plots-with-styles\">Making beautiful plots with styles<\/h2>\n<p>Seaborn gives you the ability to change your graphs\u2019 interface, and it provides five different styles out of the box:<span>&nbsp;<\/span><strong>darkgrid<\/strong>,<span>&nbsp;<\/span><strong>whitegrid<\/strong>,<span>&nbsp;<\/span><strong>dark<\/strong>,<span>&nbsp;<\/span><strong>white<\/strong>, and<span>&nbsp;<\/span><strong>ticks<\/strong>.<\/p>\n<div class=\"highlight\" readability=\"9\">\n<pre><code class=\"language-python\" data-lang=\"python\">sns<span>.<\/span>set_style(<span>\"darkgrid\"<\/span>) sns<span>.<\/span>lineplot(data <span>=<\/span> data, x <span>=<\/span> <span>\"year\"<\/span>, y <span>=<\/span> <span>\"passengers\"<\/span>) <\/code><\/pre>\n<\/div>\n<figure class=\"lph\" data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/style-darkgrid_hucf45549107692f6d02507c3af4af595c_30522_700x0_resize_box_2.png\" readability=\"2\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/style-darkgrid_hucf45549107692f6d02507c3af4af595c_30522_700x0_resize_box_2.png\" alt width=\"700\" height=\"489\" class=\" lazy\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Sample plot with darkgrid style\" data-title=\"Share Sample plot with darkgrid style on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Sample plot with darkgrid style on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Sample plot with darkgrid style<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>Here is another example<\/p>\n<div class=\"highlight\" readability=\"9\">\n<pre><code class=\"language-python\" data-lang=\"python\">sns<span>.<\/span>set_style(<span>\"whitegrid\"<\/span>) sns<span>.<\/span>lineplot(data<span>=flights<\/span>_data, x<span>=<\/span><span>\"year\"<\/span>, y<span>=<\/span><span>\"passengers\"<\/span>) <\/code><\/pre>\n<\/div>\n<figure class data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/style-whitegrid_huee49c3cd5b65b1c39ac9e88d98bb041f_31252_700x0_resize_box_2.png\" readability=\"2\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/style-whitegrid_huee49c3cd5b65b1c39ac9e88d98bb041f_31252_700x0_resize_box_2.png\" alt width=\"700\" height=\"489\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Sample plot with whitegrid style\" data-title=\"Share Sample plot with whitegrid style on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Sample plot with whitegrid style on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Sample plot with whitegrid style<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<h2 id=\"cool-use-cases\">Cool use cases<\/h2>\n<p>We know the basics of<span>&nbsp;<\/span><code>seaborn<\/code>, now let\u2019s get them into practice by building multiple charts over the same dataset. In our case, we will use the dataset \u201ctips\u201d that you can download directly using<span>&nbsp;<\/span><code>seaborn<\/code>.<\/p>\n<p>First, load the dataset.<\/p>\n<p><figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1333095 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.51.24.png\" alt width=\"778\" height=\"476\" sizes=\"(max-width: 778px) 100vw, 778px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.51.24.png 936w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.51.24-280x171.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.51.24-442x270.png 442w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.51.24-221x135.png 221w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.51.24-796x486.png 796w\"><\/figure>\n<\/p>\n<div class=\"highlight\" readability=\"10.009646302251\">\n<pre><span>I like to print the first few rows of the data set to get a feeling of the columns and the data itself. Usually, I use some<\/span><span>&nbsp;<\/span><span><\/span><code>pandas<\/code><span>&nbsp;<\/span><span>functions to fix some data issues like<\/span><span>&nbsp;<\/span><span><\/span><code>null<\/code><span>&nbsp;<\/span><span>values and add information to the data set that may be helpful. You can read more about this on the<\/span><span>&nbsp;<\/span><span><\/span><a href=\"https:\/\/livecodestream.dev\/post\/how-to-work-with-pandas-in-python\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">guide to working with pandas<\/a><span>&nbsp;<\/span><span>.<\/span><\/pre>\n<\/div>\n<p>Let\u2019s create an additional column to the data set with the percentage that represents the tip amount over the total of the bill.<\/p>\n<p><span><\/p>\n<figure class=\"post-image post-mediaBleed alignnone\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-1333096 lazy\" src=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.52.03.png\" alt width=\"811\" height=\"565\" sizes=\"(max-width: 811px) 100vw, 811px\" data-lazy=\"true\" data-srcset=\"https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.52.03.png 970w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.52.03-280x195.png 280w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.52.03-387x270.png 387w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.52.03-194x135.png 194w, https:\/\/cdn0.tnwcdn.com\/wp-content\/blogs.dir\/1\/files\/2021\/01\/Screenshot-2021-01-05-at-13.52.03-796x555.png 796w\"><\/figure>\n<p><\/span><\/p>\n<p>Next, we can start plotting some charts.<\/p>\n<h3 id=\"understanding-tip-percentages\">Understanding tip percentages<\/h3>\n<p>Let\u2019s try first to understand the tip percentage distribution. For that, we can use<span>&nbsp;<\/span><code>histplot<\/code><span>&nbsp;<\/span>that will generate a histogram chart.<\/p>\n<div class=\"highlight\" readability=\"8\">\n<pre><code class=\"language-python\" data-lang=\"python\">sns<span>.<\/span>histplot(tips_df[<span>\"tip_percentage\"<\/span>], binwidth<span>=<\/span><span>0.05<\/span>) <\/code><\/pre>\n<\/div>\n<figure class data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/tip-percentages_hu2927fbe982b4f0d35b701c3d310cf773_8956_700x0_resize_box_2.png\" readability=\"2\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/tip-percentages_hu2927fbe982b4f0d35b701c3d310cf773_8956_700x0_resize_box_2.png\" alt width=\"700\" height=\"490\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Understanding tip percentages plot\" data-title=\"Share Understanding tip percentages plot on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Understanding tip percentages plot on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Understanding tip percentages plot<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>That\u2019s good, we had to customize the<span>&nbsp;<\/span><code>binwidth<\/code><span>&nbsp;<\/span>property to make it more readable, but now we can quickly appreciate our understanding of the data. Most customers would tip between 15 to 20%, and we have some edge cases where the tip is over 70%. Those values are anomalies, and they are always worth exploring to determine if the values are errors or not.<\/p>\n<p>It would also be interesting to know if the tip percentage changes depending on the moment of the day,<\/p>\n<div class=\"highlight\" readability=\"10\">\n<pre><code class=\"language-python\" data-lang=\"python\">sns<span>.<\/span>histplot(data<span>=<\/span>tips_df, x<span>=<\/span><span>\"tip_percentage\"<\/span>, binwidth<span>=<\/span><span>0.05<\/span>, hue<span>=<\/span><span>\"time\"<\/span>) <\/code><\/pre>\n<\/div>\n<figure class data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/hue_hu391f91c0b2935fe091908e84631efd90_11559_700x0_resize_box_2.png\" readability=\"2\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/hue_hu391f91c0b2935fe091908e84631efd90_11559_700x0_resize_box_2.png\" alt width=\"700\" height=\"495\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Understanding tip percentages by time plot\" data-title=\"Share Understanding tip percentages by time plot on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Understanding tip percentages by time plot on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Understanding tip percentages by time plot<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>This time we loaded the chart with the full dataset instead of just one column, and then we set the property<span>&nbsp;<\/span><code>hue<\/code><span>&nbsp;<\/span>to the column<span>&nbsp;<\/span><code>time<\/code>. This will force the chart to use different colors for each value of<span>&nbsp;<\/span><code>time<\/code><span>&nbsp;<\/span>and add a legend to it.<\/p>\n<h3 id=\"total-of-tips-per-day-of-the-week\">Total of tips per day of the week<\/h3>\n<p>Another interesting metric is to know how much money in tips can the personnel expect depending on the day of the week.<\/p>\n<div class=\"highlight\" readability=\"10\">\n<pre><code class=\"language-python\" data-lang=\"python\">sns<span>.<\/span>barplot(data<span>=<\/span>tips_df, x<span>=<\/span><span>\"day\"<\/span>, y<span>=<\/span><span>\"tip\"<\/span>, estimator<span>=<\/span>np<span>.<\/span>sum) <\/code><\/pre>\n<\/div>\n<figure class data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/tips_day_huec708a051e2aeb706158119271e2ac7f_8130_700x0_resize_box_2.png\" readability=\"2\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/tips_day_huec708a051e2aeb706158119271e2ac7f_8130_700x0_resize_box_2.png\" alt width=\"700\" height=\"489\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Understanding tip percentages per day plot\" data-title=\"Share Understanding tip percentages per day plot on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Understanding tip percentages per day plot on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Understanding tip percentages per day plot<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<p>It looks like Friday is a good day to stay home.<\/p>\n<h3 id=\"impact-of-table-size-and-day-on-the-tip\">Impact of table size and day on the tip<\/h3>\n<p>Sometimes we want to understand how to variables play together to determine output. For example, how do the day of the week and the table size impact the tip percentage?<\/p>\n<p>To draw the next chart we will combine the<span>&nbsp;<\/span><code>pivot<\/code><span>&nbsp;<\/span>function of pandas to pre-process the information and then draw a heatmap chart.<\/p>\n<div class=\"highlight\" readability=\"11\">\n<pre><code class=\"language-python\" data-lang=\"python\">pivot <span>=<\/span> tips_df<span>.<\/span>pivot_table( index<span>=<\/span>[<span>\"day\"<\/span>], columns<span>=<\/span>[<span>\"size\"<\/span>], values<span>=<\/span><span>\"tip_percentage\"<\/span>, aggfunc<span>=<\/span>np<span>.<\/span>average) sns<span>.<\/span>heatmap(pivot) <\/code><\/pre>\n<\/div>\n<figure class data-src=\"\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/tips_day_size_hu15f48281e45815606a33eb3c6ccd5f81_10084_700x0_resize_box_2.png\" readability=\"2\">\n<p><figure class=\"post-image post-mediaBleed aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"lazy loaded lazy\" src=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/tips_day_size_hu15f48281e45815606a33eb3c6ccd5f81_10084_700x0_resize_box_2.png\" alt width=\"700\" height=\"530\" data-lazy=\"true\"><figcaption><a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/#\" data-url=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fthenextweb.com%2Fsyndication%2F2021%2F01%2F05%2Fa-beginners-guide-to-data-visualization-with-python-and-seaborn%2F&amp;via=thenextweb&amp;related=thenextweb&amp;text=Check out this picture on: Understanding tip percentages per day and table size plot\" data-title=\"Share Understanding tip percentages per day and table size plot on Twitter\" data-width=\"685\" data-height=\"500\" class=\"post-image-share popitup\" title=\"Share Understanding tip percentages per day and table size plot on Twitter\"><i class=\"icon icon--inline icon--twitter--dark\"><\/i><\/a>Understanding tip percentages per day and table size plot<\/figcaption><\/figure>\n<\/p>\n<\/figure>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Of course, there\u2019s much more we can do with<span>&nbsp;<\/span><code>seaborn<\/code>, and you can learn more use cases by visiting the<span>&nbsp;<\/span><a href=\"https:\/\/seaborn.pydata.org\/examples\/index.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">official documentation<\/a>. I hope that you enjoyed this article as much as I enjoyed writing it.<\/p>\n<hr>\n<p><i><span>This <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/post\/how-to-build-beautiful-plots-with-python-and-seaborn\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>article<\/span><\/i><\/a><i><span> was originally published on <\/span><\/i><a href=\"https:\/\/livecodestream.dev\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> by <\/span><\/i><a href=\"https:\/\/www.linkedin.com\/in\/bajcmartinez\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Juan Cruz Martinez<\/span><\/i><\/a><i><span> (twitter: <\/span><\/i><a href=\"https:\/\/twitter.com\/bajcmartinez\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>@bajcmartinez<\/span><\/i><\/a><i><span>), founder and publisher of Live Code Stream, entrepreneur, developer, author, speaker, and doer of things.<\/span><\/i><\/p>\n<p><a href=\"https:\/\/livecodestream.dev\/subscribe\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><i><span>Live Code Stream<\/span><\/i><\/a><i><span> is also available as a free weekly newsletter. Sign up for updates on everything related to programming, AI, and computer science in general.<\/span><\/i><\/p>\n<p> <a href=\"https:\/\/thenextweb.com\/syndication\/2021\/01\/05\/a-beginners-guide-to-data-visualization-with-python-and-seaborn\/\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data visualization is a technique that allows data scientists to convert raw data into charts and plots that generate valuable insights. Charts reduce the complexity of the data and make it easier&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2102,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/2101"}],"collection":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2101"}],"version-history":[{"count":0,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/posts\/2101\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=\/wp\/v2\/media\/2102"}],"wp:attachment":[{"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2101"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2101"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.londonchiropracter.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2101"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}