How to display charts using plotly library in Streamlit

In this recipe, we will learn how to display charts using the plotly library in Streamlit. We will also take a look at a Streamlit application consisting Plotly charts.

Recipe Objective: How to display charts using plotly library in Streamlit?

Plotly is a high level charting library. Streamlit offers support to plotly library and you can easily plot any charts using "st.plotly_chart".

 Syntax: st.plotly_chart(figure_or_data, use_container_width=False, sharing='streamlit', **kwargs)
 Parameters:
   figure_or_data -> Disctionary or list of plotly.graph_objs.Figure/Data
   use_container_width -> If this is set as true, it sets the chart width to the column width. It takes precedence over the figure's native width value.
   sharing -> Could be set to 'streamlit', 'private', 'secret' or 'public'. Using plotly's offline mode, use 'streamlit' to put the plot and all of its dependencies straight in the Streamlit app (default). To share the chart to Plotly chart studio, which requires an account, use any other sharing option.
   **kwargs -> Could be any argument accepted by Plotly's plot() function.

Code:

#importing require libraries
import plotly.figure_factory as ff
import numpy as np

#Creating sample histogram data
a1 = np.random.randn(200) - 2
a2 = np.random.randn(200)
a3 = np.random.randn(200) + 2
a4 = np.random.randn(200) + 4

#Grouping the data together
hist_data = [a1, a2, a3, a4]

group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']

#Creating distplot with custom bin_size
fig = ff.create_distplot(hist_data, group_labels, bin_size=[.1, .25, .5, .75])

#Plotting the chart
st.plotly_chart(fig, use_container_width=True)

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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Insurance Pricing Forecast Using XGBoost Regressor
In this project, we are going to talk about insurance forecast by using linear and xgboost regression techniques.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.