Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting plotly margins #9805

Merged
merged 3 commits into from
Oct 25, 2024
Merged

Allow setting plotly margins #9805

merged 3 commits into from
Oct 25, 2024

Conversation

aliabid94
Copy link
Collaborator

Plotly margins, when explicitly set, were being overwritten, so users couldn't specify them. Fixed now. See discussion here:
https://huggingface.slack.com/archives/C02QZLG8GMN/p1729596330205269

See modified outbreak_forecast demo below:

import gradio as gr
from math import sqrt
import numpy as np
import pandas as pd
import plotly.express as px

def outbreak(r, month, countries, social_distancing):
    months = ["January", "February", "March", "April", "May"]
    m = months.index(month)
    start_day = 30 * m
    final_day = 30 * (m + 1)
    x = np.arange(start_day, final_day + 1)
    pop_count = {"USA": 350, "Canada": 40, "Mexico": 300, "UK": 120}
    if social_distancing:
        r = sqrt(r)
    df = pd.DataFrame({"day": x})
    for country in countries:
        df[country] = x ** (r) * (pop_count[country] + 1)

    fig = px.line(df, x="day", y=countries)
    fig.update_layout(
          # title="Outbreak in " + month,
          xaxis_title="Cases",
          yaxis_title="Days Since Day 0",
          margin=dict(l=0, r=100, t=30, b=0),
      )
    return fig


inputs = [
    gr.Slider(1, 4, 3.2, label="R"),
    gr.Dropdown(["January", "February", "March", "April", "May"], label="Month", value="March"),
    gr.CheckboxGroup(
        ["USA", "Canada", "Mexico", "UK"], label="Countries", value=["USA", "Canada"]
    ),
    gr.Checkbox(label="Social Distancing?"),
]
outputs = gr.Plot()

demo = gr.Interface(
    fn=outbreak,
    inputs=inputs,
    outputs=outputs,
)

demo.launch()

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Oct 22, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Storybook ready! Storybook preview
🦄 Changes detecting...

Install Gradio from this PR

pip install https://gradio-pypi-previews.s3.amazonaws.com/7a4b272f9c24d9ef30c24991091687936de63148/gradio-5.3.0-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@7a4b272f9c24d9ef30c24991091687936de63148#subdirectory=client/python"

Install Gradio JS Client from this PR

npm install https://gradio-npm-previews.s3.amazonaws.com/7a4b272f9c24d9ef30c24991091687936de63148/gradio-client-1.7.0.tgz

Use Lite from this PR

<script type="module" src="https://gradio-lite-previews.s3.amazonaws.com/7a4b272f9c24d9ef30c24991091687936de63148/dist/lite.js""></script>

@gradio-pr-bot
Copy link
Collaborator

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/plot minor
gradio minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

Allow setting plotly margins

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@abidlabs abidlabs requested review from hannahblair and pngwn October 22, 2024 20:43
Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM @aliabid94 ! Tested and works well

@albertvillanova
Copy link

Thanks for allowing passing margins.

However, please note that the bug I reported was not that margins were ignored. My reported bug is that, without specifying margins, some labels are cut off from the resulting figure. See example: https://huggingface.co/spaces/albertvillanova/bug-plotly-line-polar

  • The resulting figure only shows 4 labels: GPQA, IFEval, MMLU-Pro and MuSR.
  • However the plot has 6 labels: the top (BBH) and bottom (MATH) labels are cut off

Code:

import gradio as gr
import pandas as pd
import plotly.express as px


DATA = {"BBH": [0.2, 0.7], "GPQA": [0.4, 0.5], "IFEval": [0.6, 0.3], "MATH": [0.2, 0.7], "MMLU-Pro": [0.4, 0.5], "MuSR": [0.6, 0.3]}


def display_plot():
    df = pd.DataFrame(DATA, index=["model-1", "model-2"])
    fig = px.line_polar(
        df.melt(ignore_index=False, var_name="Benchmark", value_name="Score").reset_index(names="Model"),
        r="Score", theta="Benchmark", color="Model",
        line_close=True,
        range_r=[0, 1],
        color_discrete_sequence=["#FF9D00", "#32343D"],
    )
    return fig


with gr.Blocks() as demo:
    plot = gr.Plot()
    btn = gr.Button()

    btn.click(
        fn=display_plot,
        outputs=plot,
    )

demo.launch()

@hysts
Copy link
Collaborator

hysts commented Oct 25, 2024

@albertvillanova

I think this fixes your issue.
When I run your sample code with the changes in this PR, I get the following:

You can try it yourself by uninstalling gradio and running the pip install command in this comment.

@albertvillanova
Copy link

@hysts, thanks for checking and for the hint how I can check next time! 🤗

@freddyaboulton freddyaboulton merged commit 78e3b51 into main Oct 25, 2024
23 of 24 checks passed
@freddyaboulton freddyaboulton deleted the accept_plotly_margins branch October 25, 2024 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants