How to display line, area and bar chart in streamlit

In this recipe, we will learn how to display line, area, and bar charts in Streamlit. We will also take a look at Streamlit application consisting of these charts.

Recipe Objective: How to display line, area and bar chart using Streamlit?

Streamlit offers support to many different charting libraries. It also provides some charts like line chart and area chart that are native to Streamlit. Let us see how to display these charts.

st.line_chart - to display a simple line chart

  Syntax: st.line_chart(data=None, width=0, height=0, use_container_width=True)
  Parameters:
   data -> The data to be plotted
   width -> The width of the chart in pixels
   height -> The height of the chart in pixels
   use_container_width -> If this is set as true, it sets the chart width to the column width. It takes precedence over the width parameter

st.area_chart - to display a simple area chart

  Syntax:st.area_chart(data=None, width=0, height=0, use_container_width=True)
  Parameters:
   data -> The data to be plotted
   width -> The width of the chart in pixels
   height -> The height of the chart in pixels
   use_container_width -> If this is set as true, it sets the chart width to the column width. It takes precedence over the width parameter

st.bar_chart - to display a simple bar chart

  Syntax:st.bar_chart(data=None, width=0, height=0, use_container_width=True)
  Parameters:
   data -> The data to be plotted
   width -> The width of the chart in pixels
   height -> The height of the chart in pixels
   use_container_width -> If this is set as true, it sets the chart width to the column width. It takes precedence over the width parameter

Code:

#importing required libraries
import streamlit as st
import pandas as pd
import numpy as np

#creating a sample dataframe
df = pd.DataFrame(np.random.randn(30,2),columns=['A','B'])

#line chart
st.line_chart(df)

#area chart
st.area_chart(df)

#bar chart
st.bar_chart(df)

To run the app, either create an appname.py file with the above code using any text editor or if you are using 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

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

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.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.