Python - Data visualization using Bokeh - GeeksforGeeks

23 Jun, 2020
Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots. Bokeh output can be obtained in various mediums like notebook, html and server. It is possible to embed bokeh plots in Django and flask apps.Bokeh provides two visualization interfaces to users:
bokeh.models : A low level interface that provides high flexibility to application developers.bokeh.plotting : A high level interface for creating visual glyphs.
To install bokeh package, run the following command in the terminal:
pip install bokeh
The dataset used for generating bokeh graphs is collected from Kaggle.
Code #1: Scatter MarkersTo create scatter circle markers, circle() method is used.
# import modulesfrom bokeh.plotting import figure, output_notebook, show  # output to notebookoutput_notebook()  # create figurep = figure(plot_width = 400, plot_height = 400)  # add a circle renderer with# size, color and alphap.circle([1, 2, 3, 4, 5], [4, 7, 1, 6, 3],          size = 10, color = "navy", alpha = 0.5)  # show the resultsshow(p) 
Output :
Code #2: Single lineTo create a single line, line() method is used.
# import modulesfrom bokeh.plotting import figure, output_notebook, show  # output to notebookoutput_notebook()  # create figurep = figure(plot_width = 400, plot_height = 400)   # add a line rendererp.line([1, 2, 3, 4, 5], [3, 1, 2, 6, 5],         line_width = 2, color = "green")  # show the resultsshow(p)
Output :
Code #3: Bar ChartBar chart presents categorical data with rectangular bars. The length of the bar is proportional to the values that are represented.
# import necessary modulesimport pandas as pdfrom bokeh.charts import Bar, output_notebook, show  # output to notebookoutput_notebook()  # read data in dataframedf = pd.read_csv(r"D:/kaggle/mcdonald/menu.csv")  # create barp = Bar(df, "Category", values = "Calories",        title = "Total Calories by Category",                         legend = "top_right")  # show the resultsshow(p)
Output :
Code #4: Box PlotBox plot is used to represent statistical data on a plot. It helps to summarize statistical properties of various data groups present in the data.
# import necessary modulesfrom bokeh.charts import BoxPlot, output_notebook, showimport pandas as pd  # output to notebookoutput_notebook()  # read data in dataframedf = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv")  # create barp = BoxPlot(df, values = "Protein", label = "Category",             color = "yellow", title = "Protein Summary (grouped by category)",             legend = "top_right")  # show the resultsshow(p)
Output :
Code #5: HistogramHistogram is used to represent distribution of numerical data. The height of a rectangle in a histogram is proportional to the frequency of values in a class interval.
# import necessary modulesfrom bokeh.charts import Histogram, output_notebook, showimport pandas as pd  # output to notebookoutput_notebook()  # read data in dataframedf = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv")  # create histogramp = Histogram(df, values = "Total Fat",               title = "Total Fat Distribution",                color = "navy")  # show the resultsshow(p) 
Output :
Code #6: Scatter plotScatter plot is used to plot values of two variables in a dataset. It helps to find correlation among the two variables that are selected.
# import necessary modulesfrom bokeh.charts import Scatter, output_notebook, showimport pandas as pd  # output to notebookoutput_notebook()  # read data in dataframedf = pd.read_csv(r"D:/kaggle / mcdonald / menu.csv")  # create scatter plotp = Scatter(df, x = "Carbohydrates", y = "Saturated Fat",            title = "Saturated Fat vs Carbohydrates",            xlabel = "Carbohydrates", ylabel = "Saturated Fat",            color = "orange")   # show the resultsshow(p) 
Output :References: https://bokeh.pydata.org/en/latest/
python-modules
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists