Plotly Dash : Scatter Plot from Iris Dataset
Plotly Dash : Scatter Plot using Iris Dataset
Scatter Plots
Scatter plots are a valuable tool to visualize relationship between two numerical variables. For instance, they can be used to examine the relationship between weight and height. By analyzing the pattern of dots on the graph, one can identify the relationship as positive, negative, or nonexistent.
Scatter Plot using Plotly Dash
Install Required Libraries
First of all install required libraries using "pip". Open your terminal or command prompt. Then type the following command and press Enter:
pip install dash plotly scikit-learn
This will download and install Dash, Plotly, Scikit-learn along with dependencies. Once the installation is complete, you can import the Iris dataset from Scikit-learn in your Python code and use it to create your scatter plot.
Scikit-Learn
As you noted, I am using Scikit-Learn for Iris dataset. Scikit-learn is a powerful Python library designed for machine learning tasks. It provides a simple yet effective interface for building various machine learning models, ranging from simple linear regression to complex neural networks. Since Scikit-learn is frequently used in machine learning tasks, so I decided to use Iris dataset in this library.
Pandas
Pandas
is a versatile and powerful Python tool for data analysis, exploration and visualization.
This library is specifically designed to handle and analyze structured data. It
provides high-performance data structures and data analysis tools, making it an
essential tool for data scientists, researchers, and analysts.
Pandas plays a crucial role in machine learning, including data cleaning and preparation, feature engineering, and data exploration.
Pandas
also offers flexibility in customizing visualizations to suit specific needs
and preferences.
If you have not already installed pandas library, install it with the following command:
pip install pandas
Create the Dash App
import dash, sklearn
from dash import dcc
from dash import html
import plotly.express as px
import pandas as pd
from sklearn.datasets import load_iris
# Load the Iris dataset
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df['species'] = iris.target
# Map numeric labels to species names
species_names = {0: 'setosa', 1: 'versicolor', 2: 'virginica'}
df['species'] = df['species'].map(species_names)
# Initialize the Dash app
app = dash.Dash(__name__)
# Create a scatter plot
fig = px.scatter(df, x='sepal length (cm)', y='petal length (cm)',
color='species', labels={'species': 'Species'}, title='Scatter Plot of Iris Dataset')
# Define the layout of the app
app.layout = html.Div([
html.H1("Iris Dataset Scatter Plot"),
dcc.Graph(figure=fig)
])
# Create the Dash app layout
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Graph(figure=fig)
])
# Run the app
if __name__ == '__main__':
app.run_server(debug=True)
Run the App
Save the app
Run the app using command:
python app.py
See also:
Comments
Post a Comment