我想在 python 中绘制一个悬挂的根图。我知道这里已经有一个问题:How to plot a hanging rootogram in python? 但我的方法有点不同。
在我的 pandas 数据框中,数据是这样组织的:
x:是x轴点,这里是1,2,3,...
height : 是栏的高度
y:绘图的起点
线:每个柱的边界线
我想要的是每个点 x 的条形图,它从 y 开始并且是 height 。然后栏的末尾位于行
这是我到目前为止得到的:
fig, ax = plt.subplots()
rects = ax.bar(df['x'], df['height'] - df['y'], 1)
ax.plot(df['x'], df['line'], color='r')
ax.set_ylim([-10,15])
我想要的是这样的:
请您参考如下方法:
这种情况下的解决方案比链接问题中的解决方案容易得多。您只需要使用 plt.bar 的 bottom 参数。
import numpy as np; np.random.seed(0)
import matplotlib.pyplot as plt
x = np.arange(12)
line = np.sqrt(np.sinc(np.sqrt(x/2.))**2)*2+0.35
y = np.random.rand(12)-1
#height is redundant
height = line-y
plt.bar(x, height, bottom=y, edgecolor="k" )
plt.plot(x,line, color="#dd0000")
plt.plot(x,y, marker="o", ls="", color="k")
plt.ylim(-2,3)
plt.show()
