【问题标题】:Fitting x and y to the same dimension将 x 和 y 拟合到相同的维度
【发布时间】:2017-09-17 14:29:33
【问题描述】:

我刚刚发布了一个类似的代码,但现在有一个不同的问题。假设我将 y 绑定为 0 或 1,并且我希望我的 x 具有大量值,我如何让 x 和 y 达到相同的维度?

#Part A - Plot function against values of variable x
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as quad
import math 

#Make heaviside "theta" function
def heaviside (x): 
    if np.any(x >= 0):
        return 1
    else: 
        return 0  

#Plot
x = np.linspace(-5,5,11)
y = heaviside(x)*[1-heaviside(x-1)]
plt.plot(x, y)
plt.show()

【问题讨论】:

    标签: python-3.x function matplotlib dimensions


    【解决方案1】:

    如果目的是绘制重载函数,您可以使用numpy.heaviside

    import numpy as np
    import matplotlib.pyplot as plt 
    
    x = np.linspace(-5,5,101)
    y = np.heaviside(x,1)*(1-np.heaviside(x-1,1))
    plt.plot(x, y)
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 2017-11-28
      • 2014-04-25
      • 2018-09-30
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多