亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在plotlyexpress圖表旁邊添加表格并將其保存為pdf

如何在plotlyexpress圖表旁邊添加表格并將其保存為pdf

紅糖糍粑 2023-12-20 20:04:37
我有一個dataframe              a            b   c0   2610.101010 13151.030303   33.0000001   1119.459459 5624.216216    65.7777782   3584.000000 18005.333333    3.0000003   1227.272727 5303.272727    29.3333334   1661.156504 8558.836558   499.666667我正在使用繪制直方圖,plotly.express并且還describe使用以下簡單代碼打印表格:import plotly.express as pxfor col in df.columns:    px.histogram(df, x=col, title=col).show()    print(df[col].describe().T)是否可以在每個直方圖旁邊添加describe并將所有圖(及其各自的直方圖)保存在單個 pdf 中?
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

實現此目的的一種方法是創建一個子圖網格,大小為 n_columns * 2(一個用于直方圖,一個用于表格。例如:


from plotly.subplots import make_subplots


titles = [[f"Histogram of {col}", f"Stats of {col}"] for col in df.columns]

titles = [item for sublist in titles for item in sublist]


fig = make_subplots(rows=3,?

? ? ? ? ? ? ? ? ? ? cols=2,?

? ? ? ? ? ? ? ? ? ? specs=[[{"type": "histogram"}, {"type": "table"}]] *3,

? ? ? ? ? ? ? ? ? ? subplot_titles=titles)


for i, col in enumerate(df.columns):

? ? fig.add_histogram(x=df[col],?

? ? ? ? ? ? ? ? ? ? ? row=i+1,?

? ? ? ? ? ? ? ? ? ? ? col=1)

? ? fig.add_table(cells=dict(

? ? ? ? ? ? ? ? ? ? ? ? values=df[col].describe().reset_index().T.values.tolist()

? ? ? ? ? ? ? ? ? ? ? ? ),?

? ? ? ? ? ? ? ? ? header=dict(values=['Statistic', 'Value']),?

? ? ? ? ? ? ? ? ? row=i+1,?

? ? ? ? ? ? ? ? ? col=2

? ? ? ? ? ? ? ? ?)

fig.update_layout(showlegend=False)?

fig.show()


fig.write_image("example_output.pdf")

最后,您可以按照pdf此處的.write_image()說明保存完整的圖(6 個圖表) 。您需要安裝實用程序才能執行此操作。輸出將如下所示(您當然可以自定義它):kaleidoorca

https://img1.sycdn.imooc.com/6582d88200017f7606510312.jpg

如果您需要將每個圖形+表格保存在 PDF 的單獨頁面上,您可以利用該PyPDF2庫。因此,首先,您將每個圖形+表格保存為單個 PDF(如上所述,但您將保存與您擁有的列數一樣多的 PDF 文件,而不是 1)


查看完整回答
反對 回復 2023-12-20
  • 1 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號