1

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 
9
  • You can use from pandas.plotting import table to plot tables Commented Feb 25, 2020 at 16:43
  • 1
    Does this answer your question? How to save a pandas DataFrame table as a png Commented Feb 25, 2020 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? Commented Feb 26, 2020 at 1: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 Commented Feb 26, 2020 at 1: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. Commented Feb 26, 2020 at 17:13

1 Answer 1

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') 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.