Let's now use Matplotlib to create a plot in a Streamlit app.
Here's an explanation of the code in a step-by-step manner:
- Import the
matplotlib.pyplot
asplt
(so that we can later refer tomatplotlib.pyplot
literally asplt
instead of having to type the full version ofmatplotlib.pyplot
. New line of code: Likewise, we import the Streamlit library usingimport streamlit as st
. - Write out the title of the app via
st.title()
. - Create
years
andpopulation
variables that will be used for subsequent steps in creating the plot. - Define
fig
andax
viafig, ax = plt.subplots()
, which will be used inax.plot()
andax.scatter
as well asst.pyplot(fig)
. - Create the plot via
ax.plot()
and specifyingyears
andpopulation
as input arguments. This will create a line plot. - The line is changed to red dashed line via the third argument
r--
. - Data points are added via the
ax.scatter
function. - A second green line with green translucent data points are also added here.
- Add labels to the plot as well as the X and Y axes.
- Finally, we're going to display the plot via
st.pyplot(fig)
(recall thefig
variable that we defined earlier).
import streamlit as st
import matplotlib.pyplot as plt
st.title('📈 Line plot with Matplotlib')
# Data
years = [1950, 1960, 1970, 1980, 1990, 2000, 2010, 2020]
population = [2.5, 3.0, 3.7, 4.5, 5.3, 6.1, 6.9, 7.7]
population2 = [7.7, 8.1, 8.5, 9.0, 9.5, 10.0, 10.5, 11.0]
# Create plot
# Here we define fig and ax and subsequently replace plt by ax for the plots
fig, ax = plt.subplots()
ax.plot(years, population, 'r--')
ax.scatter(years, population, c='r', alpha=0.6)
ax.plot(years, population2, 'g')
ax.scatter(years, population2, c='g', alpha=0.6)
# Add labels
plt.title('Population Growth')
plt.xlabel('Year')
plt.ylabel('Population (millions)')
# Show plot
# Instead of plt.show() we're using st.pyplot
st.pyplot(fig)
The above code gives us the following Streamlit app (GitHub repo | Demo app)
Now that you have this Matplotlib app completed, can you build upon this Streamlit app by adding input widgets for allowing users to choose a line color or data point color of their choice. After selection, the app will update and generate the corresponding plot with the specified colors.
📣 Learn in Public:
Share your solution (the updated Streamlit app) on social media (Twitter and/or LinkedIn) and tag us (
@streamlit
).