πΈΒ Β Altex
Submitted by Arnaud Miribel
Summary
A simple wrapper on top of Altair to make Streamlit charts in an express API. If you're lazy and/or familiar with Altair, this is probably a good fit! Inspired by plost and plotly-express.
Functions
_chart
Create an Altair chart with a simple API. Supported charts include line, bar, point, area, histogram, sparkline, sparkbar, sparkarea.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mark_function |
str
|
Altair mark function, example line/bar/point |
required |
data |
DataFrame
|
Dataframe to use for the chart |
required |
x |
Union[X, str]
|
Column for the x axis |
required |
y |
Union[Y, str]
|
Column for the y axis |
required |
color |
Optional[Union[Color, str]]
|
Color a specific group of your data. Defaults to None. |
None
|
opacity |
Optional[Union[value, float]]
|
Change opacity of marks. Defaults to None. |
None
|
column |
Optional[Union[Column, str]]
|
Groupby a specific column. Defaults to None. |
None
|
rolling |
Optional[int]
|
Rolling average window size. Defaults to None. |
None
|
title |
Optional[str]
|
Title of the chart. Defaults to None. |
None
|
width |
Optional[int]
|
Width of the chart. Defaults to None. |
None
|
height |
Optional[int]
|
Height of the chart. Defaults to None. |
None
|
spark |
bool
|
Whether or not to make spark chart, i.e. a chart without axes nor ticks nor legend. Defaults to False. |
False
|
autoscale_y |
bool
|
Whether or not to autoscale the y axis. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Chart
|
alt.Chart: Altair chart |
Source code in src/streamlit_extras/altex/__init__.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
Import:
- You should add this to the top of your .py file
scatter_chart
Import:
- You should add this to the top of your .py file
Examples
example_line
@cache_data
def example_line():
stocks = get_stocks_data()
line_chart(
data=stocks.query("symbol == 'GOOG'"),
x="date",
y="price",
title="A beautiful simple line chart",
)
example_multi_line
@cache_data
def example_multi_line():
stocks = get_stocks_data()
line_chart(
data=stocks,
x="date",
y="price",
color="symbol",
title="A beautiful multi line chart",
)
example_bar
@cache_data
def example_bar():
stocks = get_stocks_data()
bar_chart(
data=stocks.query("symbol == 'GOOG'"),
x="date",
y="price",
title="A beautiful bar chart",
)
example_hist
@cache_data
def example_hist():
stocks = get_stocks_data()
hist_chart(
data=stocks.assign(price=stocks.price.round(0)),
x="price",
title="A beautiful histogram",
)
example_scatter
@cache_data
def example_scatter():
weather = get_weather_data()
scatter_chart(
data=weather,
x=alt.X("wind:Q", title="Custom X title"),
y=alt.Y("temp_min:Q", title="Custom Y title"),
title="A beautiful scatter chart",
)
example_sparkline
@cache_data
def example_sparkline():
stocks = get_stocks_data()
sparkline_chart(
data=stocks.query("symbol == 'GOOG'"),
x="date",
y="price",
title="A beautiful sparkline chart",
rolling=7,
height=150,
)
example_minisparklines
@cache_data
def example_minisparklines():
stocks = get_stocks_data()
left, middle, right = st.columns(3)
with left:
data = stocks.query("symbol == 'GOOG'")
st.metric("GOOG", int(data["price"].mean()))
sparkline_chart(
data=data,
x="date",
y="price:Q",
height=80,
autoscale_y=True,
)
with middle:
data = stocks.query("symbol == 'MSFT'")
st.metric("MSFT", int(data["price"].mean()))
sparkline_chart(
data=data,
x="date",
y="price:Q",
height=80,
autoscale_y=True,
)
with right:
data = stocks.query("symbol == 'AAPL'")
st.metric("AAPL", int(data["price"].mean()))
sparkline_chart(
data=data,
x="date",
y="price:Q",
height=80,
autoscale_y=True,
)
example_sparkbar
@cache_data
def example_sparkbar():
stocks = get_stocks_data()
sparkbar_chart(
data=stocks.query("symbol == 'GOOG'"),
x="date",
y="price",
title="A beautiful sparkbar chart",
height=150,
)
example_sparkarea
@cache_data
def example_sparkarea():
random_data = get_random_data()
df = pd.melt(
random_data,
id_vars="index",
value_vars=list("abcdefg"),
)
sparkarea_chart(
data=df,
x="index",
y="value",
color=alt.Color("variable", legend=None),
title="A beautiful (also probably useless) sparkarea chart",
opacity=alt.value(0.6),
height=200,
)
example_hist_time
@cache_data
def example_hist_time():
weather = get_weather_data()
hist_chart(
data=weather,
x="week(date):T",
y="day(date):T",
color=alt.Color(
"median(temp_max):Q",
legend=None,
),
title="A beautiful time hist chart",
)
example_bar_sorted
@cache_data
def example_bar_sorted():
weather = get_weather_data()
bar_chart(
data=weather.sort_values(by="temp_max", ascending=False).head(25),
x=alt.X("date", sort="-y"),
y=alt.Y("temp_max:Q"),
title="A beautiful sorted-by-value bar chart",
)
example_bar_normalized
@cache_data
def example_bar_normalized():
barley = get_barley_data()
bar_chart(
data=barley,
x=alt.X("variety:N", title="Variety"),
y=alt.Y("sum(yield):Q", stack="normalize"),
color="site:N",
title="A beautiful normalized stacked bar chart",
)
example_bar_grouped
@cache_data
def example_bar_grouped():
barley = get_barley_data()
bar_chart(
data=barley,
x="year:O",
y="sum(yield):Q",
color="year:N",
column="site:N",
title="A beautiful grouped bar charts",
width=90,
use_container_width=False,
)
example_bar_horizontal
@cache_data
def example_bar_horizontal():
weather = get_weather_data()
bar_chart(
data=weather.head(15),
x="temp_max:Q",
y=alt.Y("date:O", title="Temperature"),
title="A beautiful horizontal bar chart",
)
example_bar_log
@cache_data
def example_bar_log():
weather = get_weather_data()
bar_chart(
data=weather,
x=alt.X("temp_max:Q", title="Temperature"),
y=alt.Y(
"count()",
title="Count of records",
scale=alt.Scale(type="symlog"),
),
title="A beautiful histogram... with log scale",
)
example_scatter_opacity
@cache_data
def example_scatter_opacity():
weather = get_weather_data()
scatter_chart(
data=weather,
x=alt.X("wind:Q", title="Custom X title"),
y=alt.Y("temp_min:Q", title="Custom Y title"),
title="A beautiful scatter chart with custom opacity",
opacity=0.2,
)
example_bar_normalized_custom
@cache_data
def example_bar_normalized_custom():
barley = get_barley_data()
bar_chart(
data=barley,
x=alt.X("variety", title="Variety"),
y="sum(yield)",
color=alt.Color("site", scale=alt.Scale(scheme="lighttealblue"), legend=None),
title="A beautiful stacked bar chart (without legend, custom colors)",
)