Python: Plotly graph not rendering

Viewed 185

I'm trying to plot a stock price using Plotly in Python 3.9 (Anaconda/Spyder), but the graph isn't displaying. The following code executes without error, but no plot.

import yfinance
tsla = yfinance.Ticker('TSLA')
hist = tsla.history(period='1y')

import plotly.graph_objects as go

fig = go.Figure(data=go.Scatter(x=hist.index,y=hist['Close'], mode='lines'))
fig.show()

I'm working on local version of Python, no web interaction. The following code works for plotting in a browser window:

import plotly.express as px
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
fig.write_html('first_figure.html', auto_open=True)

Just not sure why .show() isn't working. Seems to be the most basic function there is to Plotly. Hoping there's something easy/obvious I'm missing.

1 Answers

Try adding this :

import plotly.io as pio
pio.renderers.default = 'browser'

It will open the plot in your default browser.

More info on renderers

Related