【问题标题】:Plotting the "relation" of two functions dependent values绘制两个函数相关值的“关系”
【发布时间】:2017-07-25 01:08:40
【问题描述】:

我正在尝试设计或找到一种方法,让我可以绘制两个函数的比率值(我想不出正确的词)以相互绘制。

例如,它会像查找具有两个因变量 xy f(x) = g(y) 的两个函数的根。但我想根据它们的功能绘制xy 之间的关系。

例如:

这就像尝试查看这些值在哪里匹配相同的 N(>M_h) = N(>M_star)。查看它们的 x 值(从该图像中截取)与它们匹配的 y 值匹配的位置,然后绘制它们的关系,例如 M_h(M_star) 或 M_star(M_h)。

不幸的是,我没有任何示例代码,因为我不知道如何启动这样的方法。

如果他们需要更详细的说明,请告诉我。

【问题讨论】:

    标签: python matplotlib plot scipy


    【解决方案1】:

    绘制两个独立变量 xy 之间的关系,以函数 f(x)g(y) 为条件,可以通过在 f(x)-g(y) = 0 处绘制等高线来完成。

    import numpy as np
    import matplotlib.pyplot as plt
    
    f = lambda x: x**2
    g = lambda y: np.sqrt(y)
    
    x = np.linspace(0,2,101)
    y = np.linspace(0,10,101)
    
    fig, (ax, ax2, ax3) = plt.subplots(ncols=3, figsize=(10,4))
    
    ax.plot(x, f(x))
    ax.plot([1.5],[f(1.5)], marker="o")
    ax2.plot(y, g(y))
    ax2.plot([f(1.5)**2],[f(1.5)], marker="o")
    
    #create a meshgrid from the x and y array
    X,Y = np.meshgrid(x,y)
    #plot the contour f(X)-g(Y) = 0
    c = ax3.contour(X,Y, f(X)-g(Y), 0)
    ax3.plot([1.5],[f(1.5)**2], marker="o")
    
    ax.set_ylim([0,4])
    ax2.set_ylim([0,4])
    ax.set_xlabel("x")
    ax.set_ylabel("f(x)")
    ax2.set_xlabel("y")
    ax2.set_ylabel("g(y)")
    ax3.set_xlabel("x")
    ax3.set_ylabel("y")
    
    ax3.legend(handles=[c.collections[0]], labels=["f(x) = g(y)"])
    plt.tight_layout()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-19
      • 2017-08-18
      • 1970-01-01
      • 2020-08-14
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-14
      相关资源
      最近更新 更多