๐Ÿ“Š Data Engineering

Plotly

Plotly

An interactive visualization library. Builds dashboards.

Detailed Explanation

Plotly is an interactive visualization library developed by the company of the same name, founded in 2012. It can be used in Python (plotly.py), JavaScript (plotly.js), and R, and generates web-based interactive charts. It supports zoom, pan, hover, and click events, making it well suited for exploratory data analysis (EDA) and building dashboards.

Plotly Express is a high-level API that creates complex visualizations with a single line of code. You can quickly build charts with intuitive functions such as px.scatter(), px.line(), and px.bar(), and use the graph_objects module when detailed customization is needed.

It provides major chart types including scatter, line, bar, histogram, box plot, violin plot, heatmap, 3D scatter, and choropleth (map). The facet feature splits data into groups to create multiple subplots, and animation_frame expresses changes over time as animations.

Combined with the Dash framework, it can be used to build full-fledged data apps and dashboards. In Jupyter Notebook, it renders inline, and it can be exported as an HTML file for sharing on the web. Cloud hosting is also supported through Chart Studio.

Code Example

import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np

# ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ
np.random.seed(42)
df = pd.DataFrame({
    'date': pd.date_range('2024-01-01', periods=100),
    'sales': np.cumsum(np.random.randn(100) * 10 + 50),
    'category': np.random.choice(['A', 'B', 'C'], 100),
    'region': np.random.choice(['North', 'South', 'East', 'West'], 100),
    'profit': np.random.randn(100) * 20 + 100
})

# Plotly Express - ๊ฐ„๋‹จํ•œ ์‹œ๊ฐํ™”
fig1 = px.scatter(
    df, x='sales', y='profit',
    color='category',
    size=abs(df['profit']),
    hover_data=['date', 'region'],
    title='Sales vs Profit by Category'
)
fig1.show()

# ๋ผ์ธ ์ฐจํŠธ with ์• ๋‹ˆ๋ฉ”์ด์…˜
fig2 = px.line(
    df, x='date', y='sales',
    color='category',
    title='Sales Trend by Category'
)
fig2.update_layout(hovermode='x unified')
fig2.show()

# Facet ๊ทธ๋ฆฌ๋“œ (์—ฌ๋Ÿฌ ์„œ๋ธŒํ”Œ๋กฏ)
fig3 = px.histogram(
    df, x='profit',
    facet_col='category',
    facet_row='region',
    color='category',
    title='Profit Distribution'
)
fig3.show()

# Graph Objects - ์„ธ๋ฐ€ํ•œ ์ œ์–ด
fig4 = go.Figure()

fig4.add_trace(go.Scatter(
    x=df['date'], y=df['sales'],
    mode='lines+markers',
    name='Sales',
    line=dict(color='royalblue', width=2),
    marker=dict(size=6)
))

fig4.add_trace(go.Bar(
    x=df['date'], y=df['profit'],
    name='Profit',
    marker_color='lightgreen',
    opacity=0.6,
    yaxis='y2'
))

fig4.update_layout(
    title='Sales and Profit Over Time',
    xaxis_title='Date',
    yaxis_title='Sales',
    yaxis2=dict(title='Profit', overlaying='y', side='right'),
    legend=dict(x=0.01, y=0.99),
    hovermode='x unified'
)
fig4.show()

# ์„œ๋ธŒํ”Œ๋กฏ
fig5 = make_subplots(
    rows=2, cols=2,
    subplot_titles=['Scatter', 'Histogram', 'Box', 'Heatmap']
)

fig5.add_trace(go.Scatter(x=df['sales'], y=df['profit'], mode='markers'), row=1, col=1)
fig5.add_trace(go.Histogram(x=df['profit']), row=1, col=2)
fig5.add_trace(go.Box(y=df['sales'], x=df['category']), row=2, col=1)

# ํžˆํŠธ๋งต
corr_data = df[['sales', 'profit']].corr()
fig5.add_trace(go.Heatmap(z=corr_data.values, x=corr_data.columns, y=corr_data.index), row=2, col=2)

fig5.update_layout(height=800, showlegend=False)
fig5.show()

# HTML๋กœ ์ €์žฅ
fig4.write_html('interactive_chart.html')

# ์ด๋ฏธ์ง€๋กœ ์ €์žฅ (kaleido ํ•„์š”)
# fig4.write_image('chart.png', scale=2)

How It's Said in Practice

Senior: "We build executive dashboards with Plotly. It has zoom and filtering features, so they can explore the data themselves."

Junior: "How is it different from Matplotlib?"

Senior: "Matplotlib produces static images, while Plotly produces interactive web charts. In Jupyter, you can hover the mouse to see values, and you can zoom in and out too."

Interviewer: "Please tell us about your data visualization experience."

Candidate: "I developed a real-time monitoring dashboard with Plotly and Dash. I visualized an anomaly detection chart with px.scatter and a time-series trend with px.line, and linked them to dropdown filters using callback functions. I also implemented a feature that exports charts as HTML and automatically shares them via Slack."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "If you set hovermode='x unified' in update_layout, you can see all the data points on the same x-axis at once."

Developer: "Oh, that would be handy when comparing multiple lines in a time-series chart. I'll apply it right away."

Cautions

Learn More