masa729406 commited on
Commit
4f59094
ยท
1 Parent(s): 6a2c3c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -133
app.py CHANGED
@@ -1,140 +1,34 @@
1
- import altair as alt
2
- import gradio as gr
3
- import numpy as np
4
- import pandas as pd
5
- from vega_datasets import data
6
-
7
-
8
- def make_plot(plot_type):
9
- if plot_type == "scatter_plot":
10
- cars = data.cars()
11
- return alt.Chart(cars).mark_point().encode(
12
- x='Horsepower',
13
- y='Miles_per_Gallon',
14
- color='Origin',
15
- )
16
- elif plot_type == "heatmap":
17
- # Compute x^2 + y^2 across a 2D grid
18
- x, y = np.meshgrid(range(-5, 5), range(-5, 5))
19
- z = x ** 2 + y ** 2
20
-
21
- # Convert this grid to columnar data expected by Altair
22
- source = pd.DataFrame({'x': x.ravel(),
23
- 'y': y.ravel(),
24
- 'z': z.ravel()})
25
- return alt.Chart(source).mark_rect().encode(
26
- x='x:O',
27
- y='y:O',
28
- color='z:Q'
29
- )
30
- elif plot_type == "us_map":
31
- states = alt.topo_feature(data.us_10m.url, 'states')
32
- source = data.income.url
33
-
34
- return alt.Chart(source).mark_geoshape().encode(
35
- shape='geo:G',
36
- color='pct:Q',
37
- tooltip=['name:N', 'pct:Q'],
38
- facet=alt.Facet('group:N', columns=2),
39
- ).transform_lookup(
40
- lookup='id',
41
- from_=alt.LookupData(data=states, key='id'),
42
- as_='geo'
43
- ).properties(
44
- width=300,
45
- height=175,
46
- ).project(
47
- type='albersUsa'
48
- )
49
- elif plot_type == "interactive_barplot":
50
- source = data.movies.url
51
-
52
- pts = alt.selection(type="single", encodings=['x'])
53
-
54
- rect = alt.Chart(data.movies.url).mark_rect().encode(
55
- alt.X('IMDB_Rating:Q', bin=True),
56
- alt.Y('Rotten_Tomatoes_Rating:Q', bin=True),
57
- alt.Color('count()',
58
- scale=alt.Scale(scheme='greenblue'),
59
- legend=alt.Legend(title='Total Records')
60
- )
61
- )
62
-
63
- circ = rect.mark_point().encode(
64
- alt.ColorValue('grey'),
65
- alt.Size('count()',
66
- legend=alt.Legend(title='Records in Selection')
67
- )
68
- ).transform_filter(
69
- pts
70
- )
71
-
72
- bar = alt.Chart(source).mark_bar().encode(
73
- x='Major_Genre:N',
74
- y='count()',
75
- color=alt.condition(pts, alt.ColorValue("steelblue"), alt.ColorValue("grey"))
76
- ).properties(
77
- width=550,
78
- height=200
79
- ).add_selection(pts)
80
 
81
- plot = alt.vconcat(
82
- rect + circ,
83
- bar
84
- ).resolve_legend(
85
- color="independent",
86
- size="independent"
87
- )
88
- return plot
89
- elif plot_type == "radial":
90
- source = pd.DataFrame({"values": [12, 23, 47, 6, 52, 19]})
91
-
92
- base = alt.Chart(source).encode(
93
- theta=alt.Theta("values:Q", stack=True),
94
- radius=alt.Radius("values", scale=alt.Scale(type="sqrt", zero=True, rangeMin=20)),
95
- color="values:N",
96
- )
97
-
98
- c1 = base.mark_arc(innerRadius=20, stroke="#fff")
99
-
100
- c2 = base.mark_text(radiusOffset=10).encode(text="values:Q")
101
-
102
- return c1 + c2
103
- elif plot_type == "multiline":
104
- source = data.stocks()
105
-
106
- highlight = alt.selection(type='single', on='mouseover',
107
- fields=['symbol'], nearest=True)
108
-
109
- base = alt.Chart(source).encode(
110
- x='date:T',
111
- y='price:Q',
112
- color='symbol:N'
113
- )
114
-
115
- points = base.mark_circle().encode(
116
- opacity=alt.value(0)
117
- ).add_selection(
118
- highlight
119
- ).properties(
120
- width=600
121
- )
122
-
123
- lines = base.mark_line().encode(
124
- size=alt.condition(~highlight, alt.value(1), alt.value(3))
125
- )
126
 
127
- return points + lines
128
 
 
 
 
 
129
 
130
- with gr.Blocks() as demo:
131
- button = gr.Radio(label="Plot type",
132
- choices=['scatter_plot', 'heatmap', 'us_map',
133
- 'interactive_barplot', "radial", "multiline"], value='scatter_plot')
134
- plot = gr.Plot(label="Plot")
135
- button.change(make_plot, inputs=button, outputs=[plot])
136
- demo.load(make_plot, inputs=[button], outputs=[plot])
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
  if __name__ == "__main__":
140
- demo.launch()
 
1
+ import altair
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ import gradio as gr
4
+ from math import sqrt
5
+ import matplotlib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ matplotlib.use("Agg")
8
 
9
+ import matplotlib.pyplot as plt
10
+ import numpy as np
11
+ import plotly.express as px
12
+ import pandas as pd
13
 
 
 
 
 
 
 
 
14
 
15
+ def outbreak(date):
16
+ if date:
17
+ # start_day = start_day
18
+ # final_day = date
19
+ # x = np.arange(start_day, final_day + 1)
20
+ fig = plt.figure()
21
+ plt.plot(y)
22
+ plt.title("่ฆณๅฎขๆ•ฐไบˆๆธฌ")
23
+ plt.ylabel("่ฆณๅฎขๆ•ฐ")
24
+ plt.xlabel("Days since Day 0")
25
+ return fig
26
+
27
+ demo = gr.Interface(
28
+ fn=outbreak,
29
+ inputs='checkbox',
30
+ outputs=gr.Plot()
31
+ )
32
 
33
  if __name__ == "__main__":
34
+ demo.launch()