How to group facet_col with px.bar?

I wanna show only one " Building " under middle bar graph.

import streamlit as st
import pandas as pd
import numpy as np
import plotly.express as px
import altair as alt
import plotly.graph_objects as go
from query import *

st.set_page_config(
page_title=“True Digital Park Report”,
layout=“wide”,
page_icon=“”
)

@st.cache_data
def get_data_property():

cols = [‘Event_start’, ‘Year’, ‘Month’, ‘Quarter’, ‘Building’]

return pd.read_csv( ‘data/true_data.csv’, parse_dates=[‘Event_start’])

df = get_data_property()

year_filter = st.selectbox(“Select the Year”, pd.unique(df[‘Year’]))

df = df[df[‘Year’] == year_filter]

df

df[‘Month_of_Year’] = df[‘Event_start’].dt.month
df[‘Day’] = df[‘Event_start’].dt.day_name()
df[‘Day_of_week’] = df[‘Event_start’].dt.day_of_week

year=st.sidebar.multiselect(
“SELECT YEAR”,
options=df.sort_values(by=‘Year’, ascending=True)[‘Year’].unique(),
default=df.sort_values(by=‘Year’, ascending=True)[‘Year’].unique()
)

month=st.sidebar.multiselect(
“SELECT MONTH”,
options=df.sort_values(by=‘Month_of_Year’, ascending=True)[‘Month’].unique(),
default=df.sort_values(by=‘Month_of_Year’, ascending=True)[‘Month’].unique()
)

quarter=st.sidebar.multiselect(
“SELECT QUARTOR”,
options=df.sort_values(by=‘Quarter’, ascending=True)[‘Quarter’].unique(),
default=df.sort_values(by=‘Quarter’, ascending=True)[‘Quarter’].unique(),
)

building=st.sidebar.multiselect(
“SELECT Building”,
options=df.sort_values(by=‘Building’, ascending=True)[‘Building’].unique(),
default=df.sort_values(by=‘Building’, ascending=True)[‘Building’].unique(),
)

day=st.sidebar.multiselect(
“SELECT Day of Week”,
options=df.sort_values(by=‘Day_of_week’, ascending=True)[‘Day’].unique(),
default=df.sort_values(by=‘Day_of_week’, ascending=True)[‘Day’].unique(),
)

df_selection = df.query(
“Year==@year & Month==@month & Quarter==@quarter & Building==@building & Day==@day”
)

df2 = df_selection.groupby([‘Quarter’, ‘Month’, ‘Year’])[‘Event_name’].count().reset_index()
df3 = df_selection.groupby([‘Year’, ‘Building’])[‘Event_name’].count().reset_index()
df4 = df_selection.groupby([‘Month’, ‘Day’])[‘Event_name’].count().reset_index()
df5 = df_selection.groupby([ ‘Month’, ‘Year’, ‘Type_event’])[‘Event_name’].count().reset_index()

ordered_months = [“January”, “February”, “March”, “April”, “May”, “June”,
“July”, “August”, “September”, “October”, “November”, “December”]

DF 2

for axis in fig.layout:

if axis.startswith(“xaxis”) or axis.startswith(“yaxis”):

fig.layout[axis].title = “”

def month_and_quarter():
fig = px.bar(
df2,
x=“Quarter”,
y=“Month”,
color=“Month”,
# facet_col=“Year”,
text_auto=‘.2s’,
# barmode=“group”,
labels={“Event_name”: “Event Count”},
category_orders={“Month”: [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”]},
title=“Number of event by month or quarter”
)

fig.update_layout(showlegend=True)

for axis in fig.layout:
if axis.startswith(“xaxis”) or axis.startswith(“yaxis”):
fig.layout[axis].title = “”

fig.update_layout(

xaxis_title=“x Axis Title”,

yaxis_title=“y Axis Title”,

)

fig.update_layout(

xaxis=dict(title=“Categories”),

legend_title=“Legend Title”,

)

