简体   繁体   English

使用离线 plotly 图将 jupyter 笔记本导出到 pdf; 缺少图表

[英]Exporting jupyter notebook to pdf with offline plotly graph; missing graphs

I am trying to create pdf export of my lesson plans and I use plotly offline for the graphs.我正在尝试创建我的课程计划的 pdf 导出,并且我离线使用 plotly 来制作图表。 In a MWE below, the plot will display in the Jupyter Notebook but will not show up when I export to pdf. I export using File-->Download as-->PDF via Latex (.pdf) .在下面的 MWE 中,plot 将显示在 Jupyter Notebook 中,但当我导出到 pdf 时不会显示。我使用File-->Download as-->PDF via Latex (.pdf)

I'd like to make a pdf instead of using html. I understand it might take an extra step to convert an html export to pdf, but I was just wondering if there was a more direct route (a code modification?) that would allow me to export directly through File-->Download as-->PDF via Latex (.pdf)我想制作一个 pdf 而不是使用 html。我知道将 html 导出转换为 pdf 可能需要额外的步骤,但我只是想知道是否有更直接的途径(代码修改?)我直接通过File-->Download as-->PDF via Latex (.pdf)

from plotly.offline import download_plotlyjs, init_notebook_mode, iplot
init_notebook_mode(connected=True)
import plotly.graph_objs as go

data = [go.Scatter(
    x=[1, 2, 3],
    y=[3, 2, 1])
]
iplot(data)

I think because plotly graphs are svg objects and generated by javascript, I dont have export to PDF working in my jupyter notebook, so I was unable to check and confirm my answer.我认为因为 plotly 图形是 svg 对象并且由 javascript 生成,所以我没有在我的 jupyter notebook 中导出到 PDF,所以我无法检查和确认我的答案。

Plotly offline does not have show as image, you can use plotly online to do this, its free to generate graphs, Plotly offline 没有显示为图像,你可以使用 plotly online 来做到这一点,它可以免费生成图形,

You need to create an online account, also you need to paste the username and API key from plotly website (API key can be found in settings).您需要创建一个在线帐户,还需要粘贴来自 plotly 网站的用户名和 API 密钥(API 密钥可以在设置中找到)。

Note: please check in plotly if the plots are shared in public or private, I am not responsible for your plots becoming public.注意:如果情节是公开或私人共享的,请仔细检查,我对您的情节公开不承担任何责任。

Anyway this will give you an image output of the graph, and you can export it to PDF无论如何,这将为您提供图形的图像输出,您可以将其导出为 PDF

Code:代码:

import plotly
import plotly.graph_objs as go

plotly.plotly.sign_in('<<username goes here>>', '<<api key goes here>>')
trace = go.Bar(x=[2, 4, 6], y= [10, 12, 15])
data = [trace]
layout = go.Layout(title='A Simple Plot', width=800, height=640)
fig = go.Figure(data=data, layout=layout)

plotly.plotly.image.save_as(fig, filename='a-simple-plot.png')

from IPython.display import Image
Image('a-simple-plot.png')

A bit easier solution is to use image_bytes = fig.to_image(format='png') and than display using Image(image_bytes) , but firstly you should install orca , and一个更简单的解决方案是使用image_bytes = fig.to_image(format='png')而不是使用Image(image_bytes)显示,但首先你应该安装orca ,并且
pip3 install psutil requests
example:例子:

fig = go.Figure()
""" here goes some code to draw """

image_bytes = fig.to_image(format='png', , width=1200, height=700, scale=1) # you can use other formats as well (like 'svg','jpeg','pdf')

#instead of using fig.show()
from IPython.display import Image
Image(img_bytes)

You can read more here你可以在这里阅读更多

Than you can convert it with File -> Download as -> PDF or using terminal jupyter nbconvert --to pdf <notebook_name>.ipynb您可以使用File -> Download as -> PDF或使用终端jupyter nbconvert --to pdf <notebook_name>.ipynb

