2

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=2,zorder=10)

produces:

errorbar

I find it more aesthetically pleasing if there was a black border around the errorbar like there is on the marker.

Thanks for any help you can provide

2 Answers 2

4

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_effects

e = 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

Sign up to request clarification or add additional context in comments.

5 Comments

I would also add 'capsize = x' to make the sides of the caps also appear in black
Any idea how one would go by so that it also appears in the legend ?
Good idea re: capsize. No idea about the legend, sorry
@AnthonyLethuillier OK, So I do have an idea - we can use patheffects. I've edited the answer. Its still not perfect, but might give you something to play with.
That's great, I use it for now, I posted an issue on Gihub we will see what comes of it.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.