【问题标题】:Exporting jupyter notebook to pdf with offline plotly graph; missing graphs使用离线绘图图将 jupyter notebook 导出为 pdf;缺少图表
【发布时间】:2018-01-27 10:47:42
【问题描述】:

我正在尝试创建我的课程计划的 pdf 导出,并且我使用 plotly offline 绘制图表。在下面的 MWE 中,该图将显示在 Jupyter Notebook 中,但在我导出为 pdf 时不会显示。我使用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)

【问题讨论】:

    标签: python pdf jupyter-notebook plotly


    【解决方案1】:

    您需要指定适当的Default Renderer(或渲染器,如果您想在 Notebook 中可视化它以及使用您提到的File-->Download as-->PDF via Latex (.pdf) 选项导出为 PDF 时)。

    我自己也为此苦苦挣扎了几个小时,但最终为我工作的设置如下:

    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
    

    请注意,您可以使用 + 符号连接任意数量的渲染器,并且 Plotly 将 magically know 何时使用它们。

    【讨论】:

    • 嗯。这看起来很有希望,但我无法通过乳胶将其下载为 pdf。您介意发布一个包含我的 MWE 代码的解决方案,并确认您可以通过 Latex 下载为 pdf 格式没有问题吗?
    【解决方案2】:

    对于上述确切的原始问题,我没有解决方案。但是,如果您想考虑 .html 格式,这里有一个对我有用的不错的解决方案。毕竟,我的目标只是与没有安装jupyter 的人共享笔记本。

    使用plotlyhtmlexporter 执行到.html 的转换。 这是一段您可以复制粘贴的代码:

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

    然后您可能想尝试将浏览器中的 .html 保存(打印)为 .pdf,但我认为这相当于将原始笔记本打印为 .pdf。正如我所说,就我而言,拥有一个独立于 jupyter 的 .html 文件就足够了。

    【讨论】:

      【解决方案3】:

      一个更简单的解决方案是使用image_bytes = fig.to_image(format='png')而不是使用Image(image_bytes)显示,但首先你应该安装orca
      pip3 install psutil requests
      示例:

      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)
      

      您可以阅读更多here

      您可以使用File -> Download as -> PDF 或使用终端jupyter nbconvert --to pdf <notebook_name>.ipynb 进行转换

      【讨论】:

      • 嗨迪玛日尔科。请考虑添加更多信息,因为如果链接过时,那么您的答案质量就会非常低。
      • 谢谢你,@TiagoMartinsPeres
      【解决方案4】:

      我认为因为绘图是 svg 对象并由 javascript 生成,所以我的 jupyter notebook 中没有导出到 PDF 的工作,所以我无法检查并确认我的答案。

      Plotly 离线没有显示为图片,你可以使用 plotly online 来做,它可以免费生成图表,

      您需要创建一个在线帐户,还需要从 plotly 网站粘贴用户名和 API 密钥(API 密钥可以在设置中找到)。

      注意:如果情节是公开或私下共享的,请务必签入,我不对您的情节公开负责。

      无论如何,这将为您提供图形的图像输出,您可以将其导出为 PDF

      代码:

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

      【讨论】:

      • 你只拍摄静态图像,你是否设法将交互性导出为 pdf 一些方式?
      • @PaariVendhan 交互性是javascript的原因,PDF不能嵌入Javascript!
      • 我并不是说 pdf 应该有 javascript,而是一些如何将交互性转换为 pdf 内部可接受的形式。例如,tikz pgfplots 具有可点击的功能。
      • @PaariVendhan GO 用 plotly 提出这个改进,看看他们的回应,谢谢!
      • 它显然超出了 plotly 和 webgl 的范围。这取决于 ipython 或我们。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多