Is it possible to have something similar to edgecolor
and facecolor
for plt.plot()? I need to plot a curve with a different colored boundary around it for e.g. a segment of the line would look like ||| with the outer lines in a different color and the inner line in a different color. Can this be done using a single plot command rather than plotting three plots?
Asked
Active
Viewed 706 times
1

madvn
- 33
- 7
-
No it can't. A line is a line, which has no edge. You may define a polygon instead. Or you may draw 3 parallel lines.Depending on the use case this can range from just repeating the call to `plot` 3 times to something like shown in [this answer](https://stackoverflow.com/a/42190453/4124317). – ImportanceOfBeingErnest Jan 16 '18 at 15:44
-
@ImportanceOfBeingErnest a line _can_ have an edge if you define its `path_effects` using a `Stroke`. – tmdavison Jan 16 '18 at 15:54
1 Answers
2
You can do this using the matplotlib.patheffects
module. You can set the path_effect
of a line by using the path_effects
kwarg.
Specifically in this case, we can use the Stroke
class for the outline, and the Normal
class for the inner part of the line (this just uses the linewidth and color specified by plt.plot
). See the example below.
import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
import numpy as np
fig, ax = plt.subplots(1)
ax.plot(np.random.rand(5), linewidth=4, color='r', path_effects=[
path_effects.Stroke(linewidth=8, foreground='black'),
path_effects.Normal()
])
plt.show()

tmdavison
- 64,360
- 12
- 187
- 165