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()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值