Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Plotly – How to simultaneously apply color/shape/size in a Scatter Plot?
Plotly is an open-source Python library for creating interactive web-based visualizations. In this tutorial, we'll explore how to simultaneously apply color, shape, and size properties to scatter plots for enhanced data visualization.
Understanding Scatter Plot Properties
Scatter plots can be customized with multiple visual properties:
Color ? Maps data values to different colors using color scales
Size ? Varies marker sizes based on data values
Shape ? Uses different marker symbols to represent categories
Basic Scatter Plot with Color and Size
Let's create a scatter plot where color and size represent different data dimensions:
import plotly.express as px
import pandas as pd
# Sample data
data = {
'x': [1, 2, 3, 4, 5, 6, 7, 8],
'y': [10, 11, 12, 13, 14, 15, 16, 17],
'size_values': [20, 30, 40, 50, 25, 35, 45, 55],
'color_values': [1, 2, 1, 3, 2, 3, 1, 2],
'category': ['A', 'B', 'A', 'C', 'B', 'C', 'A', 'B']
}
df = pd.DataFrame(data)
# Create scatter plot with color and size
fig = px.scatter(df,
x='x',
y='y',
size='size_values',
color='color_values',
title='Scatter Plot with Color and Size')
fig.show()
Adding Shape Variation
To include shape variation, we'll use the symbol parameter to map different shapes to categories:
import plotly.express as px
import pandas as pd
# Enhanced data with categories
data = {
'x': [1, 2, 3, 4, 5, 6, 7, 8],
'y': [10, 11, 12, 13, 14, 15, 16, 17],
'size_values': [20, 30, 40, 50, 25, 35, 45, 55],
'color_values': [1, 2, 1, 3, 2, 3, 1, 2],
'shape_category': ['Circle', 'Square', 'Triangle', 'Circle', 'Square', 'Triangle', 'Circle', 'Square']
}
df = pd.DataFrame(data)
# Create scatter plot with color, size, and shape
fig = px.scatter(df,
x='x',
y='y',
size='size_values',
color='color_values',
symbol='shape_category',
title='Scatter Plot with Color, Size, and Shape',
size_max=60)
fig.update_layout(width=800, height=500)
fig.show()
Using Graph Objects for Advanced Control
For more precise control over scatter plot properties, use plotly.graph_objects:
import plotly.graph_objects as go
import numpy as np
# Generate sample data
np.random.seed(42)
n_points = 50
x_data = np.random.randn(n_points)
y_data = np.random.randn(n_points)
colors = np.random.randint(1, 5, n_points)
sizes = np.random.randint(10, 50, n_points)
fig = go.Figure()
# Add scatter trace with multiple properties
fig.add_trace(go.Scatter(
x=x_data,
y=y_data,
mode='markers',
marker=dict(
size=sizes,
color=colors,
colorscale='Viridis',
showscale=True,
colorbar=dict(title='Color Scale'),
line=dict(width=2, color='black'),
opacity=0.8
),
text=[f'Point {i+1}' for i in range(n_points)],
name='Data Points'
))
fig.update_layout(
title='Advanced Scatter Plot with Multiple Properties',
xaxis_title='X Values',
yaxis_title='Y Values',
width=800,
height=600
)
fig.show()
Real-World Example with Iris Dataset
Here's a practical example using the famous Iris dataset:
import plotly.express as px
# Load built-in iris dataset
iris = px.data.iris()
# Create comprehensive scatter plot
fig = px.scatter(iris,
x='sepal_width',
y='sepal_length',
size='petal_length',
color='petal_width',
symbol='species',
title='Iris Dataset: Multi-dimensional Visualization',
labels={
'sepal_width': 'Sepal Width (cm)',
'sepal_length': 'Sepal Length (cm)',
'petal_length': 'Petal Length',
'petal_width': 'Petal Width'
},
size_max=20)
fig.update_layout(width=800, height=600)
fig.show()
Customization Tips
Key parameters for customizing scatter plots:
size_max ? Controls maximum marker size
opacity ? Adjusts marker transparency
colorscale ? Sets color scheme (Viridis, Plasma, etc.)
hover_data ? Adds extra information on hover
Conclusion
Plotly allows simultaneous application of color, shape, and size properties to create rich, multi-dimensional scatter plots. Use plotly.express for quick visualizations or plotly.graph_objects for advanced customization.
