Spaces:
Sleeping
Sleeping
Commit ·
ddf36f7
1
Parent(s): 4bd2145
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import xyzservices.providers as xyz
|
| 3 |
+
from bokeh.plotting import figure
|
| 4 |
+
from bokeh.tile_providers import get_provider
|
| 5 |
+
from bokeh.models import ColumnDataSource, Whisker
|
| 6 |
+
from bokeh.plotting import figure
|
| 7 |
+
from bokeh.sampledata.autompg2 import autompg2 as df
|
| 8 |
+
from bokeh.sampledata.penguins import data
|
| 9 |
+
from bokeh.transform import factor_cmap, jitter, factor_mark
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def get_plot(plot_type):
|
| 13 |
+
if plot_type == "map":
|
| 14 |
+
tile_provider = get_provider(xyz.OpenStreetMap.Mapnik)
|
| 15 |
+
plot = figure(
|
| 16 |
+
x_range=(-2000000, 6000000),
|
| 17 |
+
y_range=(-1000000, 7000000),
|
| 18 |
+
x_axis_type="mercator",
|
| 19 |
+
y_axis_type="mercator",
|
| 20 |
+
)
|
| 21 |
+
plot.add_tile(tile_provider)
|
| 22 |
+
return plot
|
| 23 |
+
elif plot_type == "whisker":
|
| 24 |
+
classes = list(sorted(df["class"].unique()))
|
| 25 |
+
|
| 26 |
+
p = figure(
|
| 27 |
+
height=400,
|
| 28 |
+
x_range=classes,
|
| 29 |
+
background_fill_color="#efefef",
|
| 30 |
+
title="Car class vs HWY mpg with quintile ranges",
|
| 31 |
+
)
|
| 32 |
+
p.xgrid.grid_line_color = None
|
| 33 |
+
|
| 34 |
+
g = df.groupby("class")
|
| 35 |
+
upper = g.hwy.quantile(0.80)
|
| 36 |
+
lower = g.hwy.quantile(0.20)
|
| 37 |
+
source = ColumnDataSource(data=dict(base=classes, upper=upper, lower=lower))
|
| 38 |
+
|
| 39 |
+
error = Whisker(
|
| 40 |
+
base="base",
|
| 41 |
+
upper="upper",
|
| 42 |
+
lower="lower",
|
| 43 |
+
source=source,
|
| 44 |
+
level="annotation",
|
| 45 |
+
line_width=2,
|
| 46 |
+
)
|
| 47 |
+
error.upper_head.size = 20
|
| 48 |
+
error.lower_head.size = 20
|
| 49 |
+
p.add_layout(error)
|
| 50 |
+
|
| 51 |
+
p.circle(
|
| 52 |
+
jitter("class", 0.3, range=p.x_range),
|
| 53 |
+
"hwy",
|
| 54 |
+
source=df,
|
| 55 |
+
alpha=0.5,
|
| 56 |
+
size=13,
|
| 57 |
+
line_color="white",
|
| 58 |
+
color=factor_cmap("class", "Light6", classes),
|
| 59 |
+
)
|
| 60 |
+
return p
|
| 61 |
+
elif plot_type == "scatter":
|
| 62 |
+
|
| 63 |
+
SPECIES = sorted(data.species.unique())
|
| 64 |
+
MARKERS = ["hex", "circle_x", "triangle"]
|
| 65 |
+
|
| 66 |
+
p = figure(title="Penguin size", background_fill_color="#fafafa")
|
| 67 |
+
p.xaxis.axis_label = "Flipper Length (mm)"
|
| 68 |
+
p.yaxis.axis_label = "Body Mass (g)"
|
| 69 |
+
|
| 70 |
+
p.scatter(
|
| 71 |
+
"flipper_length_mm",
|
| 72 |
+
"body_mass_g",
|
| 73 |
+
source=data,
|
| 74 |
+
legend_group="species",
|
| 75 |
+
fill_alpha=0.4,
|
| 76 |
+
size=12,
|
| 77 |
+
marker=factor_mark("species", MARKERS, SPECIES),
|
| 78 |
+
color=factor_cmap("species", "Category10_3", SPECIES),
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
p.legend.location = "top_left"
|
| 82 |
+
p.legend.title = "Species"
|
| 83 |
+
return p
|
| 84 |
+
|
| 85 |
+
with gr.Blocks() as demo:
|
| 86 |
+
with gr.Row():
|
| 87 |
+
plot_type = gr.Radio(value="scatter", choices=["scatter", "whisker", "map"])
|
| 88 |
+
plot = gr.Plot()
|
| 89 |
+
plot_type.change(get_plot, inputs=[plot_type], outputs=[plot])
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
demo.launch()
|