I don't have a solution for the exact original problem above.我没有解决上述确切原始问题的解决方案。 However, if you wish to consider the .html format, here is a nice solution that worked for me.但是,如果您想考虑 .html 格式,这里有一个对我有用的不错的解决方案。 After all, my goal was just to share the notebook with people who didn't have jupyter installed.毕竟,我的目标只是与没有安装jupyter人共享笔记本。

The conversion to .html is performed with plotlyhtmlexporter .使用plotlyhtmlexporter执行到.html的转换。 Here is a piece of code you can copy-paste:这是您可以复制粘贴的一段代码:

  pip install plotlyhtmlexporter
  jupyter nbconvert --to plotlyhtml mynotebook.ipynb

You might then want to try and save (print) the .html from your browser as a .pdf, but I think it would be equivalent to just printing the original notebook to .pdf.然后,您可能想尝试将浏览器中的 .html 保存(打印)为 .pdf,但我认为这相当于将原始笔记本打印为 .pdf。 As I say, in my case, having a jupyter-independent .html file was enough.正如我所说,就我而言,拥有一个独立于 jupyter 的 .html 文件就足够了。

You need to specify the appropriate Default Renderer (or Renderers, if you want to visualize it in the Notebook and also when exporting to PDF using the File-->Download as-->PDF via Latex (.pdf) option you mentioned).您需要指定适当的默认渲染器(或渲染器,如果您想在 Notebook 中将其可视化,以及在使用您提到的File-->Download as-->PDF via Latex (.pdf)选项导出为 PDF 时)。

I have been struggling with this myself for some hours too, but the setup that ended up working for me is the following:我自己也为此苦苦挣扎了几个小时,但最终对我有用的设置如下:

import plotly.io as pio
pio.renderers.default = "notebook+pdf"  # Renderer for Notebook and HTML exports + Renderer for PDF exports
# init_notebook_mode(connected=True)  # Do not include this line because it may not work

Note that you can concatenate as many Renderers as you want using the + symbol, and Plotly will magically know when to use each of them.请注意,您可以使用+符号连接任意数量的渲染器,并且 Plotly 会神奇地知道何时使用它们中的每一个。

Just use this and download the notebook as PDF via HTML. Also you need to pip install kaleido first.只需使用它并通过 HTML 将笔记本下载为 PDF。此外,您还需要 pip 先安装 kaleido。

    import plotly.io as pio
    pio.kaleido.scope.default_format = "png"

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

相关问题 将带有 plotly 的 Jupyter 笔记本导出到 html 离线时无法正确显示 - Exporting Jupyter notebook with plotly to html not displaying correctly when offline 将jupyter ipython笔记本导出为pdf - exporting jupyter ipython notebook to pdf 在 Jupyter Notebook 中保存 Plotly 离线图像? - Save Plotly Offline image in Jupyter Notebook? 在 Jupyter Notebook 上离线绘制 icreate_animations - Plotly icreate_animations offline on Jupyter Notebook 使用 nbconvert 将 Jupyter 笔记本导出到 PDF 时的模板 - Templates while exporting Jupyter notebook to PDF with nbconvert 将 Jupyter Notebook 导出为 PDF 格式时缺少和未定义的控制序列错误 - Missing and Undefined Control Sequence Errors when Exporting Jupyter Notebook to PDF Format plotly.grid_objs 模块已弃用 jupyter notebook 离线 plot - The plotly.grid_objs module is deprecated for jupyter notebook offline plot Jupyter Notebookly 没有显示图表..(只是图表) - Jupyter notebook plotly not showing graph.. (just graph) 将线段添加到绘图的简洁方法(使用 python/jupyter notebook)? - Succint way to add line segments to plotly graph (with python/jupyter notebook)? 将保存为 html 文件的绘图图形导入并显示到 Jupyter Notebook 中 - Importing and displaying a plotly graph saved as an html file into Jupyter Notebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM