Jupyter Notebook server crashes when calling pandas.DataFrame.style - pandas

My Jupyter Notebook server freezes when I call the style property of a large pandas DataFrame, as in this example:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(9999,3),columns=list("ABC"))
df.style
When I replace 9999 by 999 in the above code this issue doesn't occur. The same script also runs fine in the Anaconda prompt. What could be the cause of the freeze?

Related

Seaborn renders plots slowly in apache zeppelin notebooks

I am currently trying to generate visualizations in zeppelin (0.8.1) notebooks using the pyspark interpreter with python 3.7.3.
Generating the following simple plot with seaborn (0.9.0) takes around 5 minutes (with very high CPU usage throughout the duration):
%pyspark
import seaborn as sns
import numpy as np
import pandas as pd
data = pd.DataFrame(np.random.rand(100,3))
sns.pairplot(data)
This behavior is rather inconsistent as the following (much more data intensive) plot is rendered instantly
%pyspark
import seaborn as sns
import numpy as np
import pandas as pd
df = pd.DataFrame(data = np.random.rand(10000,2))
sns.lineplot(x = 0, y = 1, data = df)
I noticed that using matplotlib (3.1.0) is generally much faster for and almost as snappy as I am used to from jupyter notebook environments.
I have already read about issue ZEPPELIN-1894 but I can render the mentioned scatterplot instantly as well.
Ok, after posting here the solution is to use the %spark.ipyspark interpreter, this might require installing additional packages:
pip install protobuf grpcio

JupyterLab fig does not show. It shows blank result (but works fine on jupyternotebook)

I am new to JupyterLab trying to learn.
When I try to plot a graph, it works fine on jupyter notebook, but does not show the result on jupyterlab. Can anyone help me with this?
Here are the codes below:
import pandas as pd
import pandas_datareader.data as web
import time
# import matplotlib.pyplot as plt
import datetime as dt
import plotly.graph_objects as go
import numpy as np
from matplotlib import style
# from matplotlib.widgets import EllipseSelector
from alpha_vantage.timeseries import TimeSeries
Here is the code for plotting below:
def candlestick(df):
fig = go.Figure(data = [go.Candlestick(x = df["Date"], open = df["Open"], high = df["High"], low = df["Low"], close = df["Close"])])
fig.show()
JupyterLab Result:
Link to the image (JupyterLab)
JupyterNotebook Result:
Link to the image (Jupyter Notebook)
I have updated both JupyterLab and Notebook to the latest version. I do not know what is causing JupyterLab to stop showing the figure.
Thank you for reading my post. Help would be greatly appreciated.
Note*
I did not include the parts for data reading (Stock OHLC values). It contains the API keys. I am sorry for inconvenience.
Also, this is my second post on stack overflow. If this is not a well-written post, I am sorry. I will try to put more effort if it is possible. Thank you again for help.
TL;DR:
run the following and then restart your jupyter lab
jupyter labextension install #jupyterlab/plotly-extension
Start the lab with:
jupyter lab
Test with the following code:
import plotly.graph_objects as go
from alpha_vantage.timeseries import TimeSeries
def candlestick(df):
fig = go.Figure(data = [go.Candlestick(x = df.index, open = df["1. open"], high = df["2. high"], low = df["3. low"], close = df["4. close"])])
fig.show()
# preferable to save your key as an environment variable....
key = # key here
ts = TimeSeries(key = key, output_format = "pandas")
data_av_hist, meta_data_av_hist = ts.get_daily('AAPL')
candlestick(data_av_hist)
Note: Depending on system and installation of JupyterLab versus bare Jupyter, jlab may work instead of jupyter
Longer explanation:
Since this issue is with plotly and not matplotlib, you do NOT have to use the "inline magic" of:
%matplotlib inline
Each extension has to be installed to the jupyter lab, you can see the list with:
jupyter labextension list
For a more verbose explanation on another extension, please see related issue:
jupyterlab interactive plot
Patrick Collins already gave the correct answer.
However, the current JupyterLab might not be supported by the extension, and for various reasons one might not be able to update the JupyterLab:
ValueError: The extension "#jupyterlab/plotly-extension" does not yet support the current version of JupyterLab.
In this condition a quick workaround would be to save the image and show it again:
from IPython.display import Image
fig.write_image("image.png")
Image(filename='image.png')
To get the write_image() method of Plotly to work, kaleido must be installed:
pip install -U kaleido
This is a full example (originally from Plotly) to test this workaround:
import os
import pandas as pd
import plotly.express as px
from IPython.display import Image
df = pd.DataFrame([
dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Resource="Alex"),
dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Resource="Alex"),
dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Resource="Max")
])
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Resource", color="Resource")
if not os.path.exists("images"):
os.mkdir("images")
fig.write_image("images/fig1.png")
Image(filename='images/fig1.png')

mayavi remotely running on Jupyter notebook

I trying to run the following code in Jupyter Notebook. But it didn't work and throw out that the server is done.
I could import mayavi, but I could not 'from mayavi import malb'
%gui qt
from mayavi import mlab
import numpy as np
x, y, z = np.mgrid[-10:10:20j, -10:10:20j, -10:10:20j]
s = np.sin(xyz)/(xyz)
mlab.pipeline.volume(mlab.pipeline.scalar_field(x,y,z,s))
mlab.savefig('test')
The sever just shut down.

Sympy doesn't print equations when using matplotlib in PyCharm

I recently started using Pycharm instead of Jupyter on my laptop to do some light scientific work(mainly because I like how it auto-completes the code).
this code works fine I Jupyter
import sympy as sp
import matplotlib.pyplot as plt
x = sp.symbols("x")
sp.Integral(x, x)
but when I run it in Pycharm, nothing happens and I don't get any output and I don't know why.

Jupyter notebook wont show image with matplotlib

I am trying to plot image using jupyter notebook.
import pylab as plt
import numpy as np
Z=np.array(((1,2,3,4,5),(4,5,6,7,8),(7,8,9,10,11)))
im = plt.imshow(Z, cmap='hot')
plt.colorbar(im, orientation='horizontal')
plt.show()
And I get some strange error.
When I ran same code in Pycharm, It worked but wont work in jupyter notebook.
.