Holoview plots do not show/appear/display in jupyter notebooks - python

I'm trying to make a Holoviews Sankey plot in Jupyter notebook (running Jupyter notebook using Pycharm), but the Sankey plot does not appear after the block is executed.
First I import required packages and set 'bokeh' as Holoviews extension. Using holoviews 1.10.9 and jupyterlab 0.35.4.
import pandas as pd
import holoviews as hv
hv.extension('bokeh')
After performing some data wrangling, I prepare a dataframe 'private_investments_grouped' with 3 columns (source, target, value).
I then execute the following code to make the Sankey plot for a sub-dataframe:
hv.Sankey(private_investments_grouped.loc[1:7]).options(label_position='left')
No plot appears in the output block, no pop-ups, no error messages. It appears the code executes, but no visualization appears.
I can use the 'bokeh' renderer in a Python file in Pycharm and store the Sankey plot as a html file, which works fine. However, I'd like to see the plot in Jupyter notebook.

Related

How to save a Plotly graph in a ipynb file?

I understand that it is possible to export a Plotly graph, and that I can display it.
While sharing notebooks, the matplotlib plots remain intact in the Jupyter Notebooks, however, the Plotly graphs do not. They simply disappear
I understand that the Plotly graph is browser rendered, but is there any way I can store the graph in the ipynb file when I export it?
Is there any way that I can display the Plotly graph, just like the matplotlib graph?
Edit: As suggested in an answer, I tried to save it to a figure object, and display that, but no luck there either :/
If you put the code for a series of graphs in a single cell, execute it and save it, I think it will be displayed the next time you open it.
import plotly.express as px
df = px.data.iris() # iris is a pandas DataFrame
fig = px.scatter(df, x="sepal_width", y="sepal_length")
fig.show()

interactive plots Jupyter Lab 2.0

I am using Jupyter Lab 2.0, matplotlib 3.2.0 and Python 3.8.0
So, I found out you can get interactive plots in Jupyter Lab, and embarked on the journey to make this happen for myself. Along the way, here are the things I tried:
Ensured nodejs is installed
Installed the Jupyter Lab extension manager
Installed ipympl
Installed jupyter-matplotlib
Re-build JupyterLab
Restart JupyterLab (several times)
Next, in my code I tried all of the following common recommendations (alone and in various combinations):
matplotlib.use("nbragg")
%matplotlib ipympl
%matplotlib widget
%matplotlib inline
At best, some of these cause all of my rcParams defaults to be overridden, and show tiny graphs with no interactivity. At worst, my graphs don't show at all, except for sometimes displaying some random text below the code.
I have tried all of the suggestions from the following SO answers, GitHub and matplotlib docs:
jupyterlab interactive plot
https://github.com/matplotlib/ipympl/issues/9
https://github.com/matplotlib/ipympl/issues/133
https://matplotlib.org/users/prev_whats_new/whats_new_1.4.html#the-nbagg-backend
Sample of my working code:
train_df = pd.read_csv('data/train.csv', index_col='PassengerId')
test_df = pd.read_csv('data/test.csv', index_col='PassengerId')
survival = train_df.pop('Survived')
df = pd.concat([train_df, test_df])
# extract last names and titles:
df['Title'] = df.Name.str.extract(r'([A-Za-z]+)\.')
# extract last name:
df['LName'] = df.Name.str.extract(r'([A-Za-z]+),')
# Plot histogram of parsed titles:
sns.countplot(df.Title).set_title("Histogram of Passenger Titles")
plt.show()
Note that I have tried this with and without plt.show().

Inline Interactive Plots with Julia in jupyter notebook

when I use
%matplotlib notebook
import matplotlib.pyplot as plt
I get interactive plots, i.e. I can also zoom into the figure.
For julia this command does not seem to exist. Any ideas?
You can get interactive plots by using Plotly, either directly with Plotly.jl or through its Plots backend.
Today I am thinking about how to enlarge the plot window interactively without referring to python or plotly or something else in Jupiter notebook. And I find a good solution. It is to use the package Interact. A simple example is as follows. You can get an interactive plot in 3 lines.
import Pkg; Pkg.add("Interact")
using LinearAlgebra, Plots, Interact
#manipulate for n in 10:100, w in 200:1000, h in 200:1000
plot(randn(n),randn(n),seriestype=:scatter,label="n=$(n)",size = (w, h))
end
The result looks like this

Problems creating a categorical histogram in Bokeh and Pandas

I am trying to produce a simple histogram of categorical data with the following code
import pandas as pd
from bokeh.plotting import figure, show
# some fake categories and count data
counts = pd.Series({'Cat0':1599, 'Cat1':1357, 'Cat2':671,
'Cat3':610, 'Cat4':446, 'Cat5':210})
# pull out the categories from the index
cats = list(counts.keys())
plt = figure(x_range=cats)
plt.vbar(cats, top=list(counts.values), width=2, line_color='green')
show(plt)
but instead of a plot I get
Javascript error adding output!
Error: Error rendering Bokeh model: could not find tag with id: e7346df5-7d3d-4f34-92e2-9e59eb36ec41
See your browser Javascript console for more details.
Is this a bug or have I specified something wrong?
I am using Firefox 54.0 running on Ubuntu (kernel 4.10.0). Other Bokeh plots run without a problem. I am outputting them inline to a Jupyter notebook.
Bokeh is bokeh-0.12.4-py3.6.
Not sure what is causing the problem, but re-executing
from bokeh.plotting import figure, show, output_notebook
and re-running
output_notebook()
solves the problem and plots again show in the notebook.

Seaborn Plot doesn't show up

I am creating a bar chart with seaborn, and it's not generating any sort of error, but nothing happens either.
This is the code I have:
import pandas
import numpy
import matplotlib.pyplot as plt
import seaborn
data = pandas.read_csv('fy15crime.csv', low_memory = False)
seaborn.countplot(x="primary_type", data=data)
plt.xlabel('crime')
plt.ylabel('amount')
seaborn.plt.show()
I added "seaborn.plt.show() in an effort to have it show up, but it isn't working still.
You should place this line somewhere in the top cell in Jupyter to enable inline plotting:
%matplotlib inline
It's simply plt.show() you were close. No need for seaborn
I was using PyCharm using a standard Python file and I had the best luck with the following:
Move code to a Jupyter notebook (which can you do inside of PyCharm by right clicking on the project and choosing new - Jupyter Notebook)
If running a chart that takes a lot of processing time it might not have been obvious before, but in Jupyter mode you can easily see when the cell has finished processing.

Categories