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 get the default blue colour of matplotlib.pyplot.scatter?
The default color of a matplotlib scatter plot is blue. You can explicitly use this default blue color by specifying its hex code #1f77b4 or by using the color name. Here's how to work with matplotlib's default blue color.
Getting the Default Blue Color
The default blue color in matplotlib has the hex code #1f77b4. You can use this directly or access it programmatically ?
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
# Method 1: Using hex code directly
fig, ax = plt.subplots(figsize=(8, 4))
# Default scatter (automatic blue)
ax.scatter(-1, 1, s=100)
ax.annotate("default color", xy=(-0.8, 1))
# Explicit hex code
ax.scatter(1, 1, c='#1f77b4', s=100)
ax.annotate("using hex code", xy=(1.2, 1))
ax.set_xlim(-2, 3)
ax.set_ylim(0, 2)
ax.set_title("Default Blue Color in Matplotlib Scatter Plot")
plt.tight_layout()
plt.show()
Accessing Default Colors Programmatically
You can also get the default color from matplotlib's color cycle ?
import matplotlib.pyplot as plt
# Get the default color cycle
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
# First color is the default blue
default_blue = colors[0]
print(f"Default blue color: {default_blue}")
# Use it in scatter plot
fig, ax = plt.subplots(figsize=(6, 4))
ax.scatter([1, 2, 3], [1, 2, 3], c=default_blue, s=100)
ax.set_title(f"Using default blue: {default_blue}")
plt.show()
Default blue color: #1f77b4
Different Ways to Specify the Default Blue
Here are multiple ways to use matplotlib's default blue color ?
import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
# Method 1: Let matplotlib use default
axes[0, 0].scatter([1, 2, 3], [1, 2, 3], s=100)
axes[0, 0].set_title("Default (automatic)")
# Method 2: Hex code
axes[0, 1].scatter([1, 2, 3], [1, 2, 3], c='#1f77b4', s=100)
axes[0, 1].set_title("Hex code: #1f77b4")
# Method 3: RGB tuple
axes[1, 0].scatter([1, 2, 3], [1, 2, 3], c=(0.12, 0.47, 0.71), s=100)
axes[1, 0].set_title("RGB tuple")
# Method 4: From color cycle
default_color = plt.rcParams['axes.prop_cycle'].by_key()['color'][0]
axes[1, 1].scatter([1, 2, 3], [1, 2, 3], c=default_color, s=100)
axes[1, 1].set_title("From color cycle")
plt.tight_layout()
plt.show()
Comparison
| Method | Code | Best For |
|---|---|---|
| Automatic | scatter(x, y) |
Simple plots |
| Hex Code | c='#1f77b4' |
Explicit control |
| Color Cycle | plt.rcParams['axes.prop_cycle'] |
Dynamic access |
Conclusion
The default matplotlib blue color is #1f77b4. You can use it explicitly with the hex code or access it programmatically from the color cycle. For simple plots, letting matplotlib use the default automatically is the easiest approach.
