5 min read min read
Plotly Express Basics
Learn the quick way to create Plotly charts
Plotly Express Basics
What is Plotly Express?
Plotly Express (px) is the easy way to use Plotly. One line = one chart.
Import
code.pyPython
import plotly.express as px
import pandas as pdLine Chart
code.pyPython
df = pd.DataFrame({
'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
'Sales': [100, 120, 115, 140, 160]
})
fig = px.line(df, x='Month', y='Sales', title='Monthly Sales')
fig.show()Bar Chart
code.pyPython
fig = px.bar(df, x='Month', y='Sales', title='Sales by Month')
fig.show()Scatter Plot
code.pyPython
df = pd.DataFrame({
'Age': [25, 30, 35, 40, 45],
'Salary': [40000, 50000, 60000, 70000, 80000]
})
fig = px.scatter(df, x='Age', y='Salary', title='Age vs Salary')
fig.show()Color by Category
code.pyPython
df = pd.DataFrame({
'Age': [25, 30, 35, 40, 45, 50],
'Salary': [40000, 50000, 60000, 70000, 80000, 90000],
'Department': ['Sales', 'IT', 'Sales', 'IT', 'HR', 'HR']
})
fig = px.scatter(df, x='Age', y='Salary', color='Department')
fig.show()Each department gets a different color!
Size by Value
code.pyPython
df['Experience'] = [2, 5, 8, 12, 15, 20]
fig = px.scatter(df, x='Age', y='Salary',
color='Department', size='Experience')
fig.show()Histogram
code.pyPython
import numpy as np
data = np.random.normal(50, 10, 500)
fig = px.histogram(x=data, nbins=30, title='Distribution')
fig.show()Box Plot
code.pyPython
df = pd.DataFrame({
'Department': ['Sales']*20 + ['IT']*20,
'Salary': list(range(40000, 60000, 1000)) + list(range(60000, 80000, 1000))
})
fig = px.box(df, x='Department', y='Salary')
fig.show()Pie Chart
code.pyPython
df = pd.DataFrame({
'Category': ['A', 'B', 'C', 'D'],
'Value': [30, 25, 25, 20]
})
fig = px.pie(df, values='Value', names='Category', title='Distribution')
fig.show()Common Parameters
| Parameter | What it does |
|---|---|
| x | X-axis column |
| y | Y-axis column |
| color | Color by column |
| size | Size by column |
| title | Chart title |
| labels | Rename axes |
Rename Axis Labels
code.pyPython
fig = px.scatter(df, x='Age', y='Salary',
labels={'Age': 'Employee Age', 'Salary': 'Annual Salary'})
fig.show()Key Points
- px.line() for trends
- px.bar() for comparisons
- px.scatter() for relationships
- px.histogram() for distributions
- px.box() for spread
- px.pie() for proportions
- Use color and size for extra info
What's Next?
Learn to create interactive scatter plots with more features.