{"id":30097,"date":"2022-06-14T17:34:00","date_gmt":"2022-06-14T17:34:00","guid":{"rendered":"https:\/\/www.askpython.com\/?p=30097"},"modified":"2022-07-06T10:30:30","modified_gmt":"2022-07-06T10:30:30","slug":"comic-visualization-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/comic-visualization-python","title":{"rendered":"Implementing Comic Visualization Python"},"content":{"rendered":"\n<p>We all know that the average individual responds far better to visual information than to text alone. Typically, the graphs generated by Matplotlib are quite flawless but boring. Observing these graphs is not really entertaining. <\/p>\n\n\n\n<p>In this tutorial, we will strive to make conventional images more entertaining and humorous, using XKCD as an example.<\/p>\n\n\n\n<p>The webcomic xkcd was established by the American author Randall Munroe in 2005. xkcd is a weekly internet comic strip that is updated weekly. It is among the most popular comic books. We will here try to paint our plots in the style of xkcd comics!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Load Libraries and Data<\/h2>\n\n\n\n<p>We will begin by importing the necessary libraries and then proceed to import the data into the program. Here, we load the tips dataset and output its initial five rows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom seaborn import load_dataset\n\ntips = load_dataset(&quot;tips&quot;)\ntips.head()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"182\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/tips_dataset.png\" alt=\"Tips Dataset\" class=\"wp-image-30104\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/tips_dataset.png 393w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/tips_dataset-300x139.png 300w\" sizes=\"auto, (max-width: 393px) 100vw, 393px\" \/><figcaption>Tips Dataset<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1 &#8211; Bar Plots<\/h2>\n\n\n\n<p>A bar graph is a graphical representation of data in which categories can be highlighted with specific forms, such as a rectangle. The length and height of the bars in the bar chart show the distribution of data within the dataset.<\/p>\n\n\n\n<p><strong><em>Also Read: <a href=\"https:\/\/www.askpython.com\/python\/python-bar-plot\">Python Bar Plot \u2013 Visualize Categorical Data in Python<\/a><\/em><\/strong><\/p>\n\n\n\n<p>Using the sns.countplot function, which plots the counts of unique values in a column, we can generate a basic bar chart.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nplt.figure(facecolor=&quot;W&quot;,figsize=(10,5))\nplt.title(&quot;Bar Plot - Normal&quot;)\nsns.countplot(tips&#x5B;&#039;sex&#039;])\nplt.show()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"612\" height=\"333\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BarPlot-Normal.png\" alt=\"BarPlot Normal\" class=\"wp-image-30109\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BarPlot-Normal.png 612w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BarPlot-Normal-300x163.png 300w\" sizes=\"auto, (max-width: 612px) 100vw, 612px\" \/><figcaption>BarPlot Normal<\/figcaption><\/figure>\n\n\n\n<p>To make the plot comic, we need to add the plt.xkcd method before the entire code.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"618\" height=\"339\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BarPlot-Comic.png\" alt=\"BarPlot Comic\" class=\"wp-image-30110\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BarPlot-Comic.png 618w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BarPlot-Comic-300x165.png 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><figcaption>BarPlot Comic<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2: Box Plot<\/h2>\n\n\n\n<p>Boxplots are a measurement of the data set&#8217;s data distribution. It separates the data set into quartiles. This graph depicts the data set&#8217;s minimum, maximum, median, first quartile, and third quartile.<\/p>\n\n\n\n<p><strong><em>Also Read: <a href=\"https:\/\/www.askpython.com\/python\/examples\/boxplots\">Boxplots: Everything you need to know<\/a><\/em><\/strong><\/p>\n\n\n\n<p>Using the code below, we can generate a boxplot using the sns.boxplot function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nplt.figure(facecolor=&quot;W&quot;,figsize=(10,5))\nplt.title(&quot;BoxPlot - Normal&quot;)\nsns.boxplot(x = &quot;day&quot;, y = &quot;total_bill&quot;, hue = &quot;sex&quot;, data = tips);\nplt.show()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"333\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BoxPlot-Normal.png\" alt=\"BoxPlot Normal\" class=\"wp-image-30113\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BoxPlot-Normal.png 606w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BoxPlot-Normal-300x165.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><figcaption>BoxPlot Normal<\/figcaption><\/figure>\n\n\n\n<p>To make a plot comic, we need to add the plt.xkcd method before the entire code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nplt.xkcd()\nplt.figure(facecolor=&quot;W&quot;,figsize=(10,5))\nplt.title(&quot;BoxPlot - Comic&quot;)\nsns.boxplot(x = &quot;day&quot;, y = &quot;total_bill&quot;, hue = &quot;sex&quot;, data = tips);\nplt.show()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"615\" height=\"340\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BoxPlot-Comic.png\" alt=\"BoxPlot Comic\" class=\"wp-image-30116\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BoxPlot-Comic.png 615w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/BoxPlot-Comic-300x166.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><figcaption>BoxPlot Comic<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example 3 &#8211; Sine Wave<\/h2>\n\n\n\n<p>In this example, we will generate the values for a sine wave plot by utilizing the linspace and sin functions, as seen in the code below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nplt.figure(facecolor=&quot;W&quot;,figsize=(10,5))\nplt.plot(np.sin(np.linspace(0, 10)))\nplt.title(&#039;A simple Sine Wave - Normal&#039;)\nplt.show()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"610\" height=\"319\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Sine-Wave-Normal.png\" alt=\"Sine Wave Normal\" class=\"wp-image-30120\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Sine-Wave-Normal.png 610w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Sine-Wave-Normal-300x157.png 300w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><figcaption>Sine Wave Normal<\/figcaption><\/figure>\n\n\n\n<p>To make a plot comic, we need to add the plt.xkcd method before the entire code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nplt.xkcd()\nplt.figure(facecolor=&quot;W&quot;,figsize=(10,5))\nplt.plot(np.sin(np.linspace(0, 10)))\nplt.title(&#039;A simple Sine Wave - Comic&#039;)\nplt.show()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"618\" height=\"325\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Sine-Wave-Comic.png\" alt=\"Sine Wave Comic\" class=\"wp-image-30123\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Sine-Wave-Comic.png 618w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/Sine-Wave-Comic-300x158.png 300w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><figcaption>Sine Wave Comic<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example 4 &#8211; Pie Chart<\/h2>\n\n\n\n<p>Matplotlib&#8217;s pie() function supports pie charts. The plt.pie() method can be used to generate a plot. The following code generates a pie chart:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nlabels = &#039;Python&#039;, &#039;C++&#039;, &#039;Ruby&#039;, &#039;Java&#039;\nsizes = &#x5B;215, 130, 245, 210]\ncolors = &#x5B;&#039;gold&#039;, &#039;yellowgreen&#039;, &#039;lightcoral&#039;, &#039;lightskyblue&#039;]\nexplode = (0.1, 0, 0, 0)\nplt.figure(facecolor=&quot;W&quot;,figsize=(10,5))\nplt.pie(sizes, explode=explode, labels=labels, colors=colors,autopct=&#039;%1.1f%%&#039;)\nplt.axis(&#039;equal&#039;)\nplt.title(&quot;Pie Chart - Normal&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"302\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/PieChart-Normal.png\" alt=\"PieChart Normal\" class=\"wp-image-30127\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/PieChart-Normal.png 572w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/PieChart-Normal-300x158.png 300w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><figcaption>PieChart Normal<\/figcaption><\/figure>\n\n\n\n<p>To make plot comic, we need to add the plt.xkcd method before the entire code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nlabels = &#039;Python&#039;, &#039;C++&#039;, &#039;Ruby&#039;, &#039;Java&#039;\nsizes = &#x5B;215, 130, 245, 210]\ncolors = &#x5B;&#039;gold&#039;, &#039;yellowgreen&#039;, &#039;lightcoral&#039;, &#039;lightskyblue&#039;]\nexplode = (0.1, 0, 0, 0)\nplt.xkcd()\nplt.figure(facecolor=&quot;W&quot;,figsize=(10,5))\nplt.pie(sizes, explode=explode, labels=labels, colors=colors,autopct=&#039;%1.1f%%&#039;)\nplt.axis(&#039;equal&#039;)\nplt.title(&quot;Pie Chart - Comic&quot;)\nplt.show()\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"572\" height=\"303\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/PieChart-Comic.png\" alt=\"PieChart Comic\" class=\"wp-image-30130\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/PieChart-Comic.png 572w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/06\/PieChart-Comic-300x159.png 300w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><figcaption>PieChart Comic<\/figcaption><\/figure>\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>I hope you have enjoyed the tutorial from the boring standard visualizations to fun and interesting comic ones with the help of xkcd. I would also recommend you the following tutorials:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python-modules\/data-visualization-using-python-bokeh\">Data Visualization using Python Bokeh<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/animating-data-in-python\">Animating Data in Python \u2013 A Simple Guide<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/ascii-art\">ASCII Art in Python Programming Language<\/a><\/li><\/ol>\n\n\n\n<p>Thank you for reading! Happy Learning! &#x1f603;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all know that the average individual responds far better to visual information than to text alone. Typically, the graphs generated by Matplotlib are quite flawless but boring. Observing these graphs is not really entertaining. In this tutorial, we will strive to make conventional images more entertaining and humorous, using XKCD as an example. The [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":30134,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-30097","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\/30097","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\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=30097"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/30097\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/30134"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=30097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=30097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=30097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}