熊猫图-范围

时间:2020-07-23 03:57:06

标签: python pandas

我必须绘制类似这样的东西

enter image description here

data = {'begin': ['10', '15','17','25','7','8','16'],
        'end': ['15', '20', '20', '30', '15','10','19' ],
       'duration' :['5','5','3','5','8','2','3']}

我希望绘图包含相对于x轴的“开始”和“结束”。所以每一行都是一个范围(开始和结束)。有没有办法在大熊猫中绘制这种方式?

1 个答案:

答案 0 :(得分:1)

您的问题中的图形和数据不匹配,但是您要根据数据执行以下操作吗?我们尚未实现添加圈子或其他任何操作。请尝试看看。

import pandas as pd

data = {'begin': ['10', '15','17','25','7','8','16'],
        'end': ['15', '20', '20', '30', '15','10','19' ],
       'duration' :['5','5','3','5','8','2','3']}

df = pd.DataFrame(data)

import matplotlib.pyplot as plt
import matplotlib.patches as patches

fig = plt.figure(figsize=(4,3),dpi=144)
ax = fig.add_subplot(111)

for index,row in df.iterrows():
#     print(index,row[0],row[1])
    r = patches.Rectangle(xy=(int(row[0]),int(index+1)), width=int(row[2]), height=0.4, ec='k', fc='k', fill=True)
    ax.add_artist(r)

ax.set_ylim(0,10)
ax.set_xlim(0,35)

enter image description here