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|
| 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()
|
| 141 |
+
|
| 142 |
+
|