I'm trying to create an environment in which I develop Python code with PyCharm while at the same time creating interactive charts using holoviews and bokeh.
I followed steps in Holoview Introduction and it works in Jupyter notebook - the charts are nicely interactive indeed. However, when I run the same code in PyCharm's Python Console, no charts or browser shows up.
In contrast, when I directly call bokeh's methods, as in this example, a browser launches and I can manipulate the charts interactively. I would like to achieve this using holoviews (+bokeh).
Many thanks for your help in advance.
My libraries:
plotting : A high level interface for creating visual glyphs. The dataset used for generating bokeh graphs is collected from Kaggle. To create scatter circle markers, circle() method is used. To create a single line, line() method is used.
Matplotlib, seaborn, ggplot, and Pandas¶Uses bokeh to display a Matplotlib Figure. You can store a bokeh plot in a standalone HTML file, as a document in a Bokeh plot server, or embedded directly into an IPython Notebook output cell. Parameters: fig (matplotlib.
HoloViews is an open-source Python library designed to make data analysis and visualization seamless and simple. With HoloViews, you can usually express what you want to do in very few lines of code, letting you focus on what you are trying to explore and convey, not on the process of plotting.
The only thing you need to add to your code is show(hv.render(your_holoviews_plot))
, like this:
import holoviews as hv
hv.extension('bokeh')
from bokeh.plotting import show
show(hv.render(your_holoviews_plot))
When you run your script in PyCharm (or any other IDE), this will open your plot in the browser.
It sets bokeh as renderer and uses bokeh.plotting.show() to open the plot in the browser.
So no need to go to the commandline etc.
Full working example code:
# import libraries
import numpy as np
import pandas as pd
import hvplot.pandas
import holoviews as hv
# setting bokeh as backend
hv.extension('bokeh')
# going to use show() to open plot in browser
from bokeh.plotting import show
# create some sample data
data = np.random.normal(size=[50, 2])
df = pd.DataFrame(
data=data,
columns=['col1', 'col2'],
)
# using hvplot here to create a holoviews plot
# could have also just used holoviews itself
plot = df.hvplot(kind='scatter', x='col1', y='col2')
# use show() from bokeh
show(hv.render(plot))
The solution is in: http://holoviews.org/user_guide/Deploying_Bokeh_Apps.html
you need these lines of code:
import holoviews.plotting.bokeh
....
layout=#whathever holoview you want to plot
...
doc = hv.renderer('bokeh').server_doc(layout)
And then go to your command prompt, cd to the right directory and run: bokeh serve --show myscript.py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With