0

I currently have a python script that analyzes a jstack dump and outputs a dataframe like this:

Dataframe

I want to turn this into a png or jpg image of a table containing this data. Something simple like this:

Table

Does anyone know what the easiest, most straight forward way to produce an image like this so it is saved in the same path that I am running the code?

Thank you! Javier

***EDIT:

The provided solutions are outputting an unreadable table with mostly white space as below:

Table output

Can anyone suggest what I'm doing wrong and which parameters to adjust?

***EDIT #2:

Here is my code:

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")

And the current output:

Table output #2

***EDIT #3:

This is the dataframe I am passing:

                                         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
Javier
  • 9
  • 1
  • 1
  • 3
  • You can use `from pandas.plotting import table` to plot tables – XXavier Feb 25 '20 at 16:43
  • 1
    Does this answer your question? [How to save a pandas DataFrame table as a png](https://stackoverflow.com/questions/35634238/how-to-save-a-pandas-dataframe-table-as-a-png) – AMC Feb 25 '20 at 16:45
  • @Javier, I did not see your comment you wrote in the original post until now. Can you post the code that you used with the sample table for me to check why it is not working for you? – XXavier Feb 26 '20 at 01:40
  • One thing that you need is probably larger font. `t = table(ax, df)` `t.auto_set_font_size(False)` `t.set_fontsize(12)` I am not sure about the extra white space. I will check that once you show your code. Another option you can try is to do `df.to_html()` to see how the html format the table – XXavier Feb 26 '20 at 01:52
  • @XXavier, thank you so much for getting back to me on this. The font size definetely has things more visible but the format is still off. I have edited my original post with my code as well as the new output with the font set to 12. – Javier Feb 26 '20 at 17:13
  • @Javier: Can you please post a sample data? Can you remove `frame_on=False` from `ax = plt.subplot(111, frame_on=False)` to see if this makes any difference. I need sample data to replicate the problem. – XXavier Feb 26 '20 at 17:26
  • @XXavier, removing 'frame_on=False' did not resolve the issue. I have posted the dataframe I am passing in my original post. Thanks again for your continued help! – Javier Feb 27 '20 at 18:27
  • @Javier: Please find my updated answer – XXavier Feb 27 '20 at 19:52
  • @XXavier, thank you so much! That solved the issue! Are you aware of a way to set different column widths for different columns? Or to increase the width of the entire png as a whole? Some of the text is being cutoff, I am looking through documentation for a solution on my end too. Once again, thank you for all your help! – Javier Mar 02 '20 at 16:24

1 Answers1

4

Can you try this?

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()

This is the output i got: You can change the 0.9 of the column width to resize the width of the column and change the font if it is too small enter image description here You an plot pandas table using this.

from pandas.plotting import table
table(df)

You can hide all the axis and frames by using below code

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

You can save the table using the savefig.

plt.savefig('table.png')
XXavier
  • 1,206
  • 1
  • 10
  • 14