{"id":130,"date":"2019-10-21T10:45:52","date_gmt":"2019-10-21T07:45:52","guid":{"rendered":"http:\/\/www.etemkeskin.com\/?p=130"},"modified":"2020-12-04T09:36:37","modified_gmt":"2020-12-04T06:36:37","slug":"python-da-bokeh-ile-interaktif-veri-gorsellestirme","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/","title":{"rendered":"Interactive Data Visualization with Bokeh in Python"},"content":{"rendered":"\n<p><strong>Bokeh<\/strong> is the Data visualization library published in 2013. It is widely used in the data world.<\/p>\n\n\n\n<h3>What is bokeh? and What Makes It Different From Others?<\/h3>\n\n\n\n<p>Bokeh is an Interactive Data Visualization library. Unlike data visualization libraries such as Matplotlib, Seaborn in Python, Bokeh displays graphics using <strong>Html<\/strong> and <strong>Javascipt<\/strong>.<\/p>\n\n\n\n<p>You can enlarge the plot shown in Bokeh, save, select a specific part of the plot or use other features in the tool with the tool on the right side of the plot.<\/p>\n\n\n\n<p>Bokeh can be installed as follows;<\/p>\n\n\n\n<p><code>pip install bokeh<\/code><\/p>\n\n\n\n<p>If  you use anaconda;<\/p>\n\n\n\n<p><code>conda install bokeh<\/code><\/p>\n\n\n\n<p>Learn Bokeh version;<\/p>\n\n\n\n<p><code>bokeh --version<\/code><\/p>\n\n\n\n<p>We will use the following coding steps when using the Bokeh library.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\"\"\"Data Visualization Template Codes with Bokeh\n\nSummary of basic codes to visualize your data in Bokeh\n\"\"\"\n# Data Analysis Libraries\nimport pandas as pd\nimport numpy as np\n\n# Bokeh libraries\nfrom bokeh.io import output_file, output_notebook\nfrom bokeh.plotting import figure, show\nfrom bokeh.models import ColumnDataSource\nfrom bokeh.layouts import row, column, gridplot\nfrom bokeh.models.widgets import Tabs, Panel\n\n# Preparing the Data\n\n# Define how to visualize\noutput_file('filename.html', title='Simple Bokeh Graph')  # output to static HTML file, or \noutput_notebook()  # in Jupyter Notebook as inline\n\n# Create a new plot.\nfig = figure(plot_height=300, plot_width=300)  #init  figure() object. Define sizes of figure.\n\n# Connecting to data and plotting\n\n# Arrange the layout \n\n# Plot preview and Save\nshow(fig)  # See the plot, save if it needs to be saved.<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"316\" height=\"306\" src=\"http:\/\/www.etemkeskin.com\/wp-content\/uploads\/2019\/10\/bokeh1.jpg\" alt=\"\" class=\"wp-image-147\"\/><\/figure>\n\n\n\n<p> When we execute the code, an empty graph and a display with tools on its side appeared. With the tools in the toolbar on the side, we can move the plot, enlarge it, select a part of the plot, save the plot as .png, initialize the plot and go to Bokeh&#8217;s help website.<\/p>\n\n\n\n<h3>Plotting the First Simple Chart<\/h3>\n\n\n\n<p> Let&#8217;s plotour first simple chart in Bokeh. We will show the function y = x as a line chart.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># x and  y data set \nx = [1, 2, 3, 4, 5]\ny = [6, 7, 2, 4, 5]\n\n# Static HTML file output\noutput_file(\"lines.html\")\n\n# Creating a figure () object where we define the title and the names of the axes\np = figure(title=\"basit \u00e7izgi grafi\u011fi\", x_axis_label='x', y_axis_label='y')\n\n# Adding a line renderer by defining the legend and line thickness\np.line(x, y, legend=\"S\u0131c.\", line_width=1)\n\n# show results \nshow(p)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"612\" height=\"617\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-14.png\" alt=\"\" class=\"wp-image-623\"\/><figcaption>Simple Line Chart<\/figcaption><\/figure>\n\n\n\n<p>As you can see, we created a simple line chart.<\/p>\n\n\n\n<h3>Plotting with BasicGlyphs<\/h3>\n\n\n\n<p>Let&#8217;s create a glyph chart. Let&#8217;s define the sizes of the chart in this example.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Figure object and Defining tags\np = figure(plot_width = 600, plot_height = 600, \n           title = 'Glyph Example',\n           x_axis_label = 'X', y_axis_label = 'Y')\n\n# Sample Data\nsquares_x = [1, 3, 6, 5, 7]\nsquares_y = [4, 6, 4, 2, 9]\ncircles_x = [9, 11, 4, 3, 12]\ncircles_y = [3, 4, 8, 7, 6]\n\n# Creating a square glyph\np.square(squares_x, squares_y, size = 12, color = 'navy', alpha = 0.6)\n# Create a circle glyph\np.circle(circles_x, circles_y, size = 12, color = 'red')\n\n# Defining the plot as a n output in the Notebook\noutput_notebook()\n# Show figure\nshow(p)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"607\" height=\"601\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-15.png\" alt=\"\" class=\"wp-image-625\"\/><figcaption>Glyph Example<\/figcaption><\/figure>\n\n\n\n<p>We can create our graphs with different glyphs. These;<\/p>\n\n\n\n<ul><li>asterisk()<\/li><li>circle()<\/li><li>circle_cross()<\/li><li>circle_x()<\/li><li>cross()<\/li><li>dash()<\/li><li>diamond()<\/li><li>diamond_cross()<\/li><li>inverted_triangle()<\/li><li>square()<\/li><\/ul>\n\n\n\n<h3>Working with Categorical Data<\/h3>\n\n\n\n<p>Let&#8217;s create a barchart based on the world cup win numbers below.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">output_file(\"cups.html\")\n\n# Creating a list of teams that won the world cup\nteams = ['Argentina', 'Brazil', 'Spain', 'Germany']\n\n# World cup win numbers\nwc_won = [2, 5, 1, 4]\n\n# toolbar_location=None and tools=\"\"\n# hide tools to the right of the chart\np = figure(x_range=teams, plot_height=250, title=\"World Cup Win Numbers\",\n           toolbar_location=None, tools=\"\")\n\n# define barchart \np.vbar(x=teams, top=wc_won, width=0.5)\n\np.xgrid.grid_line_color = 'red'\np.y_range.start = 0\n\nshow(p)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"611\" height=\"257\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-16.png\" alt=\"\" class=\"wp-image-628\"\/><figcaption>World Cup Win Numbers<\/figcaption><\/figure>\n\n\n\n<p>We defined the starting point of our y axis with p.y_range.start in the barchart chart.<\/p>\n\n\n\n<h3>ColumnDataSource Object Usage<\/h3>\n\n\n\n<p>Bokeh uses the ColumnDataSource object to deal with data from sources such as Python dictionary and Pandas DataFrames.<\/p>\n\n\n\n<p>Bokeh Python dictionary ve Pandas DataFrames&#8217; ler gibi kaynaklardan gelen veriler ile ba\u015f edebilmek i\u00e7in ColumnDataSource objesini kullan\u0131r. With this object, we can show our plot easily.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># ColumnDataSource import etme\nfrom bokeh.models import ColumnDataSource\n\noutput_file(\"fruit.html\")\ndata = {\n    'fruits':\n    ['Apple', 'Pear', 'Quince', 'Plum', 'Grape', 'Strawberry'],\n    '2015': [2, 1, 8, 3, 2, 4],\n    '2016': [5, 3, 3, 2, 4, 6],\n    '2017': [3, 2, 4, 4, 5, 3],\n    'Alan': [10, 9 , 10 , 7 , 8 ,9]\n}\n#creating DataFrame\ndf = pd.DataFrame(data).set_index(\"fruits\")\n\n#Giving DataFrame as input to ColumnDataSource object\nsource = ColumnDataSource(data=df)\nfruits = source.data['fruits].tolist()\n\n#Create figure() object\nbarchart = figure(x_range=fruits,  plot_height=200, title=\"Tons\")\n\nbarchart.vbar(x='fruits', top='2016', width=0.5, legend='fruit', source=source) \n\nbarchart.legend.orientation = \"horizontal\" \nbarchart.legend.location = \"top_center\"\nbarchart.y_range.start = 0\n\nshow(barchart)<\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"619\" height=\"204\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-17.png\" alt=\"\" class=\"wp-image-630\"\/><\/figure>\n\n\n\n<h3>Making Interactions<\/h3>\n\n\n\n<p><br>If we want to make our chart a little more interactive, there is also a method in Bokeh. Explanatory information can be presented when navigating over the graph with the <strong>HoverTool<\/strong> feature.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># ColumnDataSource import etme\nfrom bokeh.models import ColumnDataSource\nfrom bokeh.models import HoverTool \n\noutput_file(\"fruit.html\")\ndata = {\n    'fruits':\n    ['Apple', 'Pear', 'Quince', 'Plum', 'Grape', 'Strawberry'],\n    '2015': [2, 1, 8, 3, 2, 4],\n    '2016': [5, 3, 3, 2, 4, 6],\n    '2017': [3, 2, 4, 4, 5, 3],\n    'Alan': [10, 9 , 10 , 7 , 8 ,9]\n}\n#Creating a DataFrame\ndf = pd.DataFrame(data).set_index(\"fruits\")\n\n#Giving DataFrame as input to ColumnDataSource object\nsource = ColumnDataSource(data=df)\nfruits = source.data['fruits'].tolist()\n\np = figure()\np.circle(x='2015', y='Alan', source=source, size=10, color='red')\np.square(x='2016', y='Alan', source=source, size=10, color='green')\np.cross(x='2017', y='Alan', source=source, size=10, color='blue')\n#Using the hover feature\nhover = HoverTool()\nhover.tooltips=[\n    ('Ton', '@2015'),\n    ('Acres', '@Field')\n]\n\np.add_tools(hover)\n\nshow(p)<\/pre>\n\n\n\n<p>If you want to do much more with Bokeh, you can visit the official website. In this article, I explained about Bokeh and the strengths of the Bokeh library.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"606\" height=\"609\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2020\/12\/image-18.png\" alt=\"\" class=\"wp-image-633\"\/><\/figure>\n\n\n\n<p><strong>Sources<\/strong><\/p>\n\n\n\n<ol><li><a href=\"https:\/\/docs.bokeh.org\">https:\/\/docs.bokeh.org<\/a><\/li><li><a href=\"https:\/\/realpython.com\/python-data-visualization-bokeh\/\">https:\/\/realpython.com\/python-data-visualization-bokeh\/<\/a><\/li><li><a href=\"https:\/\/towardsdatascience.com\/data-visualization-with-bokeh-in-python-part-one-getting-started-a11655a467d4\">https:\/\/towardsdatascience.com\/data-visualization-with-bokeh-in-python-part-one-getting-started-a11655a467d4<\/a><\/li><li><a href=\"https:\/\/codeburst.io\/overview-of-python-data-visualization-tools-e32e1f716d10\">https:\/\/codeburst.io\/overview-of-python-data-visualization-tools-e32e1f716d10<\/a><\/li><li><a href=\"https:\/\/towardsdatascience.com\/6-reasons-i-love-bokeh-for-data-exploration-with-python-a778a2086a95\">https:\/\/towardsdatascience.com\/6-reasons-i-love-bokeh-for-data-exploration-with-python-a778a2086a95<\/a><\/li><li><a href=\"https:\/\/programminghistorian.org\/en\/lessons\/visualizing-with-bokeh\">https:\/\/programminghistorian.org\/en\/lessons\/visualizing-with-bokeh<\/a><\/li><li><a href=\"https:\/\/stackabuse.com\/pythons-bokeh-library-for-interactive-data-visualization\/\">https:\/\/stackabuse.com\/pythons-bokeh-library-for-interactive-data-visualization\/<\/a><\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Bokeh is the Data visualization library published in 2013. It is widely used in the data world. What is bokeh? and What Makes It Different From Others? Bokeh is an Interactive Data Visualization library. Unlike data visualization libraries such as Matplotlib, Seaborn in Python, Bokeh displays graphics using Html and Javascipt. You can enlarge the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Interactive Data Visualization with Bokeh in Python - blog website<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Interactive Data Visualization with Bokeh in Python - blog website\" \/>\n<meta property=\"og:description\" content=\"Bokeh is the Data visualization library published in 2013. It is widely used in the data world. What is bokeh? and What Makes It Different From Others? Bokeh is an Interactive Data Visualization library. Unlike data visualization libraries such as Matplotlib, Seaborn in Python, Bokeh displays graphics using Html and Javascipt. You can enlarge the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2019-10-21T07:45:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-04T06:36:37+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.etemkeskin.com\/wp-content\/uploads\/2019\/10\/bokeh1.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/#primaryimage\",\"inLanguage\":\"tr\",\"url\":\"http:\/\/www.etemkeskin.com\/wp-content\/uploads\/2019\/10\/bokeh1.jpg\",\"contentUrl\":\"http:\/\/www.etemkeskin.com\/wp-content\/uploads\/2019\/10\/bokeh1.jpg\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/\",\"name\":\"Interactive Data Visualization with Bokeh in Python - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/#primaryimage\"},\"datePublished\":\"2019-10-21T07:45:52+00:00\",\"dateModified\":\"2020-12-04T06:36:37+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2019\/10\/21\/python-da-bokeh-ile-interaktif-veri-gorsellestirme\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Interactive Data Visualization with Bokeh in Python\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/130"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":108,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"predecessor-version":[{"id":635,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/130\/revisions\/635"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}