2017-10-17 117 views
2

我在Jupyter Notebook中用Seaborn創建了一些數字。我現在想在PowerPoint演示文稿中介紹這些數字。如何在PowerPoint中使用Python Seaborn Visualizations?

我知道可以將數據導出爲png並將它們包含在演示文稿中。但是,它們將是靜態的,並且如果數據幀中的某些內容發生變化,則圖片將是相同的。有沒有在PowerPoint中有動態圖形的選項?像一個小的Jupyter筆記本,你可以在幻燈片中顯示?

回答

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 -> Slideshow,你可以選擇哪些代碼單元格應該成爲幻燈片。

第三種方法是在數據框更改後創建圖形的動畫,然後在PowerPoint中包含動畫(GIF或視頻)。

+1

謝謝你的回答,喬爾。 我正在爲一家公司工作,這家公司想要使用他們的公司幻燈片和智囊細胞插件,所以從Jupyter創建幻燈片不是一個選項。 我會嘗試Anaconda Fusion。 –

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

下一步可以將整個事件包裝成一個函數,該函數將文件名和LinkToFile作爲參數。您還可以包括每次導出圖像時該過程是否生成新的幻燈片。我希望你能發現我的消息有用。我喜歡你的問題,我也希望看到其他一些建議。

+1

非常感謝,我會嘗試這個,並給它一些反饋=) –

+0

這個建議是否成功,或者你是否找到其他方法? – vestland

+1

我們有另一種想法。您可以將數字保存爲.png文件並將其插入到Powerpoint中。插入時有一個選項,每次打開PowerPoint時都會更新圖片,從我保存的文件夾中反轉新版本的文件。因此,當我在Seaborn中進行更改時,新版本的文件會自動保存爲一張圖片,然後在PowerPoint中進行更新。 –

1

我們現在去使用這種方法:

您可以將數據保存爲.png文件,並插入到PowerPoint這一點。插入時有一個選項,每次打開PowerPoint時都會更新圖片,從我保存的文件夾中反轉新版本的文件。因此,當我在Seaborn中進行更改時,新版本的文件會自動保存爲一張圖片,然後在PowerPoint中進行更新。

相關問題