🌟 Hit star button to save this repo in your profile
Plotly is a versatile Python library for creating interactive and visually appealing data visualizations. It's particularly well-suited for Exploratory Data Analysis (EDA) when you need interactive plots. Here are some common Plotly syntax and functions suitable for EDA:
-
Importing Plotly:
-
Import Plotly and its submodules:
import plotly.express as px import plotly.graph_objects as go
-
-
Basic Scatter Plot:
-
Create a simple scatter plot:
fig = px.scatter(data_frame, x="x_column", y="y_column")
-
-
Line Plot:
-
Generate a line plot to visualize trends over time or continuous data:
fig = go.Figure(data=go.Scatter(x=x_values, y=y_values, mode='lines'))
-
-
Bar Chart:
-
Create a bar chart for categorical data:
fig = px.bar(data_frame, x="category_column", y="value_column")
-
-
Histogram:
-
Generate a histogram for data distribution:
fig = px.histogram(data_frame, x="data_column")
-
-
Box Plot:
-
Create a box plot to visualize data quartiles and outliers:
fig = px.box(data_frame, x="category_column", y="value_column")
-
-
Heatmap:
-
Display a heatmap for correlation matrices or other 2D data:
fig = go.Figure(data=go.Heatmap(z=correlation_matrix, x=column_labels, y=column_labels))
-
-
3D Scatter Plot:
-
Generate interactive 3D scatter plots:
fig = px.scatter_3d(data_frame, x="x_column", y="y_column", z="z_column")
-
-
Sunburst Chart:
-
Create hierarchical sunburst charts for visualizing hierarchical data:
fig = px.sunburst(data_frame, path=["parent", "child"])
-
-
Customizing Plots:
-
Customize the appearance of plots with titles, labels, legends, and more:
fig.update_layout(title="Plot Title") fig.update_xaxes(title_text="X-axis label") fig.update_yaxes(title_text="Y-axis label")
-
-
Subplots:
-
Create subplots with multiple plots in a single figure:
from plotly.subplots import make_subplots fig = make_subplots(rows=2, cols=2, subplot_titles=("Subplot 1", "Subplot 2", "Subplot 3", "Subplot 4")) fig.add_trace(go.Scatter(x=x1, y=y1), row=1, col=1) fig.add_trace(go.Scatter(x=x2, y=y2), row=1, col=2) fig.add_trace(go.Scatter(x=x3, y=y3), row=2, col=1) fig.add_trace(go.Scatter(x=x4, y=y4), row=2, col=2)
-
-
Interactive Features:
- Plotly provides interactivity by default, including zoom, pan, and hover tooltips.
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
# URL of the Titanic dataset
url = 'https://raw.githubusercontent.com/drshahizan/dataset/main/titanic/train.csv'
# Load the dataset
titanic_data = pd.read_csv(url)
# Display the first 5 rows
print(titanic_data.head())
# Plotting the distribution of passengers' ages
fig_age = px.histogram(titanic_data, x='Age', nbins=30, title='Distribution of Passengers\' Ages')
fig_age.update_layout(xaxis_title='Age', yaxis_title='Number of Passengers')
fig_age.show()
# Plotting the survival rate by gender
fig_gender = px.bar(titanic_data, x='Sex', y='Survived', color='Sex', title='Survival Rate by Gender',
labels={'Survived': 'Survival Rate'})
fig_gender.update_layout(xaxis_title='Gender', yaxis_title='Survival Rate')
fig_gender.show()
# Plotting the survival rate by class
fig_class = px.bar(titanic_data, x='Pclass', y='Survived', color='Pclass', title='Survival Rate by Class',
labels={'Survived': 'Survival Rate', 'Pclass': 'Class'})
fig_class.update_layout(xaxis_title='Class', yaxis_title='Survival Rate')
fig_class.show()
Plotly is an excellent choice for EDA when you need to create interactive and visually engaging plots. You can use these Plotly syntax and functions to explore and present your data in a highly interactive way.
Please create an Issue for any improvements, suggestions or errors in the content.
You can also contact me using Linkedin for any other queries or feedback.