{"cells":[{"metadata":{},"cell_type":"markdown","source":"**Bokeh 101**"},{"metadata":{},"cell_type":"markdown","source":"*This notebook represents the first step of my learning journey about Bokeh. I included my learning journey resources in the references section.*\n\n**What is Bokeh?**\n\nBokeh is a library for creating interactive data visualizations. It offers a concise, human-readable syntax, which allows for rapidly presenting data in an aesthetically pleasing manner. \n\n"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#importing Bokeh\nfrom bokeh.plotting import figure\nfrom bokeh.io import output_file, show, output_notebook\noutput_notebook()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"*** Line Glyphs**\n\nSingle Lines"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"#prepare some data\nx = [1,2,3,4,5]\ny = [6,7,8,9,10]\n#create a figure object\nf = figure(plot_width=400, plot_height=400)\n#create line plot\nf.line(x,y,line_width=2)\n#write the plot in the figure object\nshow(f)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Step Line"},{"metadata":{"trusted":true},"cell_type":"code","source":"f = figure(plot_width=400, plot_height=400)\n\n# add a steps renderer\nf.step([1, 2, 3, 4, 5], [6, 7, 2, 4, 5], line_width=2, mode=\"center\")\n\nshow(f)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Multiple Lines"},{"metadata":{"trusted":true},"cell_type":"code","source":"f = figure(plot_width=400, plot_height=400)\n\nf.multi_line([[1, 3, 2], [3, 4, 6, 6]], [[2, 1, 4], [4, 7, 8, 5]],\n             color=[\"purple\", \"green\"], alpha=[0.8, 0.3], line_width=4)\n\nshow(f)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Bars**\n\nBokeh provides the hbar() and vbar() glyphs function for this purpose."},{"metadata":{"trusted":true},"cell_type":"code","source":"f = figure(plot_width=400, plot_height=400)\nf.vbar(x=[1, 2, 3], width=0.5, bottom=0,\n       top=[1.2, 2.5, 3.7], color=\"purple\")\n\nshow(f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"f = figure(plot_width=400, plot_height=400)\nf.hbar(y=[1, 2, 3], height=0.5, left=0,\n       right=[1.2, 2.5, 3.7], color=\"green\")\n\nshow(f)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Bokeh Sample Data**\n\nBokeh allows us to practice with sample data sets. iris is one of them."},{"metadata":{"_cell_guid":"","_uuid":"","trusted":true},"cell_type":"code","source":"from bokeh.sampledata.iris import flowers","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Print the first 5 rows of the data\nflowers.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Print the last 5 rows of the data\nflowers.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"colormap={'setosa':'red','versicolor':'green','virginica':'blue'}\nflowers['color'] = [colormap[x] for x in flowers['species']]\nflowers['size'] = flowers['sepal_width'] * 4","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#after adding color and size columns\nflowers.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**ColumnDataSource**\n\nThe ColumnDataSource is the core of most Bokeh plots, providing the data that is visualized by the glyphs of the plot. With the ColumnDataSource, it is easy to share data between multiple plots and widgets, such as the DataTable.A ColumnDataSource is simply a mapping between column names and lists of data. "},{"metadata":{"trusted":true},"cell_type":"code","source":"from bokeh.models import ColumnDataSource\n\nsetosa = ColumnDataSource(flowers[flowers[\"species\"]==\"setosa\"])\nversicolor = ColumnDataSource(flowers[flowers[\"species\"]==\"versicolor\"])\nvirginica = ColumnDataSource(flowers[flowers[\"species\"]==\"virginica\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Create the figure object\nf = figure(plot_width=1000, plot_height=400)\n\n#adding glyphs\nf.circle(x=\"petal_length\", y=\"petal_width\", size='size', fill_alpha=0.2, \ncolor=\"color\", line_dash=[5,3], legend_label='Setosa', source=setosa)\n\nf.circle(x=\"petal_length\", y=\"petal_width\", size='size', fill_alpha=0.2, \ncolor=\"color\", line_dash=[5,3], legend_label='Versicolor', source=versicolor)\n\nf.circle(x=\"petal_length\", y=\"petal_width\", size='size', fill_alpha=0.2,\ncolor=\"color\", line_dash=[5,3], legend_label='Virginica', source=virginica)\n\nshow(f)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Style the legend\nf.legend.location = (500,500)\nf.legend.location = 'top_left'\nf.legend.background_fill_alpha = 0\nf.legend.border_line_color = None\nf.legend.margin = 10\nf.legend.padding = 18\nf.legend.label_text_color = 'black'\nf.legend.label_text_font = 'times'\n\nshow(f)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**References**\n\n1. https://docs.bokeh.org\n2. Data Visualization on the Browser with Python and Bokeh - Udemy Course -  [udemy.com/course/python-bokeh/]\n3. Kaggle - Micro Courses - Data Visualization"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}