Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 916
user11709615 Avatar asked Oct 16 '19 07:10

user11709615


1 Answers

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

like image 137
Patrick Collins Avatar answered Oct 19 '22 21:10

Patrick Collins