Beautiful and Easy Plotting in Python — Pandas + Bokeh | by Christopher Tao | Towards Data Science

Although Matplotlib can satisfy all our needs when we want to plot something in Python, it is sometimes time-consuming to create a beautiful chart using it. Well, sometimes we may want to demonstrate something to the boss so that it would be nice to have some beautiful and interactive plots.
There are a lot of excellent libraries that can do it, Bokeh is one of them. However, it might also take some time to learn how to use such libraries. In fact, someone has already solved this problem for us. Here is a library called Pandas-Bokeh, which consumes Pandas directly and render the data using Bokeh. The syntax is extremely straightforward and I believe you can start to use it in no time!
github.com
Let me demonstrate the library using an example.
Firstly, we need to install the library using pip.
pip install pandas_bokeh
After installation, we need to import numpy, pandas and of course the pandas_bokeh library.
import numpy as npimport pandas as pdimport pandas_bokeh
I would like to generate some random data for demonstration purposes. Suppose that we have a dataset of an e-commerce website. The dataset contains sales from 2010 to 2019 for three categories. Let’s generate this dataset using Numpy.
df = pd.DataFrame({    'Year': np.arange(2010, 2020),    'Category-A': np.random.uniform(9000, 15000, 10),    'Category-B': np.random.uniform(9000, 15000, 10),    'Category-C': np.random.uniform(9000, 15000, 10)})
Now, we have data in our Pandas dataframe. Before we start to use pandas_bokeh to plot the data, we need to set the output to the notebook, which will work for Jupyter/iPython notebooks. I will explain why we need to do this later, it is because pandas_bokeh supports other output location.
pandas_bokeh.output_notebook()
OK. we can plot the dataframe now.
df.plot_bokeh(    kind='bar',    x='Year',    y=['Category-A', 'Category-B', 'Category-C'],    xlabel='Category',    ylabel='Annual Sales',    title='Annual Sales by Category')
Isn’t it much more beautiful than the default matplotlib? Let’s have a quick look at the parameters:
kind what type of chart do you want to plot? Currently, pandas_bokeh supports the following chart types: line, point, step, scatter, bar, histogram, area, pie and map.
x and y Simply pass in the column name(s) of the Pandas dataframe
xlabel and ylabel The label of the x-axis and y-axis relatively
title The title of the chart
So, you have seen how easy it is to create such a beautiful plot. More importantly, it is interactive. Below is a GIF from the official GitHub repo.
Of course, the library also supports a lot of advanced parameters that allow us to customise the plot if necessary.
Here is another example using the same dataset but plot the data using a line chart.
df.plot_bokeh.line(    x='Year',     y=['Category-A', 'Category-B', 'Category-C'],    figsize=(900, 500),    ylim=(5000, 20000),    zooming=False,    panning=False)
Please note that here I use df.plot_bokeh.line(...) which is equivalent to df.plot_bokeh(kind='line', ...).
figsize Define the size of the plot in a tuple (width, height)
xlim and ylim Define the default ranges of the x-axis and y-axis respectively. Here I only set for the y-axis.
zooming Enable/disable the zooming gesture
panning Enable/disable the panning gesture
Do you remember that we have set the output to the notebook?
pandas_bokeh.output_file('chart.html')
Apart from the Jupyter Notebook, we can also set the output to an HTML file. So, the chart will be saved and output to an HTML file that can be persisted and distributed.
In this article, I have demonstrated how to use the pandas_bokeh library to plot your Pandas dataframe end-to-end with extremely simple code but a beautiful presentation with interactive features.
It turns out that the library may not satisfy all your needs when you have many special rendering requirements, but it is an excellent library when you just want to build a typical chart for your dataset.
medium.com
If you feel my articles are helpful, please consider joining Medium Membership to support me and thousands of other writers! (Click the link above)