【发布时间】:2018-11-18 14:54:59
【问题描述】:
我正在尝试绘制连接开始 (x,y) 和结束 (x,y) 的线 这意味着一条线将 (x1start,y1start) 连接到 (x1end,y1end) 我在数据框中有多行。 复制实际数据帧的示例数据帧,如下所示:
df = pd.DataFrame()
df ['Xstart'] = [1,2,3,4,5]
df ['Xend'] = [6,8,9,10,12]
df ['Ystart'] = [0,1,2,3,4]
df ['Yend'] = [6,8,9,10,12]
据此,如果我们查看 df 的第一行,一条线将连接 (1,0) 和 (6,6) 为此,我使用 for 循环为每一行画一条线,如下所示:
fig,ax = plt.subplots()
fig.set_size_inches(7,5)
for i in range (len(df)):
ax.plot((df.iloc[i]['Xstart'],df.iloc[i]['Xend']),(df.iloc[i]['Ystart'],df.iloc[i]['Yend']))
ax.annotate("",xy = (df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
xycoords = 'data',
xytext = (df.iloc[i]['Ystart'],df.iloc[i]['Yend']),
textcoords = 'data',
arrowprops = dict(arrowstyle = "->", connectionstyle = 'arc3', color = 'blue'))
plt.show()
运行此程序时出现以下错误消息。
得到如下图:
箭头和线如预期的那样。箭头应该在每行的终点。
谁能告诉我这里发生了什么?
谢谢,
泽普
【问题讨论】:
标签: python pandas matplotlib annotate