Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly: How to remove white space after saving an image?

Tags:

python

plotly

I have a question about saving plotly table as an image. I wrote the following code:

import plotly.graph_objects as go

layout = go.Layout(autosize=True, margin={'l': 0, 'r': 0, 't': 0, 'b': 0})

fig = go.Figure(data=[go.Table(header=dict(values=['A Scores', 'B Scores']),
                           cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]]))
                  ], layout=layout)

fig.write_image("fig1.png", scale=5)

But unfortunately, there is a big white space at the bottom of the table.

big white space

Can I cut it from the image? The height setting doesn't fit because the table may have a different number of rows.

like image 208
Golov Peter Avatar asked Aug 11 '20 21:08

Golov Peter


People also ask

How do I get rid of white space in MatPlotLib?

MatPlotLib with Python To remove whitespaces at the bottom of a Matplotlib graph, we can use tight layout or autoscale_on=False.

What is Add_trace in Plotly?

Adding Traces To Subplots If a figure was created using plotly. subplots. make_subplots() , then supplying the row and col arguments to add_trace() can be used to add a trace to a particular subplot. In [12]: from plotly.subplots import make_subplots fig = make_subplots(rows=1, cols=2) fig.

What is Plotly Graph_objects in Python?

The plotly. graph_objects module (typically imported as go ) contains an automatically-generated hierarchy of Python classes which represent non-leaf nodes in this figure schema. The term "graph objects" refers to instances of these classes. The primary classes defined in the plotly.

How do you save a Plotly graph?

Save Your Plot Once you have your data and plot ready to go, click on SAVE on the left-hand side. Give your PLOT and DATA a filename and select the privacy setting.


1 Answers

You can do it by setting the cell height and the entire image height according to this cell height:

import plotly.graph_objects as go

CELL_HEIGHT = 30
a_scores = [100, 90, 80, 90]
b_scores = [95, 85, 75, 95]

layout = go.Layout(
    autosize=True, 
    margin={'l': 0, 'r': 0, 't': 0, 'b': 0}, 
    height=CELL_HEIGHT * (len(a_scores) + 1)
)

fig = go.Figure(
    data=[
        go.Table(
            header=dict(values=['A Scores', 'B Scores']), 
            cells=dict(values=[a_scores, b_scores], 
            height=CELL_HEIGHT)
        )
    ], 
    layout=layout
)

fig.write_image("fig1.png", scale=5)

4 rows and header table perfectly cropped

If you want to give a different height to the headers:

import plotly.graph_objects as go

CELL_HEIGHT = 30
HEADER_CELL_HEIGHT = 100
a_scores = [100, 90, 80, 90]
b_scores = [95, 85, 75, 95]

layout = go.Layout(
    autosize=True, 
    margin={'l': 0, 'r': 0, 't': 0, 'b': 0}, 
    height=CELL_HEIGHT * len(a_scores) + HEADER_CELL_HEIGHT
)

fig = go.Figure(
    data=[
        go.Table(
            header=dict(values=['A Scores', 'B Scores'], height=HEADER_CELL_HEIGHT), 
            cells=dict(values=[a_scores, b_scores], 
            height=CELL_HEIGHT)
        )
    ], 
    layout=layout
)

fig.write_image("fig1.png", scale=5)

4 rows and header of different size table perfectly cropped

I tried it with different data sizes, header cells size and cells size and it was always perfectly cropped.

like image 129
vinzee Avatar answered Oct 20 '22 19:10

vinzee