1

I am trying to figure out how to make only upper error bar with cap "-" in my plot, which I already figured out by lolims argument. Unfortunately, my error bar is marked with an arrow, and I would prefer the default one. The upper subplot is with error bar I would like to have, and the lower one is with the mark I would like to have on my upper subplot.

My code:

import pandas as pd
import matplotlib.pyplot as plt

sheet = pd.read_excel(load file with my data)

fig, axs = plt.subplots(2, 1)
fig.suptitle('Gene expression', fontsize=16)

axs[0].bar(sheet.columns, sheet.iloc[17],hatch="/", color="white", edgecolor="black")
axs[0].errorbar(sheet.columns, sheet.iloc[17], yerr=sheet.iloc[22], capsize=3, ecolor='black', fmt=' ', elinewidth=1,
                lolims=True)

axs[1].bar(sheet.columns, sheet.iloc[17],hatch="-", color="white", edgecolor="black")
axs[1].errorbar(sheet.columns, sheet.iloc[17], yerr=sheet.iloc[22], capsize=3, ecolor='black', fmt=' ', elinewidth=1,)
plt.show() 

and picture of my plots: enter image description here

How can I achieve it?

2

1 Answer 1

2

You have three possibilites depending on whether you'd like to see the lower caps:

  1. Specify an (2,N)-shaped yerr or
  2. Plot the error bars behind the bars
  3. Change the caps afterwards (as commented below by BigBen)

import matplotlib.pyplot as plt

fig,ax = plt.subplots(3)
x = range(1,4)

ax[0].bar(x, x, fc='w', ec='k')
ax[0].errorbar(x, x, yerr=[[0,0,0], [.1,.2,.3]], fmt='none', capsize=3)

ax[1].bar(x, x, fc='w', ec='k', zorder=1 )
ax[1].errorbar(x, x, yerr=[.1,.2,.3], fmt='none', capsize=3, zorder=0)

ax[2].bar(x, x, fc='w', ec='k')
_, cl, _ = ax[2].errorbar(x, x, yerr=[.1,.2,.3], lolims=True, fmt='none', capsize=3)
cl[0].set_marker('_')
#cl[1].set_marker('')  # to remove the lower cap

enter image description here

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

2 Comments

Seems like changing the arrows to caps is a third possibility based on the potential dupe, or am I missing something?
@BigBen: yes, indeed - in this case you have full control of the caps, so if you like you could even have different one for diffent columns.

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.