I'm trying to reproduce this diagram:
but I'm having trouble creating the horizontal lines with bars. I've tried annotate
and hlines
but they don't quite give the effect I'm after.
import matplotlib.pyplot as plt
plt.grid(which = 'both')
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(-0.5,8)
plt.ylim(-0.5,10)
plt.xlabel('Redshift, z', fontsize = 16)
plt.hlines(8, 0, .3)
plt.annotate(r'H$\alpha$', fontsize = 16, xy = (0,8), xycoords='data', xytext=(0,8),
textcoords='data',
arrowprops=dict(arrowstyle='<|-|>', connectionstyle='arc3', color = 'k', lw=2))
fig = plt.gcf()
width, height = 15,35 # inches
fig.set_size_inches(width, height, forward = True)
plt.show()
What's the best way to produce the bars like this?
I would use annotate
directly, but for more flexibility, I would separate the drawing of the horizontal bars and the corresponding text
plt.figure()
plt.grid(which = 'both')
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(-0.5,8)
plt.ylim(-0.5,10)
plt.xlabel('Redshift, z', fontsize = 16)
bar_ys = [8,4]
bar_xs = [[0,6],[3,5]]
bar_texts = [r'H$\alpha$',r'H$\beta$']
bar_color = ['k','orange']
for y,xs,t,c in zip(bar_ys,bar_xs,bar_texts,bar_color):
plt.annotate('', xy = (xs[0],y), xycoords='data', xytext=(xs[1],y),
arrowprops=dict(arrowstyle='|-|', color=c, lw=2, shrinkA=0, shrinkB=0))
plt.annotate(t, xy = (xs[1],y), xycoords='data', xytext=(-5,5), textcoords='offset points',
fontsize = 16, va='baseline', ha='right', color=c)
plt.show()