def make_ridge_chart(self): self.plot_df['year'] = pd.to_datetime( self.plot_df['year'].astype(str) + '-01-01') brush = alt.selection_interval(encodings=['x']) step = 18 overlap = 7 # Topic selection selection = alt.selection_multi(fields=['Topic']) # смена цвета на выборе темы -- всё что не выделили делается серым color = alt.condition( selection, alt.Color('Topic:N', legend=None, scale=alt.Scale(scheme="rainbow")), alt.value('lightgray')) a = alt.hconcat( alt.Chart(self.plot_df).mark_area( fillOpacity=0.6, interpolate='monotone').encode( x=alt.X('year:T'), y=alt.Y('rate:Q', scale=alt.Scale(range=[0, -overlap * (step + 1)]), axis=None), row=alt.Row('Topic:N', header=alt.Header(title=None, labelPadding=0, labelFontSize=0)), color=color).add_selection(selection).properties( width=800, height=step, bounds='flush', ).transform_filter(brush), alt.Chart(self.plot_df, height=500).mark_point().encode( y=alt.Y('Topic:N', axis=alt.Axis(orient='right')), color=color).add_selection(selection)) b = alt.Chart(self.plot_df).mark_area(interpolate='monotone').encode( y='sum(rate):Q', x='year:T', ).properties(width=800, height=100).add_selection(brush) if self.add_data_names != ['All']: add_charts = [ self.make_add_data_chart(i, brush) for i in self.add_data_names ] chart = alt.vconcat(a, *add_charts, b, padding=0, spacing=0).configure_view( stroke=None).configure_axis(grid=False) return chart else: chart = alt.vconcat(a, b, padding=0, spacing=0).configure_view( stroke=None).configure_axis(grid=False) return chart
def test_selection_to_dict(): brush = alt.selection(type='interval') # test some value selections # Note: X and Y cannot have conditions alt.Chart('path/to/data.json').mark_point().encode( color=alt.condition(brush, alt.ColorValue('red'), alt.ColorValue('blue')), opacity=alt.condition(brush, alt.value(0.5), alt.value(1.0)), text=alt.condition(brush, alt.TextValue('foo'), alt.value('bar'))).to_dict() # test some field selections # Note: X and Y cannot have conditions # Conditions cannot both be fields alt.Chart('path/to/data.json').mark_point().encode( color=alt.condition(brush, alt.Color('col1:N'), alt.value('blue')), opacity=alt.condition(brush, 'col1:N', alt.value(0.5)), text=alt.condition(brush, alt.value('abc'), alt.Text('col2:N')), size=alt.condition(brush, alt.value(20), 'col2:N')).to_dict()
def test_selection_to_dict(): brush = alt.selection(type="interval") # test some value selections # Note: X and Y cannot have conditions alt.Chart("path/to/data.json").mark_point().encode( color=alt.condition(brush, alt.ColorValue("red"), alt.ColorValue("blue")), opacity=alt.condition(brush, alt.value(0.5), alt.value(1.0)), text=alt.condition(brush, alt.TextValue("foo"), alt.value("bar")), ).to_dict() # test some field selections # Note: X and Y cannot have conditions # Conditions cannot both be fields alt.Chart("path/to/data.json").mark_point().encode( color=alt.condition(brush, alt.Color("col1:N"), alt.value("blue")), opacity=alt.condition(brush, "col1:N", alt.value(0.5)), text=alt.condition(brush, alt.value("abc"), alt.Text("col2:N")), size=alt.condition(brush, alt.value(20), "col2:N"), ).to_dict()
rand = np.random.RandomState(42) df = pd.DataFrame({"xval": range(100), "yval": rand.randn(100).cumsum()}) slider1 = alt.binding_range(min=0, max=100, step=1, name="cutoff1:") selector1 = alt.selection_single(name="SelectorName1", fields=["cutoff1"], bind=slider1, init={"cutoff1": 50}) slider2 = alt.binding_range(min=0, max=100, step=1, name="cutoff2:") selector2 = alt.selection_single(name="SelectorName2", fields=["cutoff2"], bind=slider2, init={"cutoff2": 50}) ch_base = (alt.Chart(df).mark_point().encode( x="xval", y="yval", color=alt.condition(alt.datum.xval < selector1.cutoff1, alt.value("red"), alt.value("blue")), )) ch1 = ch_base.add_selection(selector1) ch2 = ch_base.encode( color=alt.condition(alt.datum.xval < selector2.cutoff2, alt.value("red"), alt.value("blue"))).add_selection(selector2) ch1 & ch2