Black Lives Matter. Please consider donating to Campaign Zero's mission of ending police violence in America.
https://www.joincampaignzero.org

Remove whitespace at the bottom of a table

Hi,

I’m generating a table with data and save it to the image. And at the bottom appears huge whitespace which i don’t know how to remove. Does it look like a bug?

And here’s my code, pretty simple:

Is there a way to fix it? Thanks!

Hi @nggix,

Your table is saved with a big empty area under the table because you set all go.Layout.margin(s) on 0. These settings led to maximal fig window.

To get a saved image with resonable empty area, don’t set the layout margin(s), because Plotly uses in this case the default values: t=100, l=80, b=80.

Then save the image with custom real width and height in pixels:

import plotly.io as pio
pio.write_image(fig, 'your_path/filename.jpeg', scale=1, width=600, height=300)

Typing:

help(pio.write_image)

we get the following info:

Help on function write_image in module plotly.io._orca:

write_image(fig, file, format=None, scale=None, width=None, height=None, validate=True)
    Convert a figure to a static image and write it to a file or writeable
    object
    
    Parameters
    ----------
    fig:
        Figure object or dict representing a figure
    
    file: str or writeable
        A string representing a local file path or a writeable object
        (e.g. an open file descriptor)
    
    format: str or None
        The desired image format. One of
          - 'png'
          - 'jpg' or 'jpeg'
          - 'webp'
          - 'svg'
          - 'pdf'
          - 'eps' (Requires the poppler library to be installed)
    
        If not specified and `file` is a string then this will default to the
        file extension. If not specified and `file` is not a string then this
        will default to `plotly.io.config.default_format`
    
    width: int or None
        The width of the exported image in layout pixels. If the `scale`
        property is 1.0, this will also be the width of the exported image
        in physical pixels.
    
        If not specified, will default to `plotly.io.config.default_width`
    
    height: int or None
        The height of the exported image in layout pixels. If the `scale`
        property is 1.0, this will also be the height of the exported image
        in physical pixels.
    
        If not specified, will default to `plotly.io.config.default_height`
    
    scale: int or float or None
        The scale factor to use when exporting the figure. A scale factor
        larger than 1.0 will increase the image resolution with respect
        to the figure's layout pixel dimensions. Whereas as scale factor of
        less than 1.0 will decrease the image resolution.
    
        If not specified, will default to `plotly.io.config.default_scale`
    
    validate: bool
        True if the figure should be validated before being converted to
        an image, False otherwise.
    
    Returns
    -------
    None

Thanks for your reply! i tried that too but trying to avoid fixed width or height. in other words i know how many columns is in my table but size of data is always different and if I use fixed height will it skim my table? i don’t have a chance to test it with larger amount of data but is there a way to keep the height dynamically based on dataset size?

@nggix,

Unfortunately a figure is saved as an image either with layout width and height or with a fixed width and height, as I mentioned in the previous answer. No chance to be saved with a height automatically detected from the number of table rows.

Interesting idea. In theory i can get number of rows and calculate estimated height of the image. Wondering if there is any default value for the row height?