How to display a Matplotlib.pyplot figure in Streamlit

In this recipe, we will learn how to display a Matplotlib.pyplot figure in Streamlit. We will also take a look at a Streamlit application consisting of a pyplot figure.

Recipe Objective: How to display a Matplotlib.pyplot figure in Streamlit?

Matplotlib.pyplot is a python library used for plotting 2D figures. "st.pyplot" is used to display a Matplotlib.pyplot figure in Streamlit.

 Syntax: st.pyplot(fig=None, clear_figure=None, **kwargs)
 Parameter:
   fig -> The figure to plot
   clear_figure -> Can be True or False. If set True, the figure will be cleared after being rendered and if set False the will not be cleared after being rendered
   **kwargs -> Arguments that can be passed to Matplotlib's savefig function.

Code:

#importing required libraries
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np

#creating a sample array
a = np.random.normal(1, 1, size=50)

#specifying the figure to plot
fig, x = plt.subplots()
x.hist(a, bins=10)

#plotting the figure
st.pyplot(fig)

To run the app, either create an appname.py file with the above code using any text editor or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the "streamlit run appname.py" command. Once you run the command, the app will automatically open in your default browser.

What Users are saying..

profile image

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

GCP MLOps Project to Deploy ARIMA Model using uWSGI Flask
Build an end-to-end MLOps Pipeline to deploy a Time Series ARIMA Model on GCP using uWSGI and Flask

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch