ValueError: fmt is not a valid format string
7 Dec 2025
2 min read
ValueError: fmt is not a valid format string
This error arises when you pass an unrecognized format string to a Matplotlib plotting function like plt.plot(). The format string is a shorthand way to specify color, marker, and line style.
$ python -c "import matplotlib.pyplot as plt; plt.plot([1, 2, 3], [1, 4, 9], 'invalid-style')"
Traceback (most recent call last):
...
ValueError: 'invalid-style' is not a valid format string
Why this happens
Matplotlib has a specific set of characters that it recognizes for format strings. If you provide a string that contains characters or combinations not in this set, it cannot parse the style information and raises a ValueError.
Common valid format characters include:
- Colors: ‘b’ (blue), ‘g’ (green), ‘r’ (red), ‘c’ (cyan), ‘m’ (magenta), ‘y’ (yellow), ‘k’ (black), ‘w’ (white)
- Markers: ’.’ (point), ‘o’ (circle), ‘x’ (x), ’+’ (plus), ’*’ (star), ‘s’ (square), ‘d’ (diamond)
- Line Styles: ’-’ (solid), ’—’ (dashed), ’:’ (dotted), ’-.’ (dash-dot)
Fix
To fix this, you must use a valid format string by combining the allowed characters for color, marker, and line style, or use the explicit keyword arguments (color, marker, linestyle).
Wrong code
The format string 'rdashed' is invalid because “dashed” is not a recognized style shorthand.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 5, 3, 6]
# 'rdashed' is not a valid format string
plt.plot(x, y, 'rdashed')
plt.title('Invalid Plot Style')
plt.show()
Fixed code
The corrected code uses a valid format string 'r--' for a red dashed line. Alternatively, you can use keyword arguments for better readability.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [2, 5, 3, 6]
# Corrected using a valid format string
plt.plot(x, y, 'r--') # 'r' for red, '--' for dashed
plt.title('Corrected Plot Style')
plt.show()
# Alternatively, using keyword arguments
plt.plot(x, y, color='red', linestyle='dashed')
plt.title('Corrected Plot Style with Keywords')
plt.show()