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
How to color a Matplotlib scatterplot using a continuous value?
To color a matplotlib scatterplot using continuous values, we can map a third variable to the color of each point. This creates a visual representation where color intensity or hue represents the magnitude of the continuous variable.
Basic Scatterplot with Continuous Coloring
Here's how to create a scatter plot where colors represent continuous values ?
import numpy as np import matplotlib.pyplot as plt # Set figure size and layout plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True # Generate random data points x, y, z = np.random.rand(3, 50) # Create figure and subplots f, ax = plt.subplots() # Create scatter plot with continuous coloring points = ax.scatter(x, y, c=z, s=50, cmap="plasma") # Add colorbar to show the color scale f.colorbar(points) plt.show()
Understanding the Parameters
The key parameters for continuous coloring are ?
c=z− Maps the z values to colorscmap="plasma"− Specifies the colormap to uses=50− Sets the size of scatter pointscolorbar()− Adds a legend showing the color scale
Using Different Colormaps
You can experiment with different colormaps for various visual effects ?
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data
np.random.seed(42)
x = np.random.randn(100)
y = np.random.randn(100)
colors = x * y # Continuous values based on x*y
fig, axes = plt.subplots(1, 3, figsize=(12, 4))
# Different colormaps
colormaps = ['viridis', 'coolwarm', 'plasma']
titles = ['Viridis', 'Coolwarm', 'Plasma']
for ax, cmap, title in zip(axes, colormaps, titles):
scatter = ax.scatter(x, y, c=colors, cmap=cmap, alpha=0.7)
ax.set_title(f'{title} Colormap')
plt.colorbar(scatter, ax=ax)
plt.tight_layout()
plt.show()
Customizing Color Ranges
Control the color mapping by setting specific value ranges ?
import numpy as np
import matplotlib.pyplot as plt
# Create sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
colors = np.cos(x)
plt.figure(figsize=(8, 5))
# Use vmin and vmax to control color range
scatter = plt.scatter(x, y, c=colors, cmap='RdYlBu',
vmin=-0.5, vmax=0.5, s=60)
plt.colorbar(scatter, label='Color Values')
plt.xlabel('X values')
plt.ylabel('Y values')
plt.title('Scatter Plot with Custom Color Range')
plt.show()
Popular Colormaps
| Colormap | Type | Best For |
|---|---|---|
| viridis | Sequential | General purpose, colorblind-friendly |
| plasma | Sequential | High contrast, vibrant colors |
| coolwarm | Diverging | Data with positive/negative values |
| RdYlBu | Diverging | Temperature or correlation data |
Conclusion
Use the c parameter in scatter() to map continuous values to colors. Choose appropriate colormaps based on your data type and add a colorbar for reference. This technique effectively visualizes three-dimensional relationships in 2D plots.
