简体   繁体   English

PyCharm 中不显示 Plotly 图表

[英]Plotly chart is not displayed in PyCharm

How can I display interactive plotly graphs in Pycharm?如何在 Pycharm 中显示交互式绘图图? I run the following code:我运行以下代码:

import plotly.offline as py
import plotly.graph_objs as go

py.init_notebook_mode(connected=True)

data = [go.Bar(
    x=['giraffes', 'orangutans', 'monkeys'],
    y=[20, 14, 23]
)]

py.iplot(data, filename="barplot")

The result in PyCharm is a blank field: PyCharm 中的结果是一个空白字段: Pycharm 中的 Pycharm 结果

In Jupyter Notebook this code gives a (proper) interactive chart as the result.在 Jupyter Notebook 中,这段代码给出了一个(适当的)交互式图表作为结果。

Update: Answers from Embed Plotly HTML in PyCharm IDE don't work for me.更新:来自Embed Plotly HTML in PyCharm IDE 的答案对我不起作用。 When I use the plot() function (instead of the iplot() ) it exports the chart into separate file and open a browser in a new window.当我使用plot()函数(而不是iplot() )时,它将图表导出到单独的文件中,并在新窗口中打开浏览器。 The output in notebook is the filename of the generated chart. notebook 中的输出是生成的图表的文件名。 I want to include the chart into notebook and use it interactively, like in Jupyter Notebook.我想将图表包含到笔记本中并以交互方式使用它,就像在 Jupyter Notebook 中一样。 That answers are only about exporting the chart into separate html file.该答案仅与将图表导出到单独的 html 文件有关。

jayBana's answer is correct, but if you want to keep using .py scripts in PyCharm, you can simply set default renderer to 'browser' to get interactive plotly graphs: jayBana 的回答是正确的,但如果您想继续在 PyCharm 中使用 .py 脚本,您只需将默认渲染器设置为“浏览器”即可获得交互式绘图:

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

as described here: https://plot.ly/python/renderers/如此处所述: https : //plot.ly/python/renderers/

With the recently released major version of Plotly 4.0, the steps below work for me with PyCharm 2019.2 on macOs Mojave using iPython notebooks from within PyCharm.随着最近发布的 Plotly 4.0 主要版本,以下步骤适用于我在 macOs Mojave 上使用 PyCharm 中的 iPython 笔记本的 PyCharm 2019.2。

I believe that this should work on other operating systems as well with other recent versions of PyCharm supporting Jupyter notebooks.我相信这应该适用于其他操作系统以及其他支持 Jupyter 笔记本的 PyCharm 的最新版本。

I am using conda for package and environment management but this should work with other tools as well eg pip or pipenv (given that orca is installed standalone)我正在使用conda进行包和环境管理,但这也应该与其他工具一起使用,例如 pip 或 pipenv(假设 orca 是独立安装的)

Here are my steps:这是我的步骤:

Create and activate conda environment:创建并激活 conda 环境:

  • $ conda create -n pycharm-plotly python
  • $ conda activate pycharm-plotly

Install Plotly 4.0 and its dependencies as per the plotly.py's GitHub README for Jupyter Notebook Support根据plotly.py 的 GitHub README for Jupyter Notebook Support安装 Plotly 4.0 及其依赖项

  • $ conda install -c plotly plotly==4.0.0
  • $ conda install "notebook>=5.3" "ipywidgets>=7.5"

In addition, I found that "Plotly Orca" is required for this to work:此外,我发现需要“Plotly Orca”才能工作:

  • $ conda install -c plotly plotly-orca psutil requests

Please note that the above works with both "Configured Server" and "Managed Server" from within PyCharm for .ipynb file extensions using the following sample code:请注意,使用以下示例代码,对于.ipynb文件扩展名,上述内容适用于 PyCharm 中的“Configured Server”和“Managed Server”:

#%%
import plotly.graph_objects as go
import plotly.io as pio

pio.renderers.default = 'png'

fig = go.Figure(
    data=[go.Bar(y=[2, 1, 3])],
    layout_title_text="A Figure Displayed with fig.show()"
)
fig.show();

在此处输入图片说明

Additional Notes:补充说明:

  • I believe that Plotly's plot rendering doesn't work with "plain" Python files in PyCharm's Scientific mode the same way as it works for Matplotlib or Seaborn.我相信 Plotly 的绘图渲染不适用于 PyCharm 的科学模式中的“普通”Python 文件,就像它适用于 Matplotlib 或 Seaborn 一样。

I hope someone proves me wrong, but what you are trying to accomplish does in fact seem to not be possible at the moment.我希望有人证明我是错的,但你试图完成的事情实际上目前似乎是不可能的。 I was hoping that your problem was perhaps limited to the community editions of PyCharm.我希望您的问题可能仅限于 PyCharm 的社区版本。 The Scientific mode of PyCharm is stated to have PyCharm 的 科学模式据称具有

[...] an outstanding set of features [...] [...] 一组出色的功能 [...]

So, I was pretty sure that upgrading to the Professional Edition Version 2019.1.2 would provide a version not lacking in any form of Jupyter Notebook support.因此,我非常确定升级到专业版 2019.1.2 版将提供一个不缺乏任何形式的 Jupyter Notebook 支持的版本。 Alas, It seems I was wrong.唉,看来我错了。 I have tried with various other plotly approaches, but found none that makes it possible to fire up an interactive plotly figure.我尝试过其他各种情节方法,但没有发现可以激发交互式情节图。

As close to any proof that I can provide:尽可能接近我可以提供的任何证据:

在此处输入图片说明

Version confirmation:版本确认:

在此处输入图片说明


Here's a test run from a Jupyter Notebook not in PyCharm with your snippet:这是从不在 PyCharm 中的 Jupyter Notebook 使用您的代码段进行的测试:

Plot:阴谋:

在此处输入图片说明


Also, as suggested in the comments, replacing iplot with plot in plotly.offline.plot(plot(data), filename='file.html') will open a web browser and display your plot too.此外,正如评论中所建议的,在plotly.offline.plot(plot(data), filename='file.html')中用plot替换iplot打开一个网络浏览器并显示你的绘图。

If you would just simply want to show the plot without any hassle here is how I display to browser for only specific plots.如果您只想简单地显示绘图,这里是我仅针对特定绘图向浏览器显示的方式。

import numpy as np
import plotly.express as px

# Plot histogram based on rolling 2 dice
dice_1 = np.random.randint(1,7,5000)
dice_2 = np.random.randint(1,7,5000)
dice_sum = dice_1 + dice_2
# bins represent the number of bars to make
fig = px.histogram(dice_sum, nbins=11, labels={'value':'Dice Roll'},
             title='5000 Dice Roll Histogram', marginal='violin',
            color_discrete_sequence=['green'])

fig.show(renderer="browser")

在此处输入图片说明

NOTE the renderer="browser" you can of course change it to other outputs.注意renderer="browser"您当然可以将其更改为其他输出。 Here is a list of them docs这是他们的文档列表

As of plotly version 4, all you need to do is call .show() on any go.Figure :plotly版本 4 开始,您需要做的就是随时调用.show()go.Figure

import plotly.graph_objects as go
fig = go.Figure( go.Scatter(x=[1,2,3], y=[1,3,2] ) )
fig.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM