【问题标题】:Matplotlib: Can a plot a line from one set of axes to another?Matplotlib:可以绘制从一组轴到另一组轴的线吗?
【发布时间】:2013-09-02 10:53:08
【问题描述】:

我希望在 [100,100] 处绘制标记“x”,然后在 [20%, 30%] 处绘制“o”(不同的轴,相同的图)并用一条线连接它们。我可以在相同的轴(使用相同的单位)上做类似的事情,一次调用绘制线,另一次调用绘制“x”,最后调用绘制“o”。

ax.plot(x,y,"-")
ax.scatter(x[0], y[0], marker='x')
ax.scatter(x[1], y[1], marker='o')

但是,我怎样才能让线从一组轴转到另一组?

【问题讨论】:

  • 您应该包含更多代码,因为很难准确理解您的意思。如果没有更多上下文,我们可能只能猜测您是如何创建坐标区的。

标签: python matplotlib plot


【解决方案1】:

你可以使用annotate来画单线:

ax1 = plt.subplot(121)
ax2 = plt.subplot(122)

x = [[1, 2], [3,4]]
y = [[5, 6], [6,4]]

ax1.scatter(x[0], y[0])
ax2.scatter(x[1], y[1])


ax1.annotate('', xy=(x[0][0], y[0][0]), xytext=(x[1][0], y[1][0]), xycoords=ax1.transData, 
         textcoords=ax2.transData, 
         arrowprops=dict(facecolor='black', arrowstyle='-',, clip_on=False))
ax2.annotate('', xy=(x[0][0], y[0][0]), xytext=(x[1][0], y[1][0]), xycoords=ax1.transData, 
         textcoords=ax2.transData, 
         arrowprops=dict(facecolor='black', arrowstyle='-'))

产生这个结果:

【讨论】:

    【解决方案2】:

    我不确定我是否完全理解了你的问题,但是把这些放在一起看看是否是你要找的?

    import pylab
    
    #generate array of data for example
    import numpy as np
    x = np.arange(1,250,1)
    y = np.arange(1,250,1)
    
    #find marker for your 'x' points
    x_marker_location = 100
    x_marker_x = x[np.where(x==x_marker_location)]  # np.where looks for location in your data where array equals a value. Alternatively, x_marker_x and y would just be a coordinate value.
    x_marker_y = y[np.where(y==x_marker_location)]
    
    #create scaling factors
    o_marker_scale_x = 0.2
    o_marker_scale_y = 0.3
    #find marker for your 'o' points
    o_marker_x = x[np.where(x==x_marker_location*o_marker_scale_x)]
    o_marker_y = y[np.where(y==x_marker_location*o_marker_scale_y)]
    
    #draw line of all data
    pylab.plot(x,y,"-",color='black')
    #draw points interested in
    pylab.scatter(x_marker_x, x_marker_y, marker='x')
    pylab.scatter(o_marker_x, o_marker_y, marker='o')
    #draw connecting line - answer to question?
    pylab.plot([x_marker_x,o_marker_x],[x_marker_y,o_marker_y],'-',color='red')
    
    #show plot
    pylab.show()
    

    【讨论】:

      【解决方案3】:

      对于任何想要在一个图形上绘制高维数据的人,通过绘制连接不同维度上的点的线,请注意您正在寻找的东西称为“平行坐标图”。

      matplotlib 中有可能的解决方案,以及使用 pandas 和 plotly 的解决方案:

      Parallel Coordinates plot in Matplotlib

      【讨论】:

        猜你喜欢
        • 2015-04-22
        • 2016-09-26
        • 1970-01-01
        • 2021-11-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-30
        • 1970-01-01
        • 2022-12-18
        相关资源
        最近更新 更多