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

Improved plot rendering #8580

Merged
merged 26 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/twenty-jokes-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/plot": patch
"gradio": patch
---

feat:Improved plot rendering
18 changes: 9 additions & 9 deletions demo/native_plots/bar_plot_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ def bar_plot_fn(display):


with gr.Blocks() as bar_plot:
with gr.Row():
with gr.Column():
display = gr.Dropdown(
choices=["simple", "stacked", "grouped", "simple-horizontal", "stacked-horizontal", "grouped-horizontal"],
value="simple",
label="Type of Bar Plot"
)
with gr.Column():
plot = gr.BarPlot(show_label=False, show_actions_button=True)
display = gr.Dropdown(
choices=["simple", "stacked", "grouped", "simple-horizontal", "stacked-horizontal", "grouped-horizontal"],
value="simple",
label="Type of Bar Plot"
)
plot = gr.BarPlot(show_label=False, show_actions_button=True)
display.change(bar_plot_fn, inputs=display, outputs=plot)
bar_plot.load(fn=bar_plot_fn, inputs=display, outputs=plot)

if __name__ == "__main__":
bar_plot.launch()
13 changes: 5 additions & 8 deletions demo/native_plots/line_plot_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,11 @@ def line_plot_fn(dataset):


with gr.Blocks() as line_plot:
with gr.Row():
with gr.Column():
dataset = gr.Dropdown(
choices=["stocks", "climate", "seattle_weather", "gapminder"],
value="stocks",
)
with gr.Column():
plot = gr.LinePlot()
dataset = gr.Dropdown(
choices=["stocks", "climate", "seattle_weather", "gapminder"],
value="stocks",
)
plot = gr.LinePlot()
dataset.change(line_plot_fn, inputs=dataset, outputs=plot)
line_plot.load(fn=line_plot_fn, inputs=dataset, outputs=plot)

Expand Down
7 changes: 2 additions & 5 deletions demo/native_plots/scatter_plot_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ def scatter_plot_fn(dataset):


with gr.Blocks() as scatter_plot:
with gr.Row():
with gr.Column():
dataset = gr.Dropdown(choices=["cars", "iris"], value="cars")
with gr.Column():
plot = gr.ScatterPlot(show_label=False)
dataset = gr.Dropdown(choices=["cars", "iris"], value="cars")
plot = gr.ScatterPlot(show_label=False)
dataset.change(scatter_plot_fn, inputs=dataset, outputs=plot)
scatter_plot.load(fn=scatter_plot_fn, inputs=dataset, outputs=plot)

Expand Down
5 changes: 4 additions & 1 deletion gradio/components/bar_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def __init__(
self.height = height
aliabid94 marked this conversation as resolved.
Show resolved Hide resolved
self.sort = sort
self.show_actions_button = show_actions_button
if label is None and show_label is None:
show_label = False
super().__init__(
value=value,
label=label,
Expand Down Expand Up @@ -234,14 +236,15 @@ def create_plot(
properties["width"] = width

if color:
color_legend_position = color_legend_position or "bottom"
domain = value[color].unique().tolist()
range_ = list(range(len(domain)))
encodings["color"] = {
"field": color,
"type": "nominal",
"scale": {"domain": domain, "range": range_},
"legend": AltairPlot.create_legend(
position=color_legend_position, title=color_legend_title or color
position=color_legend_position, title=color_legend_title
),
}

Expand Down
5 changes: 4 additions & 1 deletion gradio/components/line_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def __init__(
self.width = width
self.height = height
self.show_actions_button = show_actions_button
if label is None and show_label is None:
show_label = False
super().__init__(
value=value,
label=label,
Expand Down Expand Up @@ -234,14 +236,15 @@ def create_plot(
properties["width"] = width

if color:
color_legend_position = color_legend_position or "bottom"
domain = value[color].unique().tolist()
range_ = list(range(len(domain)))
encodings["color"] = {
"field": color,
"type": "nominal",
"scale": {"domain": domain, "range": range_},
"legend": AltairPlot.create_legend(
position=color_legend_position, title=color_legend_title or color
position=color_legend_position, title=color_legend_title
),
}

Expand Down
5 changes: 4 additions & 1 deletion gradio/components/scatter_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def __init__(
self.x_lim = x_lim
self.y_lim = y_lim
self.show_actions_button = show_actions_button
if label is None and show_label is None:
show_label = False
super().__init__(
value=value,
label=label,
Expand Down Expand Up @@ -273,11 +275,12 @@ def create_plot(
range_ = list(range(len(domain)))
type_ = "nominal"

color_legend_position = color_legend_position or "bottom"
encodings["color"] = {
"field": color,
"type": type_,
"legend": AltairPlot.create_legend(
position=color_legend_position, title=color_legend_title or color
position=color_legend_position, title=color_legend_title
),
"scale": {"domain": domain, "range": range_},
}
Expand Down
4 changes: 2 additions & 2 deletions js/plot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"@gradio/utils": "workspace:^",
"@rollup/plugin-json": "^6.0.0",
"plotly.js-dist-min": "^2.10.1",
"svelte-vega": "^2.0.0",
"vega": "^5.22.1",
"vega": "^5.23.0",
"vega-embed": "^6.25.0",
"vega-lite": "^5.12.0"
},
"devDependencies": {
Expand Down
Loading