没有针对轴的.axvspan()
等效替代品。
而.axvline()
也并不适用于刻度。
而且没有任何意图用于标记.axvspan()
。
因此,让我们回答一个问题,如何在绘图下方生成一些红色括号(圆括号)。你可以基本上通过绘制一条形式为括号的线,并根据xaxis变换来定位它。下面是一个执行此操作的函数。
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.bar(range(-1,5),range(3,9), width=1, align="edge", ec="k", alpha=1)
ax.set_xticks(range(1,6))
def bracket(ax, pos=[0,0], scalex=1, scaley=1, text="",textkw = {}, linekw = {}):
x = np.array([0, 0.05, 0.45,0.5])
y = np.array([0,-0.01,-0.01,-0.02])
x = np.concatenate((x,x+0.5))
y = np.concatenate((y,y[::-1]))
ax.plot(x*scalex+pos[0], y*scaley+pos[1], clip_on=False,
transform=ax.get_xaxis_transform(), **linekw)
ax.text(pos[0]+0.5*scalex, (y.min()-0.01)*scaley+pos[1], text,
transform=ax.get_xaxis_transform(),
ha="center", va="top", **textkw)
bracket(ax, text="0", pos=[-1,-0.01], linekw=dict(color="crimson", lw=2) )
plt.show()