Skip to content

Instantly share code, notes, and snippets.

@ShangjinTang
Created April 28, 2024 03:56
Show Gist options
  • Save ShangjinTang/984b4c0e32e7ed84d5b2df4732f397f3 to your computer and use it in GitHub Desktop.
Save ShangjinTang/984b4c0e32e7ed84d5b2df4732f397f3 to your computer and use it in GitHub Desktop.
plotly dash
#!/usr/bin/env python3
import pandas as pd
import plotly.express as px
from sklearn.datasets import load_iris
import dash
from dash import dcc, html
# Load the Iris dataset
iris = load_iris()
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df["species"] = iris.target_names[iris.target]
# Create the Dash app
app = dash.Dash(__name__)
# Define the layout of the app
app.layout = html.Div(
[
html.H1("Iris Dataset Visualization"),
html.Label("Select x-axis feature"),
dcc.Dropdown(
id="x-axis-dropdown",
options=[
{"label": feature, "value": feature} for feature in df.columns[:-1]
],
value="sepal length (cm)",
),
html.Label("Select y-axis feature"),
dcc.Dropdown(
id="y-axis-dropdown",
options=[
{"label": feature, "value": feature} for feature in df.columns[:-1]
],
value="sepal width (cm)",
),
dcc.Graph(id="scatter-plot"),
]
)
# Define the callback function for the scatter plot
@app.callback(
dash.dependencies.Output("scatter-plot", "figure"),
dash.dependencies.Input("x-axis-dropdown", "value"),
dash.dependencies.Input("y-axis-dropdown", "value"),
)
def update_scatter_plot(x_axis, y_axis):
# Create the scatter plot using Plotly Express
fig = px.scatter(df, x=x_axis, y=y_axis, color="species")
return fig
# Run the app
if __name__ == "__main__":
app.run_server(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment