Plot a function that has two parts, a constant part and a variable part with matplotlib

huangapple go评论66阅读模式
英文:

Plot a function that has two parts, a constant part and a variable part with matplotlib

问题

  1. 在图像1中,我们以图中显示的函数作为示例来回答我的问题:

  2. 在图像中,您可以看到从0到t2的递减函数,在我的情况下,我有一个特定的y轴值,就像在图像中的“m”一样。

    1. 我有“m”的值,我需要知道它的x值,在这个示例中,它是“t1”值,我该如何获取它呢?我知道interp()函数可以获取y值,但我不知道是否可以使用它来获取x值?
    1. 在获取“m”的x值(t1)后,我必须删除“m”之前的值或数据,否则在“t1”之前的值将被替换为一个常数,该常数就是“m”,这样它将从0到t1呈现为[y(x)=m]常数,并从t1开始呈现为递减函数,一直到x轴的末尾范围。
  3. 例如:

x_axis=np.arange(0, frequency, 0.1)
y_axis=function(x_axis)

(https://stackoverflow.com/questions/55343417/find-y-value-for-respective-x-from-python-plot-matplotlib)

英文:

Let's use the function shown in image 1 as an example for my question:

Plot a function that has two parts, a constant part and a variable part with matplotlib

in the image you can see a decreasing function from 0 to t2, in my case I have one specific value of y_axis, like in the image which is the "m".

1. I have the value for "m" I need to know the x_value of it in this example it's the "t1" value, how can i get it, I know the interp() function to get the y_values, I don't know if I can use it to do the reverse to get the y_values?

2. after getting the x_value of "m" (t1) I have to delete the values or the data before the "m" otherwise the values before the "t1" or replace them with a constant which is the "m" so it will look like a [y(x)=m] constant from 0 to t1 and look like a decreasing function from t1 to the end of the range of x_axis.

something like:

Plot a function that has two parts, a constant part and a variable part with matplotlib

for example:

x_axis=np.arange(0, frequency, 0.1)
y_axis=function(x_axis)

(https://stackoverflow.com/questions/55343417/find-y-value-for-respective-x-from-python-plot-matplotlib)

答案1

得分: 0

这是使用NumPy的interppiecewise函数的示例。首先,我会定义一个示例函数来生成数据点,但之后假设您只有这些点,无法访问原始函数(这是我从您的问题中理解的)。

from matplotlib import pyplot as plt
import numpy as np

# 创建函数(您可能无法访问)
def myfunc(t, r=1.0):
    return np.sqrt(r**2 - t**2)

# 生成一些函数已被评估的点
t = np.linspace(0, 1, 100)  # 在0到1之间均匀分布的100个点
y = myfunc(t)  # 在点t处评估的函数值

# 假设我们只有t和y(没有myfunc),在某一点t1处进行函数值插值
t1 = 0.68354844
y1 = np.interp(t1, t, y)

# 使用piecewise获取一个函数,在t1以下是常数,在t1以上跟随原始函数(可能在t的不同点评估)
tnew = np.linspace(0, 1, 150)  # 用于评估函数的新点(更高采样率)

condlist = [tnew <= t1, tnew > t1]  # piecewise条件列表
funclist = [y1, np.interp]  # 对于第一个条件使用常数y1,对于第二个条件使用interp

# 在tnew处评估piecewise函数
yvals = np.piecewise(tnew, condlist, funclist, t, y)

# 绘制原始函数和具有常数部分的新版本
fig, ax = plt.subplots()
ax.plot(t, y, label="原始函数")
ax.plot(tnew, yvals, ls="--", label="Piecewise函数")
ax.legend()

fig.show()

这给出了如下图所示的结果:

Plot a function that has two parts, a constant part and a variable part with matplotlib

英文:

Here is an example of what you could do using the NumPy interp and piecewise functions. I'll first define an example function to generate the data points, but after that will assume that you just have the points and don't have access to the original function (which is what I assume from your question).

from matplotlib import pyplot as plt
import numpy as np


# create the function (which you may not have access to)
def myfunc(t, r=1.0):
    return np.sqrt(r**2 - t**2)

# generate some points at which the function has been evaluate
t = np.linspace(0, 1, 100)  # 100 points linearly spaced between 0 and 1
y = myfunc(t)  # the function evaluated at the points t

# assuming we just have t and y (and not myfunc), interpolate the value of
# the function at some point t1
t1 = 0.68354844
y1 = np.interp(t1, t, y)

# use piecewise to get a function which is constant below t1 and follows the
# original function above t1 (potentially evaluated at different points in t)
tnew = np.linspace(0, 1, 150)  # new (more highly sampled) points at which to evaluate the function

condlist = [tnew &lt;= t1, tnew &gt; t1]  # list of piecewise conditions
funclist = [y1, np.interp]  # use constant y1 for first condition and interp for second condition

# evaluate the piecewise function at tnew
yvals = np.piecewise(tnew, condlist, funclist, t, y)

# plot the original function and the new version with the constant section
fig, ax = plt.subplots()
ax.plot(t, y, label=&quot;Original function&quot;)
ax.plot(tnew, yvals, ls=&quot;--&quot;, label=&quot;Piecewise function&quot;)
ax.legend()

fig.show()

which gives:

Plot a function that has two parts, a constant part and a variable part with matplotlib

huangapple
  • 本文由 发表于 2023年2月6日 21:16:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定