aliabd commited on
Commit
adaa8b9
·
1 Parent(s): 18a4d91

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +8 -8
  2. requirements.txt +2 -0
  3. run.ipynb +1 -0
  4. run.py +91 -0
README.md CHANGED
@@ -1,12 +1,12 @@
 
1
  ---
2
- title: Bokeh Plot 3-x
3
- emoji: 📊
4
- colorFrom: blue
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 4.3.0
8
- app_file: app.py
9
  pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+
2
  ---
3
+ title: bokeh_plot_3-x
4
+ emoji: 🔥
5
+ colorFrom: indigo
6
+ colorTo: indigo
7
  sdk: gradio
8
+ sdk_version: 3.50.1
9
+ app_file: run.py
10
  pinned: false
11
+ hf_oauth: true
12
  ---
 
 
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ bokeh>=3.0
2
+ xyzservices
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: bokeh_plot"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio bokeh>=3.0 xyzservices"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import xyzservices.providers as xyz\n", "from bokeh.models import ColumnDataSource, Whisker\n", "from bokeh.plotting import figure\n", "from bokeh.sampledata.autompg2 import autompg2 as df\n", "from bokeh.sampledata.penguins import data\n", "from bokeh.transform import factor_cmap, jitter, factor_mark\n", "\n", "\n", "def get_plot(plot_type):\n", " if plot_type == \"map\":\n", " plot = figure(\n", " x_range=(-2000000, 6000000),\n", " y_range=(-1000000, 7000000),\n", " x_axis_type=\"mercator\",\n", " y_axis_type=\"mercator\",\n", " )\n", " plot.add_tile(xyz.OpenStreetMap.Mapnik)\n", " return plot\n", " elif plot_type == \"whisker\":\n", " classes = list(sorted(df[\"class\"].unique()))\n", "\n", " p = figure(\n", " height=400,\n", " x_range=classes,\n", " background_fill_color=\"#efefef\",\n", " title=\"Car class vs HWY mpg with quintile ranges\",\n", " )\n", " p.xgrid.grid_line_color = None\n", "\n", " g = df.groupby(\"class\")\n", " upper = g.hwy.quantile(0.80)\n", " lower = g.hwy.quantile(0.20)\n", " source = ColumnDataSource(data=dict(base=classes, upper=upper, lower=lower))\n", "\n", " error = Whisker(\n", " base=\"base\",\n", " upper=\"upper\",\n", " lower=\"lower\",\n", " source=source,\n", " level=\"annotation\",\n", " line_width=2,\n", " )\n", " error.upper_head.size = 20\n", " error.lower_head.size = 20\n", " p.add_layout(error)\n", "\n", " p.circle(\n", " jitter(\"class\", 0.3, range=p.x_range),\n", " \"hwy\",\n", " source=df,\n", " alpha=0.5,\n", " size=13,\n", " line_color=\"white\",\n", " color=factor_cmap(\"class\", \"Light6\", classes),\n", " )\n", " return p\n", " elif plot_type == \"scatter\":\n", "\n", " SPECIES = sorted(data.species.unique())\n", " MARKERS = [\"hex\", \"circle_x\", \"triangle\"]\n", "\n", " p = figure(title=\"Penguin size\", background_fill_color=\"#fafafa\")\n", " p.xaxis.axis_label = \"Flipper Length (mm)\"\n", " p.yaxis.axis_label = \"Body Mass (g)\"\n", "\n", " p.scatter(\n", " \"flipper_length_mm\",\n", " \"body_mass_g\",\n", " source=data,\n", " legend_group=\"species\",\n", " fill_alpha=0.4,\n", " size=12,\n", " marker=factor_mark(\"species\", MARKERS, SPECIES),\n", " color=factor_cmap(\"species\", \"Category10_3\", SPECIES),\n", " )\n", "\n", " p.legend.location = \"top_left\"\n", " p.legend.title = \"Species\"\n", " return p\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " plot_type = gr.Radio(value=\"scatter\", choices=[\"scatter\", \"whisker\", \"map\"])\n", " plot = gr.Plot()\n", " plot_type.change(get_plot, inputs=[plot_type], outputs=[plot])\n", " demo.load(get_plot, inputs=[plot_type], outputs=[plot])\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import xyzservices.providers as xyz
3
+ from bokeh.models import ColumnDataSource, Whisker
4
+ from bokeh.plotting import figure
5
+ from bokeh.sampledata.autompg2 import autompg2 as df
6
+ from bokeh.sampledata.penguins import data
7
+ from bokeh.transform import factor_cmap, jitter, factor_mark
8
+
9
+
10
+ def get_plot(plot_type):
11
+ if plot_type == "map":
12
+ plot = figure(
13
+ x_range=(-2000000, 6000000),
14
+ y_range=(-1000000, 7000000),
15
+ x_axis_type="mercator",
16
+ y_axis_type="mercator",
17
+ )
18
+ plot.add_tile(xyz.OpenStreetMap.Mapnik)
19
+ return plot
20
+ elif plot_type == "whisker":
21
+ classes = list(sorted(df["class"].unique()))
22
+
23
+ p = figure(
24
+ height=400,
25
+ x_range=classes,
26
+ background_fill_color="#efefef",
27
+ title="Car class vs HWY mpg with quintile ranges",
28
+ )
29
+ p.xgrid.grid_line_color = None
30
+
31
+ g = df.groupby("class")
32
+ upper = g.hwy.quantile(0.80)
33
+ lower = g.hwy.quantile(0.20)
34
+ source = ColumnDataSource(data=dict(base=classes, upper=upper, lower=lower))
35
+
36
+ error = Whisker(
37
+ base="base",
38
+ upper="upper",
39
+ lower="lower",
40
+ source=source,
41
+ level="annotation",
42
+ line_width=2,
43
+ )
44
+ error.upper_head.size = 20
45
+ error.lower_head.size = 20
46
+ p.add_layout(error)
47
+
48
+ p.circle(
49
+ jitter("class", 0.3, range=p.x_range),
50
+ "hwy",
51
+ source=df,
52
+ alpha=0.5,
53
+ size=13,
54
+ line_color="white",
55
+ color=factor_cmap("class", "Light6", classes),
56
+ )
57
+ return p
58
+ elif plot_type == "scatter":
59
+
60
+ SPECIES = sorted(data.species.unique())
61
+ MARKERS = ["hex", "circle_x", "triangle"]
62
+
63
+ p = figure(title="Penguin size", background_fill_color="#fafafa")
64
+ p.xaxis.axis_label = "Flipper Length (mm)"
65
+ p.yaxis.axis_label = "Body Mass (g)"
66
+
67
+ p.scatter(
68
+ "flipper_length_mm",
69
+ "body_mass_g",
70
+ source=data,
71
+ legend_group="species",
72
+ fill_alpha=0.4,
73
+ size=12,
74
+ marker=factor_mark("species", MARKERS, SPECIES),
75
+ color=factor_cmap("species", "Category10_3", SPECIES),
76
+ )
77
+
78
+ p.legend.location = "top_left"
79
+ p.legend.title = "Species"
80
+ return p
81
+
82
+ with gr.Blocks() as demo:
83
+ with gr.Row():
84
+ plot_type = gr.Radio(value="scatter", choices=["scatter", "whisker", "map"])
85
+ plot = gr.Plot()
86
+ plot_type.change(get_plot, inputs=[plot_type], outputs=[plot])
87
+ demo.load(get_plot, inputs=[plot_type], outputs=[plot])
88
+
89
+
90
+ if __name__ == "__main__":
91
+ demo.launch()