从panda数据框创建表

2024-05-15 15:44:28 发布

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

我目前有一个python脚本,它分析jstack转储并输出如下数据帧:

Dataframe

我想将其转换为包含此数据的表的png或jpg图像。简单地说:

Table

有人知道最简单、最直接的方法是什么来生成这样的图像,以便将其保存在与我运行代码相同的路径中吗

谢谢大家!! 哈维尔

***编辑:

提供的解决方案输出一个不可读的表,其中大部分为空白,如下所示:

Table output

有人能建议我做错了什么,调整哪些参数吗

***编辑#2:

这是我的密码:

df = pd.DataFrame(table_data, columns = ["Method Name", "# of threads", "% of threads"])

ax = plt.subplot(111, frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

t = table(ax, df)
t.auto_set_font_size(False)
t.set_fontsize(12)
fig.savefig("test.png")

以及电流输出:

Table output #2

***编辑#3:

这是我正在传递的数据帧:

                                         Method Name # of threads % of threads
0  at sun.nio.ch.EPollArrayWrapper.epollWait(Nati...           33        32.35
1             at sun.misc.Unsafe.park(Native Method)           29        28.43
2                                NO JAVA STACK TRACE           18        17.64
3  at java.net.PlainSocketImpl.socketAccept(Nativ...            6        05.88
4  at xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...            5        04.90
5            at java.lang.Object.wait(Native Method)            4        03.92
6           at java.lang.Thread.sleep(Native Method)            3        02.94
7  at java.net.SocketInputStream.socketRead0(Nati...            3        02.94
8  at sun.nio.ch.ServerSocketChannelImpl.accept0(...            1        00.98

Tags: of数据图像false编辑pngtablejava
1条回答
网友
1楼 · 发布于 2024-05-15 15:44:28

你能试试这个吗

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.axis('off')
ax.axis('tight')
t= ax.table(cellText=df.values, colWidths = [0.9]*len(df.columns),  colLabels=df.columns,  loc='center')
t.auto_set_font_size(False) 
t.set_fontsize(8)
fig.tight_layout()
plt.show()

这是我得到的输出:您可以更改列宽的0.9来调整列宽,如果列宽太小,可以更改字体 enter image description here 您可以使用此工具创建一个绘图表

from pandas.plotting import table
table(df)

您可以使用下面的代码隐藏所有轴和帧

ax = plt.subplot(111, frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
table(ax, df)

您可以使用savefig保存表格

plt.savefig('table.png')

相关问题 更多 >