Spaces:
Sleeping
Sleeping
File size: 3,455 Bytes
e88abf9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import solara
import plotly.express as px
from utils import selected_template
@solara.component
def BarPlot():
with solara.Column(gap="10px",align="stretch" ) as main:
solara.Markdown(f"#BAR PLOTS")
# Get population change in US by querying for US data
df_us = px.data.gapminder().query("country == 'United States'")
fig1 = px.bar(df_us, x='year', y='pop',template=selected_template.value)
solara.FigurePlotly(fig1)
# Create a stacked bar with more customization
df_tips = px.data.tips()
fig2 = px.bar(df_tips, x='day', y='tip', color='sex', title='Tips by Sex on Each Day',template=selected_template.value,
labels={'tip': 'Tip Amount', 'day': 'Day of the Week'})
solara.Markdown(f"###Create a stacked bar with more customization")
solara.Markdown("""
```python
fig2 = px.bar(df_tips, x='day', y='tip', color='sex', title='Tips by Sex on Each Day',
labels={'tip': 'Tip Amount', 'day': 'Day of the Week'})
```
"""
)
solara.FigurePlotly(fig2)
# Place bars next to each other
fig3 = px.bar(df_tips, x="sex", y="total_bill",template=selected_template.value,
color='smoker', barmode='group')
solara.Markdown(f"###Place bars next to each other")
solara.Markdown("""
```python
fig3 = px.bar(df_tips, x="sex", y="total_bill",
color='smoker', barmode='group')
```
"""
)
solara.FigurePlotly(fig3)
# Display pop data for countries in Europe in 2007 greater than 2000000
df_europe = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
fig4 = px.bar(df_europe, y='pop', x='country', text='pop', color='country',template=selected_template.value)
# Put bar total value above bars with 2 values of precision
fig4.update_traces(texttemplate='%{text:.2s}', textposition='outside')
# Set fontsize and uniformtext_mode='hide' says to hide the text if it won't fit
fig4.update_layout(uniformtext_minsize=8)
# Rotate labels 45 degrees
fig4.update_layout(xaxis_tickangle=-45)
solara.Markdown(f"###Display pop data for countries in Europe in 2007 greater than 2000000")
solara.Markdown("""
```python
df_europe = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")
fig4 = px.bar(df_europe, y='pop', x='country', text='pop', color='country')
# Put bar total value above bars with 2 values of precision
fig4.update_traces(texttemplate='%{text:.2s}', textposition='outside')
# Set fontsize and uniformtext_mode='hide' says to hide the text if it won't fit
fig4.update_layout(uniformtext_minsize=8)
# Rotate labels 45 degrees
fig4.update_layout(xaxis_tickangle=-45)
```
"""
)
solara.FigurePlotly(fig4)
return main
|