import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
Pertemuan 6 : Interactive Data Visualization (plotly)
Interactive Data Visualization using Plotly
Kembali ke EDA
plotly.express
Jika belum terinstall pada jupyter notebook anda, jalankan pip install plotly
pada terminal.
Basic Charts
Line Chart
# using the iris dataset
= px.data.iris()
df
# plotting the line chart
= px.line(df, y="sepal_width")
fig
# showing the plot
fig.show()
Apa bedanya dengan line plot biasa?
import matplotlib.pyplot as plt
import seaborn as sns
# plotting the line chart
="sepal_width", x=df.index)
sns.lineplot(df, y
# showing the plot
plt.show()
Grouping
# plotting the line chart
= px.line(df, y="sepal_width", line_group='species')
fig
# showing the plot
fig.show()
# plotting the line chart
= px.line(df, y="sepal_width", line_dash='species',
fig ='species')
color
# showing the plot
fig.show()
Bar Chart
# Loading the data
= px.data.tips()
df
# Creating the bar chart
= px.bar(df, x='day', y="total_bill")
fig
fig.show()
Grouping
# Creating the bar chart
= px.bar(df, x='day', y="total_bill", color='sex',
fig ='time', facet_col='sex')
facet_row
fig.show()
Scatter Plot
# plotting the scatter chart
= px.scatter(df, x='total_bill', y="tip")
fig
# showing the plot
fig.show()
Grouping
# plotting the scatter chart
= px.scatter(df, x='total_bill', y="tip", color='time',
fig ='sex', size='size', facet_row='day',
symbol='time')
facet_col
# showing the plot
fig.show()
Histogram
# plotting the histogram
= px.histogram(df, x="total_bill")
fig
# showing the plot
fig.show()
Grouping
# plotting the histogram
= px.histogram(df, x="total_bill", color='sex',
fig =50, histnorm='percent',
nbins='overlay')
barmode
# showing the plot
fig.show()
Pie Chart
# plotting the pie chart
= px.pie(df, values="total_bill", names="day")
fig
# showing the plot
fig.show()
Donut Chart
# plotting the donut chart
= px.pie(df, values="total_bill", names="day",
fig =px.colors.sequential.RdBu,
color_discrete_sequence=0.7, hole=0.5)
opacity
# showing the plot
fig.show()
Box Plot
# plotting the boxplot
= px.box(df, x="day", y="tip")
fig
# showing the plot
fig.show()
Grouping
# plotting the boxplot
= px.box(df, x="day", y="tip", color='sex',
fig ='time', boxmode='group',
facet_row=True)
notched
# showing the plot
fig.show()
Violin Plot
# plotting the violin plot
= px.violin(df, x="day", y="tip")
fig
# showing the plot
fig.show()
Grouping
# plotting the violin plot
= px.violin(df, x="day", y="tip", color='sex',
fig ='time', box=True)
facet_row
# showing the plot
fig.show()
3D Plot
# plotting the figure
= px.scatter_3d(df, x="total_bill", y="sex", z="tip")
fig
fig.show()
Grouping
# plotting the figure
= px.scatter_3d(df, x="total_bill", y="sex", z="tip", color='day',
fig ='total_bill', symbol='time')
size
fig.show()
Adding interaction
Untuk memunculkan tombol-tombol interaktif pada plot, kita menggunakan submodule dari library plotly yaitu plotly.graph_objects
Sliders and Selectors
= df['total_bill']
x = df['tip']
y
= go.Figure(data=[go.Scatter(
plot =x,
x=y,
y='markers',)
mode
])
plot.update_layout(=dict(
xaxis=dict(
rangeselector=list([
buttonsdict(count=1,
="day",
step="backward"),
stepmode
])
),=dict(
rangeslider=True
visible
),
)
)
plot.show()
Lebih lanjut silakan buka dokumentasi library plotly pada link berikut : Dokumentasi Plotly
Untuk memperbaiki dan meningkatkan kualitas praktikum kedepannya, silakan berikan feedback anda melalui link berikut : Feedback Praktikum EDA 2023/2024