python - 按 x 和 y 坐标绘制条形图

标签 python matplotlib plot

我想用 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])

enter image description here

我想要的是这样的:

enter image description here

最佳答案

这种情况下的解决方案比链接问题中的解决方案要容易得多。您只需使用 plt.barbottom 参数即可。

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()

enter image description here

关于python - 按 x 和 y 坐标绘制条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43139771/

相关文章:

python - 如何编辑类属性

python - 通过顶点索引删除边的更快方法 [igraph Python]

python - 在 mac osx lion 中安装 MatplotLib

python Matplotlib gtk - 使用 FuncAnimation 绘制动画图

python - Pandas 数据帧的视觉探索

Python 矩阵给出与最后一条记录相同的结果

javascript - ERROR :root:code for hash md5 was not found. npm 安装错误

matplotlib - pyplot直方图箱如何解释?

r - 缺失数据的网格图

matlab - 绘制 3D 线,matlab