{"id":7043,"date":"2020-07-20T16:18:29","date_gmt":"2020-07-20T16:18:29","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7043"},"modified":"2020-07-20T16:18:43","modified_gmt":"2020-07-20T16:18:43","slug":"data-visualization-using-python-bokeh","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/data-visualization-using-python-bokeh","title":{"rendered":"Data Visualization using Python Bokeh"},"content":{"rendered":"\n<p>In this article, we will be looking into data visualization using Python Bokeh. <\/p>\n\n\n\n<p>Bokeh allows users to take in data in any format such as CSV, JSON, hard-coded data, or databases. We can create scatter plots, line charts, etc using this library. It is widely used for stock market analysis in the industry because it is very easy to integrate this library with different web frameworks as well such as JS, Django, and HTML.<\/p>\n\n\n\n<p>Keep reading this article to get some insights on the usage of Bokeh<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Features of Python Bokeh<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Interactive<\/strong>: Bokeh is a very interactive library that provides the functionality of interactivity to the graphs in addition to static plots.<\/li><li><strong>Powerful<\/strong>: Bokeh is a powerful library because it allows the addition of JavaScript for use-cases.<\/li><li><strong>Portable<\/strong>: The output of the Bokeh charts can be rendered on any web framework such as Django and Python and also on Jupyter Notebooks.<\/li><li><strong>Flexible<\/strong>: Easy to plot custom and complex use cases.<\/li><li>Interaction with other popular tools: Allows easy interaction with pydata tools such as Pandas and Jupyter notebook.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1320\" height=\"628\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Bokeh-is-interactive-2.gif\" alt=\"Bokeh Is Interactive 2\" class=\"wp-image-7123\"\/><figcaption>Bokeh Is Interactive<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Starting Out With Data Visualization using Python Bokeh<\/h2>\n\n\n\n<p>Whenever we do anything with python, it is a good practice to create a virtual environment and the best way to do is by running the command <code>pip  install pipenv<\/code>. Once you run this command, you will have access to the <code>pipenv<\/code> command and you can run the <code>pipenv shell<\/code>. This ensures that the virtual environment is setup.<\/p>\n\n\n\n<p>Now you can use the virtual environment to install Bokeh and <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" class=\"rank-math-link\">Python pandas<\/a>. You can use the command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npipenv install bokeh pandas\n<\/pre><\/div>\n\n\n<p> We will be using pandas because this library allows us to read the CSV file as a dataframe.  <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Plotting a line Graph <\/h3>\n\n\n\n<p>Before going into plotting charts from the CSV file, we will walk you through the process of plotting a simple line chart to get you familiarized with Bokeh.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom bokeh.plotting import figure, output_file, show\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\"><li><code>figure<\/code> module will help users create plots.<\/li><li><code>output_file<\/code> will define the name of the HTML file to generate.<\/li><li><code>show<\/code> module will generate and show the HTML file.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx=&#x5B;1,2,3,4,5]\ny=&#x5B;4,3,3,5,4]\n<\/pre><\/div>\n\n\n<p>For data we just create it as 2 lists -[1,2,3,4,5] and [4,3,3,5,4]. <\/p>\n\n\n\n<p>These are points such as (1,4), (2,3), (3,3), and so on. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\noutput_file(&#039;index.html&#039;)\n<\/pre><\/div>\n\n\n<p>We set the output file to <code>index.html<\/code> using the above code. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\np = figure(\n    title = &#039;Example chart&#039;,\n    x_axis_label = &#039;X axis&#039;,\n    y_axis_label = &#039;Y axis&#039;\n)\n<\/pre><\/div>\n\n\n<p>We use the figure() to create the plot. The figure() takes in multiple attributes. You can refer to the documentation of this module for further details. <\/p>\n\n\n\n<p>We shall use the <code>title<\/code>, <code>x_axis_label<\/code> and <code>y_axis_label<\/code>. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\np.line(x, y, legend=&quot;test&quot;, line_width=1.5)\n<\/pre><\/div>\n\n\n<p>Now coming to rendering the glyph, we will use the code snippet above. We specify the two lists <code>x<\/code> and <code>y<\/code> defined earlier. We also specify additional parameters such as <code>legend<\/code> and <code>line_width<\/code>. <\/p>\n\n\n\n<p>Kindly note, these parameters are used here because we are using <code>line<\/code> chart. These parameters tend to vary for the other types of graphs. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nshow(p)\n<\/pre><\/div>\n\n\n<p>We use the <code>show()<\/code> function to display the result and the result is displayed on <code>index.html<\/code> as shown below. The entire code is attached as well. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom bokeh.plotting import figure, output_file, show\nx=&#x5B;1,2,3,4,5]\ny=&#x5B;4,3,3,5,4]\n\n# defining the output file\noutput_file(&#039;index.html&#039;)\n\n# Adding the plot\np = figure(\n    title = &#039;Example chart&#039;,\n    x_axis_label = &#039;X axis&#039;,\n    y_axis_label = &#039;Y axis&#039;\n)\n\n# Rendering the graph\np.line(x, y, legend=&quot;test&quot;, line_width=1.5)\n\n# Display the results\nshow(p)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Output-chart-shown-on-index-html.png\" alt=\"Output Chart Shown On Index Html\" class=\"wp-image-7046\" width=\"460\" height=\"437\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Output-chart-shown-on-index-html.png 801w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Output-chart-shown-on-index-html-300x285.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Output-chart-shown-on-index-html-768x730.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Output-chart-shown-on-index-html-24x24.png 24w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><figcaption>Output Chart Shown On index.html<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">2. Plotting graphs from CSV files<\/h3>\n\n\n\n<p>To plot a graph, we will use a simple dataset of Cars which has 2 columns namely the name of the car and horsepower. We will understand the correlation between these parameters using graphs. The dataset is as shown below<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"338\" height=\"353\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Da.png\" alt=\"Da\" class=\"wp-image-7125\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Da.png 338w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Da-287x300.png 287w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Da-24x24.png 24w\" sizes=\"auto, (max-width: 338px) 100vw, 338px\" \/><figcaption>Dataset<\/figcaption><\/figure><\/div>\n\n\n\n<p>The above dataset can be plotted as a histogram (hbar) graph using Bokeh and the code for this is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom bokeh.plotting import figure, output_file, show, save, ColumnDataSource\nfrom bokeh.models.tools import HoverTool\nfrom bokeh.transform import factor_cmap\nfrom bokeh.palettes import Blues8\nfrom bokeh.embed import components\nimport pandas\n\ndf = pandas.read_csv(&#039;cars.csv&#039;)\n\nsource = ColumnDataSource(df)\n\noutput_file(&#039;index.html&#039;)\ncar_list = source.data&#x5B;&#039;Car&#039;].tolist()\n\n# Add plot\np = figure(\n    y_range=car_list,\n    plot_width=800,\n    plot_height=600,\n    title=&#039;Cars With Top Horsepower&#039;,\n    x_axis_label=&#039;Horsepower&#039;,\n    tools=&quot;pan,box_select,zoom_in,zoom_out,save,reset&quot;\n)\n\n# Render glyph\np.hbar(\n    y=&#039;Car&#039;,\n    right=&#039;Horsepower&#039;,\n    left=0,\n    height=0.4,\n    fill_color=factor_cmap(\n      &#039;Car&#039;,\n      palette=Blues8,\n      factors=car_list\n    ),\n    fill_alpha=0.9,\n    source=source,\n    legend=&#039;Car&#039;\n)\n\n# Add Legend\np.legend.orientation = &#039;vertical&#039;\np.legend.location = &#039;top_right&#039;\np.legend.label_text_font_size = &#039;10px&#039;\n\n\n# Show results\nshow(p)\n<\/pre><\/div>\n\n\n<p>The output rendered on <code>index.html<\/code> is as follows:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"858\" height=\"618\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/graph-output.png\" alt=\"Graph Output\" class=\"wp-image-7126\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/graph-output.png 858w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/graph-output-300x216.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/graph-output-768x553.png 768w\" sizes=\"auto, (max-width: 858px) 100vw, 858px\" \/><figcaption>Graph Output<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Hence, we come to the end of this article. Bokeh is one of the robust data visualization libraries that you can use for all your projects. Do try out the examples in this article and let us know what you feel in the comments section below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will be looking into data visualization using Python Bokeh. Bokeh allows users to take in data in any format such as CSV, JSON, hard-coded data, or databases. We can create scatter plots, line charts, etc using this library. It is widely used for stock market analysis in the industry because it [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":7128,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7043","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\/7043","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\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7043"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7043\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7128"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}