Python和MATLAB的心心相印表白码

% 这个并没有什么神奇的,数学函数加上简单的参数调整就可以了
x=-2:0.01:2;
y1=sqrt(1-(abs(x)-1).^2);
plot(x,y1,'r','LineWidth',5);
y2=acos(1-abs(x))-pi;
hold on
plot(x,y2,'r','LineWidth',5);
x=-2:0.01:5;
y3=0.45*x-1.4;
hold on
plot(x,y3,'m','LineWidth',2.5);
hold on
x=4.5:0.01:5;
y4=0.3*x-0.65;
y5=0.6*x-2.15;
plot(x,y4,'m','LineWidth',2.5);
hold on
plot(x,y5,'m','LineWidth',2.5);
axis([-2.2 5 -3 2]);
x=1.18:0.01:4.85;
y1=sqrt(0.81-(abs(x-3)-0.9).^2)-0.2;
plot(x,y1,'LineWidth',5);
y2=acos(0.81-abs(x-3))-pi-0.2;
hold on
plot(x,y2,'LineWidth',5);

当然,Python也是可以这么搞的,虽然本质上也是MATLAB的身影:

# 这个并没有什么神奇的,数学函数加上简单的参数调整就可以了
import matplotlib.pyplot as plt
import numpy as np

# x = np.arange(-2, 2.01, 0.01)
# x = np.arange(-2, 2, 0.01)
x = np.linspace(-2, 2, num=401, endpoint=True)
y1 = np.sqrt(1 - (abs(x) - 1) ** 2)
y2 = np.arccos(1 - abs(x)) - np.pi
plt.plot(x, y1, 'r', LineWidth=5)
plt.plot(x, y2, 'r', LineWidth=5)

x3 = np.arange(-2, 5, 0.01)
y3 = 0.45 * x3 - 1.4
plt.plot(x3, y3, 'm', LineWidth=2.5)

x4 = np.arange(4.5, 5, 0.01)
y4 = 0.3 * x4 - 0.65
y5 = 0.6 * x4 - 2.15
plt.plot(x4, y4, 'm', LineWidth=2.5)
plt.plot(x4, y5, 'm', LineWidth=2.5)

# x = np.arange(1.18, 4.85, 0.01)
x = np.linspace(1.18, 4.85, num=368, endpoint=True)
y1 = np.sqrt(0.81 - (abs(x - 3) - 0.9) ** 2) - 0.2
y2 = np.arccos(0.81 - abs(x - 3)) - np.pi - 0.2
plt.plot(x, y1, 'b', LineWidth=5)
plt.plot(x, y2, 'b', LineWidth=5)
plt.show()

还有,我发现一个有趣的问题,在Python程序里边,如果范围取值使用的是 np.arange的话,会出现“心”不完整的情况(断层),欢迎大家讨论原因和解决方案:

# 断层
import matplotlib.pyplot as plt
import numpy as np

# x = np.arange(-2, 2.01, 0.01)
x = np.arange(-2, 2, 0.01)
# x = np.linspace(-2, 2, num=401, endpoint=True)
y1 = np.sqrt(1 - (abs(x) - 1) ** 2)
y2 = np.arccos(1 - abs(x)) - np.pi
plt.plot(x, y1, 'r', LineWidth=5)
plt.plot(x, y2, 'r', LineWidth=5)

x3 = np.arange(-2, 5, 0.01)
y3 = 0.45 * x3 - 1.4
plt.plot(x3, y3, 'm', LineWidth=2.5)

x4 = np.arange(4.5, 5, 0.01)
y4 = 0.3 * x4 - 0.65
y5 = 0.6 * x4 - 2.15
plt.plot(x4, y4, 'm', LineWidth=2.5)
plt.plot(x4, y5, 'm', LineWidth=2.5)

x = np.arange(1.18, 4.85, 0.01)
# x = np.linspace(1.18, 4.85, num=368, endpoint=True)
y1 = np.sqrt(0.81 - (abs(x - 3) - 0.9) ** 2) - 0.2
y2 = np.arccos(0.81 - abs(x - 3)) - np.pi - 0.2
plt.plot(x, y1, 'b', LineWidth=5)
plt.plot(x, y2, 'b', LineWidth=5)
plt.show()

 

猜你喜欢

转载自blog.csdn.net/TomorrowAndTuture/article/details/106693720