A trend line and a total bar chart from the same table
Write code · Challenge · §11.3 Connecting charts and tables
The table cinema holds the weekly number of viewers in two screening halls (Hall 1 and Hall 2) of a cinema. The starter code picks halls and weeks in the sidebar and stores the chosen rows in view. Below it, draw two charts, both taking their values from view.
On the left, draw a Plotly Express line chart with the week (week) on the x-axis and the viewers (viewers) on the y-axis, one line per hall. On the right, draw a Matplotlib bar chart with one bar per hall for the total viewers in the chosen weeks, with the bars in the order Hall 1, Hall 2. Place the two charts in this order in the left and right columns of left, right = st.columns(2), using st.plotly_chart() and st.pyplot() to add each one to the app. Grading runs the app again with different widget values and viewer counts.
Starter code
import matplotlib.pyplot as plt
import pandas as pd
import plotly.express as px
import streamlit as st
cinema = pd.DataFrame({
"week": [1, 2, 3, 4, 5] * 2,
"screen": ["Hall 1"] * 5 + ["Hall 2"] * 5,
"viewers": [820, 760, 910, 680, 990, 540, 610, 580, 720, 650],
})
st.title("Cinema viewers")
halls = st.sidebar.multiselect("Hall", ["Hall 1", "Hall 2"], default=["Hall 1", "Hall 2"])
weeks = st.sidebar.slider("Week", 1, 5, (1, 5))
view = cinema[cinema["screen"].isin(halls) & cinema["week"].between(weeks[0], weeks[1])]
if view.empty:
st.warning("Pick at least one hall.")
st.stop()
# Write your code hereSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.