Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I give a border (outline) to a line in matplotlib plot function?

Tags:

I try:

points = [...] axe.plot([i[0] for i in points], [i[1] for i in points], linestyle='-', linewidth=10,  color='black', markeredgewidth=2, markeredgecolor='green') 

But I just get a black contour. How can I achieve something like on the following picture? black line with green border

like image 608
Alex Avatar asked Oct 04 '12 14:10

Alex


People also ask

How do I assign a line color in Matplotlib?

The usual way to set the line color in matplotlib is to specify it in the plot command. This can either be done by a string after the data, e.g. "r-" for a red line, or by explicitely stating the color argument.

Which Matplotlib function used to draw a line chart is line?

A line chart can be created using the Matplotlib plot() function.


2 Answers

If you plot a line twice it won't show up in the legend. It's indeed better to use patheffects. Here are two simple examples:

import matplotlib.pyplot as plt import numpy as np import matplotlib.patheffects as pe  # setup data x = np.arange(0.0, 1.0, 0.01) y = np.sin(2*2*np.pi*t)  # create line plot including an outline (stroke) using path_effects plt.plot(x, y, color='k', lw=2, path_effects=[pe.Stroke(linewidth=5, foreground='g'), pe.Normal()]) # custom plot settings plt.grid(True) plt.ylim((-2, 2)) plt.legend(['sine']) plt.show() 

enter image description here

Or if you want to add a line shadow

# create line plot including an simple line shadow using path_effects plt.plot(x, y, color='k', lw=2, path_effects=[pe.SimpleLineShadow(shadow_color='g'), pe.Normal()]) # custom plot settings plt.grid(True) plt.ylim((-2, 2)) plt.legend(['sine']) plt.show() 

enter image description here

like image 114
Mattijn Avatar answered Sep 28 '22 08:09

Mattijn


Just plot the line twice with different thicknesses:

axe.plot([i[0] for i in points], [i[1] for i in points], linestyle='-', linewidth=10,  color='green') axe.plot([i[0] for i in points], [i[1] for i in points], linestyle='-', linewidth=5,  color='black') 
like image 40
aganders3 Avatar answered Sep 28 '22 09:09

aganders3