st.plotly_chart(fig, use_container_width=True)

month_and_quarter()

DF 3

def month_and_quarter():
fig = px.bar(
df3,
x=“Building”,
y=“Event_name”,
color=“Building”,
facet_col=“Year”,
text_auto=‘.2s’,
labels={“Event_name”: “Event Count”},
title=“Number of event by year compare with another yeart”
)
for axis in fig.layout:
if axis.startswith(“xaxis”) or axis.startswith(“yaxis”):
fig.layout[axis].title = “”
fig.add_annotation(x=0.5, y=-0.1,xref=“paper”,yref=“paper”,
text=“Building”,
showarrow=False,)
fig.update_layout(showlegend=True)

st.plotly_chart(fig, use_container_width=True)

month_and_quarter()

DF 4

def month_and_quarter():
fig = px.bar(
df4,
x=“Event_name”,
y=“Day”,
color=“Day”,
facet_col=“Month”,
text_auto=‘.2s’,
labels={“Event_name”: “Event Count”},
category_orders={“Day”: [“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”]},
title=“Number of event in each day by month”
)
for axis in fig.layout:
if axis.startswith(“xaxis”) or axis.startswith(“yaxis”):
fig.layout[axis].title = “”
fig.update_layout(showlegend=True)
st.plotly_chart(fig, use_container_width=True)

month_and_quarter()

DF 4

def month_and_quarter2():
fig = px.bar(
df4,
x=“Day”,
y=“Event_name”,
color=“Day”,
facet_col=“Month”,
text_auto=‘.2s’,
labels={“Event_name”: “Event Count”},
category_orders={
“Day”: [“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”],
“Month”: [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”]
},
title=“Number of event in each day by month”
)
for axis in fig.layout:
if axis.startswith(“xaxis”) or axis.startswith(“yaxis”):
fig.layout[axis].title = “”
fig.update_layout(showlegend=True)
st.plotly_chart(fig, use_container_width=True)

month_and_quarter2()

DF 5

def event_type_of_year():
fig = px.bar(
df5,
height=800,
x=“Event_name”,
y=“Type_event”,
color=“Type_event”,
facet_col=“Year”,
text_auto=‘.2s’,
labels={“Type_event”: “Type Event”, “Event_name”: “Event Count”},
title=“Type of event by month”,
)
for axis in fig.layout:
if axis.startswith(“xaxis”) or axis.startswith(“yaxis”):
fig.layout[axis].title = “”
fig.update_layout(showlegend=False)
st.plotly_chart(fig, use_container_width=True)

event_type_of_year()

def event_type_of_month():
fig = px.bar(
df5,
height=700,
x=[“Month”, “Event_name”],
y=“Type_event”,
color=“Type_event”,
facet_col=“Month”,
text_auto=‘.2s’,
labels={“Type_event”: “Type Event”, “Event_name”: “Event Count”},
category_orders={
“Month”: [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”]
},
title=“Type of event by month”,
)
for axis in fig.layout:
if axis.startswith(“xaxis”) or axis.startswith(“yaxis”):
fig.layout[axis].title = “”
fig.update_layout(showlegend=False)
st.plotly_chart(fig, use_container_width=True)

event_type_of_month()

Hi @goodten ,

Welcome to the forum !

If I am not mistaken, the “Building” title is the xaxis title, and the facet_col is year (above of each plot)

So if you want to create just a “Building” title on center of graph, the first thing you need to hide all the xaxis title and then create custom xaxis title in the center of graph using text annotation .

Make sure bothxref and yref of annotation is set to paper, to enable put text outside the plot.

You can add this code below before fig.update_layout(showlegend=False).

....

fig.update_xaxes(title=None)

fig.add_annotation(x=0.5, y=-0.1,xref="paper",yref="paper",
            text="Building",
            showarrow=False,)

....

Hope this help.
Cheers.

1 Like

Awesome … Thank you.

Hi @goodten,

I’m glad if it work well.

Have a good day.

1 Like