gradio-app / gradio

Build and share delightful machine learning apps, all in Python. ๐ŸŒŸ Star to support our work!
http://www.gradio.app
Apache License 2.0
34.13k stars 2.6k forks source link

Plotly Margins Not Taking Effect #9325

Open howardwu1 opened 2 months ago

howardwu1 commented 2 months ago

Describe the bug

When I try to add margins via something like

fig.update_layout(
    margin=dict(l=20, r=20, t=20, b=20),
)

nothing ever happens. The graph does seem to respond well to auto margins but that's about it.

Have you searched existing issues? ๐Ÿ”Ž

Reproduction

import gradio as gr
import plotly.graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(
    x=[0, 1, 2, 3, 4, 5, 6, 7, 8],
    y=[0, 1, 2, 3, 4, 5, 6, 7, 8]
))

fig.update_layout(
    autosize=False,
    width=500,
    height=500,
    margin=dict(
        l=50,
        r=50,
        b=100,
        t=100,
        pad=4
    ),
    paper_bgcolor="LightSteelBlue",
)

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    ticker_data = gr.Plot(
        value=fig, container=False, show_label=False
    )

if __name__ == "__main__":
    demo.launch()

Screenshot

No response

Logs

No response

System Info

Gradio Environment Information:
------------------------------
Operating System: Linux
gradio version: 4.31.0
gradio_client version: 0.16.2

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
altair: 5.3.0
fastapi: 0.111.0
ffmpy: 0.3.2
gradio-client==0.16.2 is not installed.
httpx: 0.27.0
huggingface-hub: 0.23.0
importlib-resources: 6.4.0
jinja2: 3.1.4
markupsafe: 2.1.5
matplotlib: 3.8.4
numpy: 1.26.4
orjson: 3.10.3
packaging: 24.0
pandas: 2.2.2
pillow: 10.3.0
pydantic: 2.7.1
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
ruff: 0.4.3
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.3
typing-extensions: 4.11.0
urllib3: 2.2.1
uvicorn: 0.29.0
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.

gradio_client dependencies in your environment:

fsspec: 2024.3.1
httpx: 0.27.0
huggingface-hub: 0.23.0
packaging: 24.0
typing-extensions: 4.11.0
websockets: 11.0.3

Severity

I can work around it

howardwu1 commented 2 months ago

Note that I can work around it, but I want to demo it, and the formatting bug is very obvious

dtamien commented 2 weeks ago

I've encountered a similar issue where the figure extends beyond the gr.Plot frame. In my experience, using plt.tight_layout() has proven beneficial for adjusting plot elements to ensure they fit within the given area. Although this solution is specific to Matplotlib