按x和y坐标绘制条形图

2024-10-01 11:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python绘制一个悬挂的根程序。我知道这里已经有一个问题了:How to plot a hanging rootogram in python? 但我的方法有点不同。在

在我的pandas dataframe中,数据是这样组织的:

x:是x轴的点,这里是1、2、3,。。。在

高度:是条的高度

y:绘图起点

线:每个条的边界线

我想要的是每个点的条形图,从y开始,高度。然后,该条的末尾位于

到目前为止我得到的是:

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


Tags: to数据方法in程序dataframepandasdf
1条回答
网友
1楼 · 发布于 2024-10-01 11:21:40

这种情况下的解决方法比关联问题简单得多。您只需要使用bottombottom参数。在

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

{a1}

相关问题 更多 >