Python:绘图图不渲染

发布于 2025-02-02 03:47:33 字数 562 浏览 2 评论 0原文

我正在尝试使用Python 3.9(Anaconda/Spyder)中的Plotly绘制股票价格,但图并未显示。以下代码执行没有错误,但没有图。

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()

我正在研究本地版的Python,没有网络交互。 以下代码可用于在浏览器窗口中绘制:

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)

不确定为什么.show()不起作用。似乎是最基本的功能。 希望我缺少一些简单/显而易见的东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2025-02-09 03:47:33

尝试添加以下内容:

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

它将在默认浏览器中打开图。

有关 Renderers 的更多信息

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文