我试图画出连接起点(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()
我运行此程序时出现以下错误消息
我得到的数字如下所示:
箭头和线条按预期插入。箭头应该在每行的终点
有人能告诉我这是怎么回事吗
谢谢你
泽普
你把箭头的位置搞混了。
xy
和xytext
中的每个坐标对由x和y值组成此外,为了查看绘图中的箭头,您需要手动设置绘图的限制,因为在缩放数据限制时,注释是不被考虑的
不是100%确定,但我认为在第二行中,您需要将xy=tuple后面的部分设置为,否则它会将前面的部分设置为关键字参数,并尝试将后面的部分作为普通参数传递
如果要绘制线段,请使用以下代码。您可能需要箭头或某种
annotate
元素(注意拼写是否正确),但您的目标似乎是绘制线段,这就完成了:相关问题 更多 >
编程相关推荐