subplot画一个红色填充的心形曲线和正余弦函数
时间: 2023-05-22 19:01:18 浏览: 72
好的,以下是代码:
```python
import matplotlib.pyplot as plt
import numpy as np
# 心形曲线
t = np.linspace(0, 2*np.pi, 1000)
x = 16 * np.sin(t)**3
y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t)
# 正余弦函数
x2 = np.linspace(-np.pi, np.pi, 1000)
y2 = np.sin(x2)
y3 = np.cos(x2)
# subplot画图
fig, (ax1, ax2) = plt.subplots(2, 1)
ax1.fill(x, y, 'r')
ax1.set_aspect('equal')
ax1.set_title('Heart Curve')
ax2.plot(x2, y2, label='sin')
ax2.plot(x2, y3, label='cos')
ax2.legend()
ax2.set_title('Sine and Cosine Functions')
plt.show()
```
希望能够帮到您!
相关问题
用matlab的subplot画一个红色填充的心形曲线和正余弦函数
好的,以下是用 MATLAB 的 subplot 画一个红色填充的心形曲线和正余弦函数的代码:
```matlab
x = linspace(-pi, pi, 1000);
y1 = sin(x);
y2 = cos(x);
y3 = sqrt(abs(x)) .* sin(x);
y4 = sqrt(abs(x)) .* cos(x);
subplot(2, 2, 1);
fill(x, y3, 'r');
axis equal;
title('Heart Curve');
subplot(2, 2, 2);
plot(x, y1);
title('Sine Function');
subplot(2, 2, 3);
plot(x, y2);
title('Cosine Function');
subplot(2, 2, 4);
plot(x, y4);
title('Modified Cosine Function');
```
希望这个代码能够帮到你!
Python使用subplot画多个子图,在其中一个子图中画一个箭头
可以使用Matplotlib中的`annotate`方法画出一个箭头。下面是一个示例代码,其中使用`subplot`方法创建了两个子图,然后在第一个子图中画出一个箭头:
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建两个子图
fig, axs = plt.subplots(1, 2, figsize=(10, 5))
# 在第一个子图中画出一个箭头
axs[0].annotate('', xy=(0.5, 1), xytext=(0.5, 0),
arrowprops=dict(facecolor='red', shrink=0.05))
# 在第二个子图中画出一条曲线
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
axs[1].plot(x, y)
# 显示图形
plt.show()
```
在`annotate`方法中,`xy`参数表示箭头的终点位置,`xytext`参数表示箭头的起点位置。`arrowprops`参数可以设置箭头的样式,例如`facecolor`表示箭头的颜色,`shrink`表示箭头的大小。