def test_chart_infer_types(): data = pd.DataFrame({'x': pd.date_range('2012', periods=10, freq='Y'), 'y': range(10), 'c': list('abcabcabca')}) def _check_encodings(chart): dct = chart.to_dict() assert dct['encoding']['x']['type'] == 'temporal' assert dct['encoding']['x']['field'] == 'x' assert dct['encoding']['y']['type'] == 'quantitative' assert dct['encoding']['y']['field'] == 'y' assert dct['encoding']['color']['type'] == 'nominal' assert dct['encoding']['color']['field'] == 'c' # Pass field names by keyword chart = alt.Chart(data).mark_point().encode(x='x', y='y', color='c') _check_encodings(chart) # pass Channel objects by keyword chart = alt.Chart(data).mark_point().encode(x=alt.X('x'), y=alt.Y('y'), color=alt.Color('c')) _check_encodings(chart) # pass Channel objects by value chart = alt.Chart(data).mark_point().encode(alt.X('x'), alt.Y('y'), alt.Color('c')) _check_encodings(chart) # override default types chart = alt.Chart(data).mark_point().encode(alt.X('x', type='nominal'), alt.Y('y', type='ordinal')) dct = chart.to_dict() assert dct['encoding']['x']['type'] == 'nominal' assert dct['encoding']['y']['type'] == 'ordinal'
def make_bump_chart(self): self.plot_df['year'] = pd.to_datetime( self.plot_df['year'].astype(str) + '-01-01') brush = alt.selection(type='interval', encodings=['x']) a = alt.Chart(self.plot_df).mark_line( point=True, interpolate='monotone').encode( x='year:T', y='rank:Q', color=alt.Color('Topic:N', sort='descending'), tooltip=['year:T', 'rank:Q', 'Topic:N', 'rate']).properties( height=400, width=800).transform_filter(brush).interactive() b = alt.Chart(self.plot_df).mark_area().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 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_chart_infer_types(): data = pd.DataFrame({ "x": pd.date_range("2012", periods=10, freq="Y"), "y": range(10), "c": list("abcabcabca"), }) def _check_encodings(chart): dct = chart.to_dict() assert dct["encoding"]["x"]["type"] == "temporal" assert dct["encoding"]["x"]["field"] == "x" assert dct["encoding"]["y"]["type"] == "quantitative" assert dct["encoding"]["y"]["field"] == "y" assert dct["encoding"]["color"]["type"] == "nominal" assert dct["encoding"]["color"]["field"] == "c" # Pass field names by keyword chart = alt.Chart(data).mark_point().encode(x="x", y="y", color="c") _check_encodings(chart) # pass Channel objects by keyword chart = (alt.Chart(data).mark_point().encode(x=alt.X("x"), y=alt.Y("y"), color=alt.Color("c"))) _check_encodings(chart) # pass Channel objects by value chart = alt.Chart(data).mark_point().encode(alt.X("x"), alt.Y("y"), alt.Color("c")) _check_encodings(chart) # override default types chart = (alt.Chart(data).mark_point().encode(alt.X("x", type="nominal"), alt.Y("y", type="ordinal"))) dct = chart.to_dict() assert dct["encoding"]["x"]["type"] == "nominal" assert dct["encoding"]["y"]["type"] == "ordinal"
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()