iT邦幫忙

2

[Python] 將Pandas table轉成圖檔

  • 分享至 

  • twitterImage
  •  

如題,最近工作關係,需要將結果匯出作為報告,因此上網查了下如何將pandas的table變成圖檔儲存

# 匯入庫
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

# 用Numpy建立樣本
table = np.random.rand(10, 5)
table = np.round(table, 2)

# 用Pandas將樣本轉成DataFrame
table_pd = pd.DataFrame(table)
table_pd.columns=['A', 'B', 'C', 'D', 'E']
table_pd.index=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

# DataFrame=>png
plt.figure('123')            # 視窗名稱
ax = plt.axes(frame_on=False)# 不要額外框線
ax.xaxis.set_visible(False)  # 隱藏X軸刻度線
ax.yaxis.set_visible(False)  # 隱藏Y軸刻度線
pd.plotting.table(ax, table_pd, loc='center') #將mytable投射到ax上,且放置於ax的中間
plt.savefig('table.png')     # 存檔

https://ithelp.ithome.com.tw/upload/images/20200513/201247669J6gKQj5iO.png

參考文獻:
How to save a pandas DataFrame table as a png


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言