Bokeh
Let’s move on to the third library of our list. Bokeh is mainly famous for its interactive charts visualization. Bokeh renders its plots using HTML and JavaScript that uses modern web browsers for presenting elegant, concise construction of novel graphics with high-level interactivity.
To install this type the below command in the terminal.
pip install bokeh
Scatter Plot
Scatter Plot in Bokeh can be plotted using the scatter() method of the plotting module. Here pass the x and y coordinates respectively.
Example:
Python3
# importing the modules from bokeh.plotting import figure, output_file, show from bokeh.palettes import magma import pandas as pd # instantiating the figure object graph = figure(title = "Bokeh Scatter Graph" ) # reading the database data = pd.read_csv( "tips.csv" ) color = magma( 256 ) # plotting the graph graph.scatter(data[ 'total_bill' ], data[ 'tip' ], color = color) # displaying the model show(graph) |
Output:
Line Chart
A line plot can be created using the line() method of the plotting module.
Example:
Python3
# importing the modules from bokeh.plotting import figure, output_file, show import pandas as pd # instantiating the figure object graph = figure(title = "Bokeh Bar Chart" ) # reading the database data = pd.read_csv( "tips.csv" ) # Count of each unique value of # tip column df = data[ 'tip' ].value_counts() # plotting the graph graph.line(df, data[ 'tip' ]) # displaying the model show(graph) |
Output:
Bar Chart
Bar Chart can be of two types horizontal bars and vertical bars. Each can be created using the hbar() and vbar() functions of the plotting interface respectively.
Example:
Python3
# importing the modules from bokeh.plotting import figure, output_file, show import pandas as pd # instantiating the figure object graph = figure(title = "Bokeh Bar Chart" ) # reading the database data = pd.read_csv( "tips.csv" ) # plotting the graph graph.vbar(data[ 'total_bill' ], top = data[ 'tip' ]) # displaying the model show(graph) |
Output:
Interactive Data Visualization
One of the key features of Bokeh is to add interaction to the plots. Let’s see various interactions that can be added.
click_policy property makes the legend interactive. There are two types of interactivity –
- Hiding: Hides the Glyphs.
- Muting: Hiding the glyph makes it vanish completely, on the other hand, muting the glyph just de-emphasizes the glyph based on the parameters.
Example:
Python3
# importing the modules from bokeh.plotting import figure, output_file, show import pandas as pd # instantiating the figure object graph = figure(title = "Bokeh Bar Chart" ) # reading the database data = pd.read_csv( "tips.csv" ) # plotting the graph graph.vbar(data[ 'total_bill' ], top = data[ 'tip' ], legend_label = "Bill VS Tips" , color = 'green' ) graph.vbar(data[ 'tip' ], top = data[ 'size' ], legend_label = "Tips VS Size" , color = 'red' ) graph.legend.click_policy = "hide" # displaying the model show(graph) |
Output:
Adding Widgets
Bokeh provides GUI features similar to HTML forms like buttons, sliders, checkboxes, etc. These provide an interactive interface to the plot that allows changing the parameters of the plot, modifying plot data, etc. Let’s see how to use and add some commonly used widgets.
- Buttons: This widget adds a simple button widget to the plot. We have to pass a custom JavaScript function to the CustomJS() method of the models class.
- CheckboxGroup: Adds a standard check box to the plot. Similarly to buttons we have to pass the custom JavaScript function to the CustomJS() method of the models class.
- RadioGroup: Adds a simple radio button and accepts a custom JavaScript function.
Example:
Python3
from bokeh.io import show from bokeh.models import Button, CheckboxGroup, RadioGroup, CustomJS button = Button(label = "GFG" ) button.js_on_click(CustomJS( code = "console.log('button: click!', this.toString())" )) # Labels for checkbox and radio # buttons L = [ "First" , "Second" , "Third" ] # the active parameter sets checks the selected value # by default checkbox_group = CheckboxGroup(labels = L, active = [ 0 , 2 ]) checkbox_group.js_on_click(CustomJS(code = """ console.log('checkbox_group: active=' + this.active, this.toString()) """ )) # the active parameter sets checks the selected value # by default radio_group = RadioGroup(labels = L, active = 1 ) radio_group.js_on_click(CustomJS(code = """ console.log('radio_group: active=' + this.active, this.toString()) """ )) show(button) show(checkbox_group) show(radio_group) |
Output:
Note: All these buttons will be opened on a new tab.
- Sliders: Adds a slider to the plot. It also needs a custom JavaScript function.
Example:
Python3
from bokeh.io import show from bokeh.models import CustomJS, Slider slider = Slider(start = 1 , end = 20 , value = 1 , step = 2 , title = "Slider" ) slider.js_on_change( "value" , CustomJS(code = """ console.log('slider: value=' + this.value, this.toString()) """ )) show(slider) |
Output:
Similarly, much more widgets are available like a dropdown menu or tabs widgets can be added.
Note: For complete Bokeh tutorial, refer Python Bokeh tutorial – Interactive Data Visualization with Bokeh
Data Visualization with Python
In today’s world, a lot of data is being generated on a daily basis. And sometimes to analyze this data for certain trends, patterns may become difficult if the data is in its raw format. To overcome this data visualization comes into play. Data visualization provides a good, organized pictorial representation of the data which makes it easier to understand, observe, analyze. In this tutorial, we will discuss how to visualize data using Python.
Python provides various libraries that come with different features for visualizing data. All these libraries come with different features and can support various types of graphs. In this tutorial, we will be discussing four such libraries.
- Matplotlib
- Seaborn
- Bokeh
- Plotly
We will discuss these libraries one by one and will plot some most commonly used graphs.
Note: If you want to learn in-depth information about these libraries you can follow their complete tutorial.
Before diving into these libraries, at first, we will need a database to plot the data. We will be using the tips database for this complete tutorial. Let’s discuss see a brief about this database.