【问题标题】:Matplotlib: Draw lines from x axis to pointsMatplotlib:从 x 轴到点画线
【发布时间】:2011-12-09 06:29:24
【问题描述】:

我有一堆点,我正在尝试使用 matplotlib 绘制。对于每个点 (a,b),我想在 [0,b] 中为 Y 绘制线 X = a。知道怎么做吗?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您只需使用两个端点绘制每条线。 [0,b] 中 Y 的垂直线 X=a 具有端点 (x,y) = (a,0) 和 (a,b)。 所以:

    # make up some sample (a,b): format might be different to yours but you get the point.
    import matplotlib.pyplot as plt
    points = [ (1.,2.3), (2.,4.), (3.5,6.) ] # (a1,b1), (a2,b2), ...
    
    plt.hold(True)
    plt.xlim(0,4)  # set up the plot limits
    
    for pt in points:
        # plot (x,y) pairs.
        # vertical line: 2 x,y pairs: (a,0) and (a,b)
        plt.plot( [pt[0],pt[0]], [0,pt[1]] )
    
    plt.show()
    

    提供如下内容:

    【讨论】:

      【解决方案2】:

      使用stem 绘图

      最简单的解决方案采用matplotlib.pyplot.stem

      import matplotlib.pyplot as plt
      x = [1. , 2., 3.5]
      y = [2.3, 4., 6.]
      plt.xlim(0,4)
      plt.stem(x,y)
      plt.show()
      

      【讨论】:

      • 正在寻找这个。谢谢:)
      猜你喜欢
      • 2016-06-22
      • 2017-03-25
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多