我想用 python 绘制一个悬挂的根图。我知道这里已经有一个问题:How to plot a hanging rootogram in python? 但我的方法有点不同。
在我的 pandas 数据框中,数据的组织方式如下:
x:是x轴点,这里是1,2,3,...
高度:是栏的高度
y:绘图的起点
线:每个条形的边界线
我想要的是每个点 x 的条形图,从 y 开始,高度为 x 。条形的末端位于行
这是我到目前为止得到的:
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()
关于python - 按 x 和 y 坐标绘制条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43139771/