Skip to content Skip to sidebar Skip to footer

Border On Errorbars In Matplotlib/python

I am looking for a way to set a black border on the errorbars in my plot, The following code: ax.errorbar(x, y, yerr, fmt='o', label='label',color='#8da0cb',capthick=2, elinewidth=

Solution 1:

Not a great solution, but you could get close by plotting the errorbars again behind your original ones, with a wider line and cap thinkness, and setting the colour of those ones to black. We can make use of the zorder kwarg to put them behind the others.

Heres a MWE:

import matplotlib.pyplot as plt
import numpy as np

# Fake data
x=np.arange(0,5,1)
y=np.ones(x.shape)
yerr = np.ones(x.shape)/4.# Create figure
fig,ax = plt.subplots(1)

# Set some limits
ax.set_xlim(-1,5)
ax.set_ylim(-2,4)

# Plot errorbars with the line color you want
ax.errorbar(x,y,yerr, fmt='o',color='r',capthick=2,elinewidth=2,capsize=3,zorder=10)

# Plot black errorbars behind (lower zorder) with a wider line and cap thinkness
ax.errorbar(x,y,yerr, fmt='o',color='k',capthick=4,elinewidth=4,capsize=4,zorder=5)

plt.show()

enter image description here


Again, not a perfect solution, but at least it allows you to include it in the legend. This time, rather than plot the errorbars twice, we will use the matplotlib.patheffects module to add a Stroke to the errorbars.

errorbar returns several Line2D and LineCollection objects, so we need to apply the stroke to each of the relevant ones.

import matplotlib.patheffects as path_effectse= ax.errorbar(x,y,yerr, fmt='o',color='r',capthick=2,elinewidth=2, label='path effects')

e[1][0].set_path_effects([path_effects.Stroke(linewidth=4, foreground='black'),
                          path_effects.Normal()])
e[1][1].set_path_effects([path_effects.Stroke(linewidth=4, foreground='black'),
                          path_effects.Normal()])
e[2][0].set_path_effects([path_effects.Stroke(linewidth=4, foreground='black'),
                          path_effects.Normal()])

ax.legend(loc=0)

enter image description here

Solution 2:

As far as I can see from the information provided in the webpage of pyplot I do not see a valid kwargs that exists for what you are asking.

There exists mfc, mec, ms and mew which are markerfacecolor, markeredgecolor, markersize and markeredgewith. It can probably be asked in GitHub so that people take this into consideration and add it in the next version of matplotlib.

Also taking a look at the answer for this question asked in Stackoverflow, I don't believe it can be done.

Post a Comment for "Border On Errorbars In Matplotlib/python"