{"id":17,"date":"2019-10-26T14:29:00","date_gmt":"2019-10-26T12:29:00","guid":{"rendered":"https:\/\/blog.nubisoft.pl\/?p=17"},"modified":"2020-01-17T12:52:03","modified_gmt":"2020-01-17T11:52:03","slug":"how-to-make-beautiful-offline-plots-in-a-few-lines-of-code-in-python","status":"publish","type":"post","link":"https:\/\/nubisoft.io\/blog\/how-to-make-beautiful-offline-plots-in-a-few-lines-of-code-in-python\/","title":{"rendered":"How to make beautiful offline plots in a few lines of code in Python"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">We all love watching beautiful and self-understanding plots instead of a bunch of data presented in tabular forms. Also, we want to present our data as plots to be more convincing.<\/h2>\n\n\n\n<p>There are numerous platforms and tools to help us with this job, starting from common office suits and ending with dedicated software packages or online platforms. Many of you are familiar already with <a href=\"https:\/\/plot.ly\/\">plotly <\/a>which is Python implementation of the data visualization software built on the top of splendid<a href=\"https:\/\/d3js.org\/\"> D3.js<\/a> library. But using bare plotly requires a lot of effort (counted in lines of code) to get expected results. Salvation brings here the <a href=\"https:\/\/pypi.org\/project\/cufflinks\/\">cufflinks library<\/a> which is a bridge between plotly and <a href=\"https:\/\/pandas.pydata.org\/\">pandas dataframes<\/a> &#8211; probably the most often used datasource in the Python ecosystem. This explosive mixture can be used simply, without the need for setting up any complex development environments, with the help of jupiter notebooks. So let&#8217;s start without delay\u2026<\/p>\n\n\n\n<p>First, let&#8217;s check if we have Python3 binaries installed. Python3 is preinstalled in a desktop distribution of Ubuntu 18.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ python3 --version\nPython 3.6.8<\/code><\/pre>\n\n\n\n<p>Then we have to install pip3 &#8211; a package manager for Python3 packages.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt install python3-pip<\/code><\/pre>\n\n\n\n<p>To keep the python configuration clean, we will install jupyter in an isolated environment, so we have to install virtualenv package first.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo -H pip3 install --upgrade pip\nsudo -H pip3 install virtualenv<\/code><\/pre>\n\n\n\n<p>Let&#8217;s create a folder dedicated for jupyter setup, create a virtual environment for it and activate this virtual environment.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mkdir ~\/jupyter\n$ cd ~\/jupyter\/\n$ virtualenv .\n$ source .\/bin\/activate<\/code><\/pre>\n\n\n\n<p>Now, we are ready to install jupyter.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install jupyter<\/code><\/pre>\n\n\n\n<p>After that, we need to install our datavis components.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install plotly\npip install cufflinks<\/code><\/pre>\n\n\n\n<p>To start jupyter notebooks we have just to type the command below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>(jupyter) user@ubuntu18:~\/jupyter$jupyter notebook<\/code><\/pre>\n\n\n\n<p>The application will start and open the web browser window.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"353\" src=\"https:\/\/blog.nubisoft.pl\/wp-content\/uploads\/2019\/11\/cl-jupyter-1024x353.png\" alt=\"\" class=\"wp-image-15\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-jupyter-1024x353.png 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-jupyter-300x103.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-jupyter-768x264.png 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-jupyter-1536x529.png 1536w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-jupyter-2048x705.png 2048w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-jupyter-1200x413.png 1200w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-jupyter-1980x682.png 1980w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Worth to know that recently plotly moves toward online services named Chart Studio. To be able to draw offline without a need to set up an account on Chart Studio platform we have to use plotly.offline and explicitly switch to offline mode, which is presented below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import plotly.offline as py\nimport cufflinks as cf\nimport pandas as pd\nimport numpy as np\nprint (cf.version)\n\ncf.go_offline();<\/code><\/pre>\n\n\n\n<p>Now, we are able to compare the traditional way of using plotly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df = cf.datagen.lines()\n\npy.iplot([{\n    'x': df.index,\n    'y': df[col],\n    'name': col\n}  for col in df.columns], filename='draw')<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"618\" src=\"https:\/\/blog.nubisoft.pl\/wp-content\/uploads\/2019\/11\/cl-bareplotly-1024x618.png\" alt=\"\" class=\"wp-image-16\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-bareplotly-1024x618.png 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-bareplotly-300x181.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-bareplotly-768x464.png 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-bareplotly-1536x927.png 1536w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-bareplotly-1200x724.png 1200w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-bareplotly.png 1910w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>And the usage with the help of cufflinks leading to exactly the same results.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>df.iplot(kind='scatter', filename='draw')<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"550\" src=\"https:\/\/blog.nubisoft.pl\/wp-content\/uploads\/2019\/11\/cl-cufflinks-1024x550.png\" alt=\"\" class=\"wp-image-14\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-cufflinks-1024x550.png 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-cufflinks-300x161.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-cufflinks-768x413.png 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-cufflinks-1536x825.png 1536w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-cufflinks-1200x645.png 1200w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2019\/11\/cl-cufflinks.png 1930w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Isn&#8217;t it lovely?<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Another view angles<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>  In addition to the one described above (<a href=\"https:\/\/plot.ly\/\">plotly<\/a>), there are many other libraries for data visualization for the python ecosystem. At least <a href=\"https:\/\/matplotlib.org\/\">Matplotlib<\/a>, <a href=\"https:\/\/seaborn.pydata.org\/\">Seaborn <\/a>(based on the former one), <a href=\"https:\/\/pypi.org\/project\/ggplot\/\">GGplot <\/a>and <a href=\"https:\/\/bokeh.org\">Bokeh <\/a>should be mentioned here. However, plotly is basing on a famous and powerful D3.js library and also offers rich online services, which make it a first-class gamer.<\/li><li>There exists also &#8216;more native&#8217; data binder for the plotly library &#8211; <a href=\"https:\/\/plot.ly\/python\/plotly-express\/\">Plotly Express<\/a>, but at least for now it is much less powerful.<\/li><li> It should also be noted that using cufflinks we do not have access to all the possibilities offered by plotly, for example, we will not be able to create a <a href=\"https:\/\/plot.ly\/python\/sunburst-charts\/\">sunburst chart<\/a>.<\/li><li> And finally, it is worth mentioning that there is a possibility of even <a href=\"https:\/\/hub.docker.com\/r\/jupyter\/datascience-notebook\/\">simpler installation<\/a> of jupiter notebooks &#8211; I mean containerization technology, namely the docker platform. <\/li><\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all love watching beautiful and self-understanding plots instead of a bunch of data presented in tabular forms. Also, we want to present our data as plots to be more convincing. There are numerous platforms and tools to help us with this job, starting from common office suits and ending with dedicated software packages or [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":18,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_case_study_excerpt":"","footnotes":""},"categories":[2,3],"tags":[10,8,11,9,7],"class_list":["post-17","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","category-software-development","tag-cufflinks","tag-datavis","tag-jupyter","tag-plotly","tag-python"],"_links":{"self":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":6,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":195,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/17\/revisions\/195"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/media\/18"}],"wp:attachment":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}