繁体   English   中英

我怎样才能把 plot 分段成 function 在 python 中?

[英]How can I plot this piecewise function in python?

分段 function

我不知道如何 plot 它

尝试使用matplotlibpyplot package。它具有非常人性化的绘图功能。 这是我绘制分段 function 的方法:

import matplotlib.pyplot as plt
import numpy as np

# boundaries
xmin = -3
xmax = 3

N = 1001 # resolution of points between [xmin, xmax]

# create x axis and empty y array
x = np.linspace(xmin, xmax, N)
y = np.zeros(N)

# apply the piecewise operations:
y[x<0] = np.sin(x[x<0])
y[(0<x) & (x<1)] = np.arctan(x[(0<x) & (x<1)])
y[1<x] = np.pi*x[1<x]**3/4

# plot
plt.plot(x, y)
plt.show()

在绘图之前,我使用带有分段条件的数组切片来编辑y数组。 希望这可以帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM