将pivottablejs图保存到文件中 [英] Save a pivottablejs figure to file

查看:320
本文介绍了将pivottablejs图保存到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用包 pivottablejs 来操作和可视化python中的数据透视表。

I have started using the package pivottablejs to manipulate and visualize pivot tables in python.

from pivottablejs import pivot_ui
pivot_ui(df)  # where df is a pandas dataframe

将在jupyter笔记本中生成交互式数据透视表/情节。

will produce an interactive pivot table/plot in a jupyter notebook.

是否有任何pythonic方法可以将此软件包生成的数字从jupyter笔记本中保存到(例如) png ?我正在寻找类似经典的 plt.savefig('file.png')。前端本质上是 javascript ,我不知道如何(或者是否可能)通过python访问javascript数字。

Is there any pythonic way to save the figures produced by this package to (say) png from within the jupyter notebook? I am looking for something similar to the classic plt.savefig('file.png'). The front-end is essentially javascript, and I do not know how (or if it is possible) to access a javascript figure through python.

推荐答案

评论时间过长,所以我将其添加为答案。 pivottablejs 将图像创建为svgs,并且它们不为图像提供任何下载/导出机制。但你可以做的是将js注入单元格并使用它来下载svg。

This is too long for a comment, so I'm adding it as an answer. pivottablejs creates images as svgs and they do not provide any download/export mechanism for the images. But what you can do is inject js into the cell and download the svg using that.

这样做的一个简单方法是使用 svg-crowbar.js 。使用 pivot_ui 生成图表并在屏幕上显示输出后,在下一个单元格中运行以下命令以下载svg。

One simple way of doing this is using svg-crowbar.js. After you generate the chart using pivot_ui and the output is displayed on the screen, run the following in the next cell to download the svg.

In [10]: %%javascript
         var e = document.createElement('script'); e.setAttribute('src', 'https://nytimes.github.io/svg-crowbar/svg-crowbar.js'); e.setAttribute('class', 'svg-crowbar'); document.body.appendChild(e);

注意:这仅适用于chrome。

Note: This only works on chrome.

这篇关于将pivottablejs图保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