【问题标题】:Line plot for each coordinate in matplot libmatplotlib 中每个坐标的线图
【发布时间】: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


    【解决方案1】:

    您混淆了箭头的位置。 xyxytext 中的每个坐标对都包含一个 x 和 y 值。

    此外,为了查看图中的箭头,您需要手动设置图的限制,因为注释 - 有充分的理由 - 在缩放数据限制时不会考虑在内。

    import pandas as pd
    import matplotlib.pyplot as plt
    
    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]
    
    
    fig,ax = plt.subplots()
    fig.set_size_inches(7,5)
    
    for i in range (len(df)):
        ax.annotate("",xy = (df.iloc[i]['Xend'],df.iloc[i]['Yend']),
                    xycoords = 'data',
                    xytext = (df.iloc[i]['Xstart'],df.iloc[i]['Ystart']),
                    textcoords = 'data',
                    arrowprops = dict(arrowstyle = "->", 
                                      connectionstyle = 'arc3', color = 'blue'))
    
    ax.set(xlim=(df[["Xstart","Xend"]].values.min(), df[["Xstart","Xend"]].values.max()),
           ylim=(df[["Ystart","Yend"]].values.min(), df[["Ystart","Yend"]].values.max()))
    plt.show()
    

    【讨论】:

    • 感谢您的回复。我解决了 turple,但是当我绘制它时,我得到了空白的数字。我已经更新了问题。
    • 更新了答案
    • 谢谢。非常感谢您的帮助
    【解决方案2】:

    如果你想绘制线段,下面的代码可以工作。您可能需要箭头或某种 annotate 元素(注意拼写正确),但您的目标似乎是绘制线段,这可以实现:

    import pandas as pd
    import matplotlib.pyplot as plt
    
    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]
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    for i in range (len(df)):
        ax.plot(
            (df.iloc[i]['Xstart'],df.iloc[i]['Xend']),
            (df.iloc[i]['Ystart'],df.iloc[i]['Yend'])
        )
    plt.show()
    

    【讨论】:

    • 我试图添加箭头和错误的东西。更新问题。
    【解决方案3】:

    不是 100% 确定,但我认为在第二行中,您需要在 xy= 之后创建一个元组,否则它会将 , 前面的部分设置为关键字参数,并尝试将 , 之后的部分作为正常 arg

    【讨论】:

    • 感谢您的回复。我解决了 turple,但是当我绘制它时,我得到了空白的数字。我已经更新了问题。
    猜你喜欢
    • 2016-08-17
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2015-03-10
    • 2017-05-18
    相关资源
    最近更新 更多