1

[I need to cut of the whitespace below the table, but cant, because of the invisible axis] 1Im plotting a table from a dataframe and saving it as a image. But i want to get rid of the "whitespace".

`df = {
        'Trace'           :['Mediaan'],
        'Max afstand(mm)' :[1],
        'Aantal staven'   :[2],
        'Gem HOH(mm)'     :[3],
        'Min C(mm)'       :[4],
        'Max C(mm)'       :[5],
        'Gem C(mm)'       :[6]
 }
Data = pd.DataFrame(df);

fig1 = plt.figure(figsize=(8,8),dpi=220)
ax1 = plt.subplot(111, frame_on=False)
ax1.yaxis.set_visible(False)
ax1.xaxis.set_visible(False)
table(ax1,Data,loc='best')
fig1.savefig('Mediaan/'+csvfiles\[i\]+'\_TraceMediaantabel.jpg', bbox_inches='tight, pad_inches=0)`

enter image description here enter image description here

The bbox_inches ignores the invisible axes. Is there a way to make the table as an image but without the axes?

Ray
  • 11
  • 2
  • Does this answer your question? [How do I plot only a table in Matplotlib?](https://stackoverflow.com/questions/32137396/how-do-i-plot-only-a-table-in-matplotlib) – BigBen Feb 07 '23 at 14:31

1 Answers1

0

Here is a possible solution to your question:

import pandas as pd
import matplotlib as mpl,matplotlib.pyplot as plt
from matplotlib.table import Table


df = {
        'Trace'           :['Mediaan'],
        'Max afstand(mm)' :[1],
        'Aantal staven'   :[2],
        'Gem HOH(mm)'     :[3],
        'Min C(mm)'       :[4],
        'Max C(mm)'       :[5],
        'Gem C(mm)'       :[6]
 }
Data = pd.DataFrame(df)



fig1 = plt.figure(figsize=(8,8),dpi=220)
ax1 = plt.subplot(111, frame_on=False)
ax1.yaxis.set_visible(False)
ax1.xaxis.set_visible(False)
table = Table(ax1, bbox=[0,0,1,1])
for i in range(len(Data.columns)):
    for j in range(len(Data)):
        table.add_cell(i, j, width=0.5, height=0.5, text=str(Data.iat[j, i]), loc='right')

table.auto_set_font_size(False)
table.set_fontsize(10)

ax1.add_table(table)
fig1.savefig(r'TraceMediaantabel.jpg', bbox_inches='tight', pad_inches=0)

which returns enter image description here