如何在PowerPoint中使用Python Seaborn Visualizations?

问题描述 投票:2回答:3

我在Jupyter笔记本中用Seaborn创造了一些数字。我现在想在PowerPoint演示文稿中呈现这些数字。

我知道可以将数字导出为png并将其包含在演示文稿中。但是它们将是静态的,如果数据框中的某些内容发生变化,那么图片将是相同的。有没有选择在PowerPoint中拥有动态数字?你可以在幻灯片中显示的小Jupyter笔记本吗?

python dynamic powerpoint seaborn figure
3个回答
2
投票

你可以试试Anaconda Fusion(也就是video here),让你在Excel中使用Python。这可能有用,因为您可以在Excel和PowerPoint之间链接数字/数据元素(但是当通过Python而不是标准Excel创建图形时可能会应用特殊限制)。 Anaconda Fusion可以免费试用几个月。

另一种解决方案是使用Jupyter Notebook to create your presentation instead of PowerPoint。转到View -> Cell Toolbar -> Slideshowand,您可以选择哪些代码单元格应成为幻灯片。

第三种方法是在数据框发生变化时创建图形动画,然后在PowerPoint中包含动画(GIF或视频)。


1
投票

以下过程可能不是最优雅的解决方案,但它可以让您生成Seaborn图,将其存储为图像文件,并将相同的图像导出到开放的powerpoint演示文稿。根据您是将LinkToFile设置为True还是False,当源更改时,图像将会更新或不会更新。我正在使用Spyder中的单元格来解决这个问题,但它也适用于Jupyter笔记本。确保您有一个名为c:\pptSeaborn\的文件夹。

这里是:

# Some imports
import numpy as np
import seaborn as sns
import os
import matplotlib.pyplot as plt
import win32com.client
import win32api

os.chdir('C:/pptSeaborn')

# Settings for some random data
mu = 0
sigma = 1
simulation = np.random.normal(mu, sigma, 10)

# Make seaborn plot from simulated data. Save as image file.
def SeabornPlot(data, filename = 'c:\\pptSeaborn\\snsPlot.png'):    
    ax = sns.kdeplot(data, shade=True)
    fig = ax.get_figure()
    fig.savefig(filename, bbox_inches='tight', dpi = 440)
    plt.close(fig)

# Import image file to active powerpoint presentation
def SeabornPPT(plotSource, linkImage):

    Application = win32com.client.Dispatch("PowerPoint.Application")
    Presentation = Application.Activepresentation
    slidenr = Presentation.Slides.Count + 1
    Base = Presentation.Slides.Add(slidenr, 12)
    gph = Base.Shapes.AddPicture(FileName=plotSource, 
                                     LinkToFile=linkImage, SaveWithDocument=True, 
                                     Left=50, Top=25, Width=800, Height=500)
    Presentation.slides(slidenr).select()

# Produce data, save plot as image, and export image to powerpoint
SeabornPlot(data = simulation)
SeabornPPT(plotSource = 'c:\\pptSeaborn\\snsPlot.png', linkImage = False)

现在,如果你有一个开放的powerpoint演示文稿并运行这五件事,你会得到这样的东西:

enter image description here

如果你继续将它保存在某个地方并重新打开它,它看起来仍然是一样的。

现在你可以设置linkImage = True,然后再运行五次。根据生成的随机数据,您仍将获得具有不同图形的五张幻灯片。

enter image description here

但是,现在,如果您保存演示文稿并重新打开它,所有绘图看起来都是一样的,因为它们链接到同一个图像文件:

enter image description here

下一步可能是将整个事物包装成一个以filename和LinkToFile作为参数的函数。您还可以包括每次导出图像时该过程是否生成新幻灯片。我希望你发现我的消化很有用。我喜欢你的问题,我也希望看到其他一些建议。


1
投票

我们现在采用这种方法:

您可以将数字保存为.png文件并将其插入到Powerpoint中。插入时有一个选项,每次打开PowerPoint时都会更新图片,从我保存的文件夹中重新搜索新版本的文件。因此,当我在Seaborn中进行更改时,文件的新版本会自动保存为图片,然后在PowerPoint中进行更新。

© www.soinside.com 2019 - 2024. All rights reserved.