函数间隔

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

Functions intervals

问题

以下是您的代码部分的翻译:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,100)
y = 2*x+1
k = 3*x+2
i = 2*x+2
plt.plot(x, y, '-r', label='y=2x+1')
plt.plot(x, k, '-r', label='k =3x+2')
plt.plot(x, i, '-r', label='i =2x+2')
plt.title('3个函数在3个区间上的图像')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()

希望这对您有所帮助。

英文:

I have 3 functions, how can I plot them using differents intervals ?

This is my code:

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5,5,100)
y = 2*x+1
k = 3*x+2
i = 2*x+2
plt.plot(x, y, '-r', label='y=2x+1')
plt.plot(x, k, '-r', label='k =3x+2')
plt.plot(x, i, '-r', label='i =2x+2')
plt.title('3 functions on 3 intervals')
plt.xlabel('x', color='#1C2833')
plt.ylabel('y', color='#1C2833')
plt.legend(loc='upper left')
plt.grid()
plt.show()

Wanted style : 3 intervals, 3 linear functions :

函数间隔

This is what I get :
函数间隔

My wish is to draw the first function 2*x+1 on the following interval [x:2->x:8].

The second function 3*x+2 has to be plotted on the interval [x:8->x:12]

The third function 2*x+2 has to be plotted on the interval [x:12->x:20]

Is it possible ?

Edit :
Ended up with this :

x = np.linspace(-5,0,100)
t = np.linspace(0,5,100)
m = np.linspace(5,10,100)
y = 2*x+1
k = 3*x-2
i = 2*x+2
plt.plot(x, y, '-r', label='y=2x+1')
plt.plot(t, k, '-r', label='k =3x-2')
plt.plot(m, i, '-r', label='i =2x+2')

Result :

函数间隔

答案1

得分: 1

这是你想要的吗?

函数间隔

在 [33]: 中导入 matplotlib.pyplot as plt
    ...: import numpy as np
    ...:
    ...: xs = [np.linspace(0,4), np.linspace(-3, 5), np.linspace(4, 10)]
    ...: fs = [np.cos, np.sin, lambda x:np.sin(x)-2*np.cos(x)]
    ...: for x, f in zip(xs, fs):
    ...:     plt.plot(x, f(x), label=f.__name__)
    ...: plt.legend()
    ...: plt.show()

绘制线性函数没有区别,

函数间隔

import matplotlib.pyplot as plt
import numpy as np

xs = [np.linspace(0,4), np.linspace(-3, 5), np.linspace(4, 10)]
fs = [np.cos, np.sin, lambda x:(x-6)*0.5]
fs[-1].__name__ = 'x/2-3'
for x, f in zip(xs, fs):
    plt.plot(x, f(x), label=f.__name__)
plt.legend()
plt.show()

仅当您要绘制仅为线性函数时,另一种方法可能是

函数间隔

import matplotlib.pyplot as plt

# 绘制 y = a x + b
y = lambda xmin, xmax, a, b: (a*xmin+b, a*xmax+b)
format = lambda b: ('y = %.2f x + %.2f' if b>=0 else 'y = %.2f x – %.2f')
Xmin = [0, 4, 7]
Xmax = [5, 6, 9]
A = [1, 0.5, 3]
B = [-2, 0, 3]

for xmin, xmax, a, b in zip(Xmin, Xmax, A, B):
    plt.plot((xmin, xmax), y(xmin, xmax, a, b),
             label=format(b)%(a, abs(b)))
plt.legend()
plt.show()
英文:

Is it this that you want?

函数间隔

In [33]: import matplotlib.pyplot as plt
    ...: import numpy as np
    ...: 
    ...: xs = [np.linspace(0,4), np.linspace(-3, 5), np.linspace(4, 10)]
    ...: fs = [np.cos, np.sin, lambda x:np.sin(x)-2*np.cos(x)]
    ...: for x, f in zip(xs, fs):
    ...:     plt.plot(x, f(x), label=f.__name__)
    ...: plt.legend()
    ...: plt.show()

Plotting a linear function is no different,

函数间隔

import matplotlib.pyplot as plt
import numpy as np

xs = [np.linspace(0,4), np.linspace(-3, 5), np.linspace(4, 10)]
fs = [np.cos, np.sin, lambda x:(x-6)*0.5]
fs[-1].__name__ = 'x/2-3'
for x, f in zip(xs, fs):
    plt.plot(x, f(x), label=f.__name__)
plt.legend()
plt.show()

If and only if you are going to plot ONLY LINEAR FUNCTIONS,
another approach could be

函数间隔

import matplotlib.pyplot as plt

# plotting y = a x + b
y = lambda xmin, xmax, a, b: (a*xmin+b, a*xmax+b)
format = lambda b: ("y = %.2f x + %.2f"if b>=0 else"y = %.2f x – %.2f")
Xmin = [0, 4, 7]
Xmax = [5, 6, 9]
A = [1, 0.5, 3]
B = [-2, 0, 3]

for xmin, xmax, a, b in zip(Xmin, Xmax, A, B):
    plt.plot((xmin, xmax), y(xmin, xmax, a, b),
             label=format(b)%(a, abs(b)))
plt.legend()
plt.show()

huangapple
  • 本文由 发表于 2023年6月1日 22:30:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76382989.html
匿名

发表评论

匿名网友

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

确定