How to Add Color Bars in Bokeh?
Bokeh is one of the promising libraries of Python in recent times. It provides high performance and efficiency in data visualizations. One of the big advantages of bokeh is that we can get the output file in various formats such as HTML, notebooks etc. In this article, we will be learning about how to add Color Bars in Bokeh.
Now, there are various visualization interfaces that bokeh provides us such as bokeh.models, bokeh.plotting, bokeh.transform etc so that we can import various modules out of it and use them to create and define various properties in the plots.
One can use Google colab that has everything preinstalled in it but if we are using the local device, then it is very important to install bokeh in our device, else the functionalities won’t work. First, we need to open a command prompt on our desktop and go to the following directory where we want to install bokeh. Be sure, you already have node.js installed on your local device, else you can refer to this website for download. After going to the directory, write the following code in the command prompt.
pip install bokeh
Now, we can use any text editor for the implementation of the above concept.
Code Explain: After importing all the necessary packages in the code, we are creating a scalar data d where we are storing values ranging from 0 to 1. After that, using LinearColorMapper, we are defining the color of the palette and the range in the color-bar. Then, we are creating a figure where we are setting the X-Axis range and Y-Axis Range from 0 to 1. After that, we are plotting the above plot using .image() where we are defining the width and height of the plot along with the starting point of the plot as parameters. So, after fixing the position and labels of the color map, we are finally showing the above plot as an HTML file.
Instead of creating a heatmap, we can also create a simple plot and add a color bar to it. But for that, we need to create a dataframe in python. We need to install pandas in our local device if we are using it or if we use google colab , then we don’t need to install anything. Open a command prompt and write the following code :
pip install pandas
After the creation of the dataframe, we can process the data and add a color bar to it. The following implementation is shown below:
In this second example, we are not creating a heat map. Instead, we are creating a plot with points plotted on the graph in the shape of a circle with the color based on the colors from the color-bar. So, in order to achieve this, we need to create a set of data that is needed to be dataframed in order to get them in a tabular format. After getting the above implementation done, we will be using LinearColorMapper() in order to define the color and the range of the color-map. After that, we are creating a figure where we are defining the plot width and plot height along with X and Y-Axis Labels. Then, we are defining the shape of the points plotted i.e a circle and along with that we are using transform() inside which we are defining our color-map and the values that each of the points(x, y) will carry at that location. This means that at every point the color will be according to the value that it carries and the values are made from the ‘col0’ of the dataframe. Finally, we are showing our resultant plot.
In this example, we will be importing a dataset ‘autompg‘ from bokeh.sampledata.autompg.
In the third example, we are importing a dataset using bokeh.sampledata.autompg and since it is already in tabular format, we don’t need to use dataframing. Then we are following the same steps to that of Example 2 and showing the above plot.
Please Login to comment...