【问题标题】:Set points outside plot to upper limit将绘图外的点设置为上限
【发布时间】:2016-10-06 20:49:08
【问题描述】:

也许这个问题已经存在,但我找不到。

我正在用 Python 制作散点图。出于说明目的,我不想设置我的坐标轴范围以包括所有点 - 可能有一些非常高或非常低的值,我关心的是这些点是否存在 - 也就是说,它们需要在情节中,但不是在它们的实际价值上——而是在画布顶部的某个地方。

我知道在 IDL 中有一个很好的简短语法:在 plot(x,y<value) 中,y 中任何大于 value 的值都将简单地放在 y=value

我在 Python 中寻找类似的东西。有人可以帮帮我吗?

【问题讨论】:

    标签: python matplotlib plot limits


    【解决方案1】:

    您可以在y 数据上使用np.minimum 将超出上限的任何内容设置为该限制。 np.minimum 按元素计算最小值,因此只有大于 ymax 的值才会设置为 ymax

    例如:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x = np.linspace(0., np.pi*2, 30)
    y = 10. * np.sin(x)
    
    ymax = 5
    
    fig, ax = plt.subplots(1)
    ax.scatter(x, np.minimum(y, ymax))
    
    plt.show()
    

    【讨论】:

    • 如何设置下限?就像我试过这个 xmin=0.8, xmax=0.18 和 plt.(np.minimum(x, xmin,xmax),y) 但它没有用。! @tom
    【解决方案2】:

    matplotlib 中没有等效的语法糖。您必须对数据进行预处理,例如:

    import numpy as np
    import matplotlib.pyplot as plt
    
    ymin, ymax = 0, 0.9
    x, y = np.random.rand(2,1000)
    y[y>ymax] = ymax
    fig, ax = plt.subplots(1,1)
    ax.plot(x, y, 'o', ms=10)
    ax.set_ylim(ymin, ymax)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 2016-11-12
      • 1970-01-01
      相关资源
      最近更新 更多