hvPlot only supports Python >=3.6 on Linux, Windows, or Mac. Anaconda normally has hvPlot installed, and you can check if it was installed in your Anaconda by:
import hvplot
print(f'hvPlot Version : {hvplot.__version__}.')
hvPlot Version : 0.8.0.
Otherwise, you can install it using the conda
command in Anaconda or Miniconda. Besides, it also needs to install the JupyterLab pyviz
extension to enable Jupyterlab support hvPlot.
conda install -c pyviz hvplot
Or install it using PyPI if you use traditional Python.
pip install hvplot
If you use traditional Python and Jupyterlab, you need to install pyviz
too. If the versions of your jupyterLab is >=3.0, the necessary extension is automatically bundled in the pyviz_comms
package. Then you can simply install it in the following way.
pip install pyviz_comms
However, if the version of your Jupyterlab is <3.0, you should also manually install the JupyterLab extension with the following command:
jupyter labextension install @pyviz/jupyterlab_pyviz
hvplot generates plots from different data structures of the PyData ecosystem, including:
hvPlot Ecosystem. Source:https://hvplot.holoviz.org/
To support each data structure of the PyData ecosystem, we should import hvplot.library name
first. For examples,
import hvplot.pandas # to support pandas DataFrames and geoDateFrame
import hvplot.xarray # to support XArray DataArray
import hvplot.dask # to support dask DataArray
import hvplot.intake # to support XArray DataSource (data catalogues)
import hvplot.networkx # to support XArray DataArray
import hvplot.streamz # to support Streamz DataFrame(s)
In this article, we will use pandas DataFrame to display the process.
hvPlot supports several plotting backends, including bokeh, Matplotlib and plotly. It is easy to change the backend using hvplot.extension()
. We can easily change the backend to plotly, matplotlib, bokeh, respectively, using the following commands:
hvplot.extension('plotly') # change to plotly plotting backend
hvplot.extension('matplotlib') # change to matplotlib plotting backend
hvplot.extension('bokeh') # change to bokeh plotting backend
To follow the examples in the following sections, we need to read a historical dataset on exchange prices of US Dollars to Chinese RMB from one of my GitHub repository using pandas.
import pandas as pd
url = 'https://raw.githubusercontent.com/shoukewei/data/main/data-wpt/USD_CNY%20Historical%20Data.csv'
df = pd.read_csv(url, parse_dates=["Date"])
df.head()
Date | Price | Open | High | Low | Vol. | Change % | |
---|---|---|---|---|---|---|---|
0 | 2022-09-23 | 7.1284 | 7.0781 | 7.1298 | 7.0780 | NaN | 0.71% |
1 | 2022-09-22 | 7.0781 | 7.0490 | 7.0956 | 7.0488 | NaN | 0.43% |
2 | 2022-09-21 | 7.0479 | 7.0183 | 7.0546 | 7.0183 | NaN | 0.43% |
3 | 2022-09-20 | 7.0176 | 7.0100 | 7.0214 | 7.0044 | NaN | 0.18% |
4 | 2022-09-19 | 7.0050 | 6.9957 | 7.0206 | 6.9956 | NaN | 0.36% |
Let's drop the 'Vol.', 'Change %' columns to use the other columns data only.
df = df.drop(['Vol.', 'Change %'], axis=1)
df.head()
Date | Price | Open | High | Low | |
---|---|---|---|---|---|
0 | 2022-09-23 | 7.1284 | 7.0781 | 7.1298 | 7.0780 |
1 | 2022-09-22 | 7.0781 | 7.0490 | 7.0956 | 7.0488 |
2 | 2022-09-21 | 7.0479 | 7.0183 | 7.0546 | 7.0183 |
3 | 2022-09-20 | 7.0176 | 7.0100 | 7.0214 | 7.0044 |
4 | 2022-09-19 | 7.0050 | 6.9957 | 7.0206 | 6.9956 |
The Bokeh plotting library is set as the default one, so it does not need to run hvplot.extension('bokeh')
. In the following example, we only need to import hvplot.pandas
to use the Pandas DataFrame dataset imported above.
import hvplot
import hvplot.pandas
Then we just need to replace pandas' .plot()
by .hvplot()
to create interactive plots with hvPlot. For example,
df.hvplot(xlabel="Date",
ylabel='Exchange Price',
title="Exchange Price of USD to CNY")
In this example, we switch the hvPlot plotting backend to Matplotlib for static plots.
import hvplot
import hvplot.pandas
hvplot.extension('matplotlib')
df.hvplot(xlabel="Date",
ylabel='Exchange Price',
title="Exchange Price of USD to CNY")
Similarly, we can easily change plotting backend to plotly as follows.
import hvplot
import hvplot.pandas
hvplot.extension('plotly')
df.hvplot(xlabel="Date",
ylabel='Exchange Price',
title="Exchange Price of USD to CNY")
For the plot above, hvPlot dynamically added the Pandas .hvplot()
method, so that you can use the same syntax as with the pandas default plotting. Besides, hvPlot has also its own explicit plotting method.
If you prefer to be more explicit, you can use directly with hvPlot objects. Let's see a simple example as follows.
from hvplot import hvPlot
# change back to bokeh backend
hvplot.extension('bokeh')
plot = hvPlot(df)
plot(xlabel="Date",
ylabel='Exchange Price',
title="Exchange Price of USD to CNY")