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.
Can I cut it from the image? The height setting doesn't fit because the table may have a different number of rows.
MatPlotLib with Python To remove whitespaces at the bottom of a Matplotlib graph, we can use tight layout or autoscale_on=False.
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.
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.
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.
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)
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)
I tried it with different data sizes, header cells size and cells size and it was always perfectly cropped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With