Open In App

Matplotlib.pyplot.setp() function in Python

Last Updated : 10 Jul, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

matplotlib.pyplot.setp() function sets properties on an artist or a list of artists such as lines, texts, or patches in Matplotlib. It allows you to modify multiple properties at once, such as color, linewidth, linestyle, font size and more, after the plot elements have been created.

Example:

Python
import matplotlib.pyplot as plt

line, = plt.plot([1, 2, 3], [4, 5, 6])
plt.setp(line, color='red', linewidth=2, linestyle='--')
plt.show()

Output

Output
Red dashed line

Explanation: plt.setp() changes the line color to red, width to 2 and style to dashed, updating the plot to show a thicker red dashed line.

Syntax

matplotlib.pyplot.setp(obj, *args, **kwargs)

Parameters:

  • obj: A single Matplotlib artist or a list of artists to modify.
  • *args: Property-value pairs provided in sequence (e.g., 'color', 'red').
  • **kwargs: Property-value pairs provided as keyword arguments (e.g., color='red').

Returns: A list of property values that were set.

Examples

Example 1: Setting properties using keyword arguments

Python
import matplotlib.pyplot as plt

lines = plt.plot([1, 2, 3], [3, 6, 9])
plt.setp(lines, color='green', linewidth=3)
plt.show()

Output

Output
Thick green line

Explanation: plt.setp() changes the line color to green and width to 3, updating the plot to show a thicker green solid line.

Example 2: Setting properties using property-value pairs as args

Python
import matplotlib.pyplot as plt

lines = plt.plot([1, 2, 3], [1, 4, 9])
plt.setp(lines, 'linestyle', ':', 'marker', 'o', 'markersize', 8)
plt.show()

Output

Output
Dotted line with circles

Explanation: plt.setp() sets a dotted line (':'), adds circle markers ('o') and increases marker size to 8, making the plot more visually distinct.

Example 3: Changing text properties

Python
import matplotlib.pyplot as plt

text = plt.text(0.5, 0.5, 'Sample Text')
plt.setp(text, color='blue', fontsize=14, fontweight='bold')
plt.show()

Output

Output
Bold blue text

Explanation: plt.setp() sets the text color to blue, size to 14 and weight to bold, displaying "Sample Text" in bold blue at (0.5, 0.5).

Related article: Matplotlib


Practice Tags :

Similar Reads