将pandas数据框的“Out[]表保存为figu

2024-05-15 08:53:55 发布

您现在位置:Python中文网/ 问答频道 /正文

这似乎是一个无用的功能,但它会对我很有帮助。我想保存输出,我得到的天篷内的IDE。我不认为这是特定的冠层,但为了清晰起见,这是我使用的。例如,我的控制台输出[2]就是我想要的:

enter image description here

我认为格式化非常好,每次都复制而不是仅仅保存输出将是浪费时间。所以我的问题是,我怎样才能掌握这个数字?理想情况下,植入方法类似于标准方法,因此可以这样做:

from matplotlib.backends.backend_pdf import PdfPages

pp = PdfPages('Output.pdf')
fig = plt.figure() 
ax = fig.add_subplot(1, 1, 1)
df.plot(how='table')
pp.savefig()
pp.close()

注意:我意识到之前有人问过一个非常类似的问题(How to save the Pandas dataframe/series data as a figure?),但它从未得到答案,我想我已经把这个问题说得更清楚了。


Tags: 方法功能pdffig情况数字idepp
3条回答

我相信,这是一个你的IDE正在呈现的HTML表。这就是ipython笔记本的功能。

你可以这么做:

from IPython.display import HTML
import pandas as pd
data = pd.DataFrame({'spam':['ham','green','five',0,'kitties'],
                     'eggs':[0,1,2,3,4]})
h = HTML(data.to_html())
h

并保存到HTML文件:

my_file = open('some_file.html', 'w')
my_file.write(h.data)
my_file.close()

这是一个有点老套的解决方案,但它能完成任务。你想要一个.pdf但是你得到了一个奖金.png。:)

import numpy as np
import pandas as pd
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

from PySide.QtGui import QImage
from PySide.QtGui import QPainter
from PySide.QtCore import QSize
from PySide.QtWebKit import QWebPage

arrays = [np.hstack([ ['one']*3, ['two']*3]), ['Dog', 'Bird', 'Cat']*2]
columns = pd.MultiIndex.from_arrays(arrays, names=['foo', 'bar'])
df =pd.DataFrame(np.zeros((3,6)),columns=columns,index=pd.date_range('20000103',periods=3))

h = "<!DOCTYPE html> <html> <body> <p> " + df.to_html() + " </p> </body> </html>";
page = QWebPage()
page.setViewportSize(QSize(5000,5000))

frame = page.mainFrame()
frame.setHtml(h, "text/html")

img = QImage(1000,700, QImage.Format(5))
painter = QPainter(img)
frame.render(painter)
painter.end()
a = img.save("html.png")

pp = PdfPages('html.pdf')
fig = plt.figure(figsize=(8,6),dpi=1080) 
ax = fig.add_subplot(1, 1, 1)
img2 = plt.imread("html.png")
plt.axis('off')
ax.imshow(img2)
pp.savefig()
pp.close()

欢迎编辑。

我认为这里需要的是在输出到pdf的图形中,以一致的方式将表输出到pdf文件。

我的第一个想法是不要使用matplotlib后端

from matplotlib.backends.backend_pdf import PdfPages

因为它在格式化选项上似乎有点受限,倾向于将表格式化为图像(从而以不可选择的格式呈现表的文本)

如果要在pdf中混合使用dataframe输出和matplotlib打印,而不使用matplotlib pdf后端,我可以想出两种方法。

  1. 像以前一样生成matplotlib图形的pdf,然后插入包含dataframe表的页面。我认为这是一个困难的选择。
  2. 使用其他库生成pdf。下面我将介绍一种方法。

首先,安装xhtml2pdf库。这看起来支持得有点零散,但它是active on Github,并且有一些basic usage documentation here。您可以通过pippip install xhtml2pdf安装它

完成后,下面是一个简单的示例,嵌入matplotlib图形,然后是表(所有文本都可以选择),然后是另一个图形。你可以随意使用CSS等来改变格式,使之符合你的具体要求,但我认为这满足了这个要求:

from xhtml2pdf import pisa             # this is the module that will do the work
import numpy as np
import pandas as pd
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

# Utility function
def convertHtmlToPdf(sourceHtml, outputFilename):
    # open output file for writing (truncated binary)
    resultFile = open(outputFilename, "w+b")

    # convert HTML to PDF
    pisaStatus = pisa.CreatePDF(
            sourceHtml,                # the HTML to convert
            dest=resultFile,           # file handle to recieve result
            path='.')                  # this path is needed so relative paths for 
                                       # temporary image sources work

    # close output file
    resultFile.close()                 # close output file

    # return True on success and False on errors
    return pisaStatus.err

# Main program
if __name__=='__main__':   

    arrays = [np.hstack([ ['one']*3, ['two']*3]), ['Dog', 'Bird', 'Cat']*2]
    columns = pd.MultiIndex.from_arrays(arrays, names=['foo', 'bar'])
    df = pd.DataFrame(np.zeros((3,6)),columns=columns,index=pd.date_range('20000103',periods=3))

    # Define your data
    sourceHtml = '<html><head>'         
    # add some table CSS in head
    sourceHtml += '''<style>
                     table, td, th {
                           border-style: double;
                           border-width: 3px;
                     }

                     td,th {
                           padding: 5px;
                     }
                     </style>'''
    sourceHtml += '</head><body>'
    #Add a matplotlib figure(s)
    plt.plot(range(20))
    plt.savefig('tmp1.jpg')
    sourceHtml += '\n<p><img src="tmp1.jpg"></p>'

    # Add the dataframe
    sourceHtml += '\n<p>' + df.to_html() + '</p>'

    #Add another matplotlib figure(s)
    plt.plot(range(70,100))
    plt.savefig('tmp2.jpg')
    sourceHtml += '\n<p><img src="tmp2.jpg"></p>'

    sourceHtml += '</body></html>'
    outputFilename = 'test.pdf'

    convertHtmlToPdf(sourceHtml, outputFilename)

注意在编写时xhtml2pdf中似乎有一个bug,这意味着某些CSS不受尊重。与这个问题特别相关的是,似乎不可能在桌子周围有两个边界


编辑

在回应评论中,很明显有些用户(至少@Keith同时回答并获得了奖金!)希望表是可选择的,但绝对是在matplotlib轴上。这与原来的方法有些一致。因此-这里是一个只对matplotlib和matplotlib对象使用pdf后端的方法。我认为这个表看起来不太好,特别是层次列标题的显示,但我想这是一个选择的问题。我很感激this answer和注释,它们提供了格式化表格显示轴的方法。

import numpy as np
import pandas as pd
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt

# Main program
if __name__=='__main__':   
    pp = PdfPages('Output.pdf')
    arrays = [np.hstack([ ['one']*3, ['two']*3]), ['Dog', 'Bird', 'Cat']*2]
    columns = pd.MultiIndex.from_arrays(arrays, names=['foo', 'bar'])
    df =pd.DataFrame(np.zeros((3,6)),columns=columns,index=pd.date_range('20000103',periods=3))

    plt.plot(range(20))
    pp.savefig()
    plt.close()

    # Calculate some sizes for formatting - constants are arbitrary - play around
    nrows, ncols = len(df)+1, len(df.columns) + 10
    hcell, wcell = 0.3, 1.
    hpad, wpad = 0, 0   

    #put the table on a correctly sized figure    
    fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
    plt.gca().axis('off')
    matplotlib_tab = pd.tools.plotting.table(plt.gca(),df, loc='center')    
    pp.savefig()
    plt.close()

    #Add another matplotlib figure(s)
    plt.plot(range(70,100))
    pp.savefig()
    plt.close()

    pp.close()

相关问题 更多 >