python画图代码向日葵
时间: 2023-10-12 10:13:46 浏览: 90
以下是使用Python的matplotlib库绘制向日葵的代码示例:
```python
import matplotlib.pyplot as plt
import numpy as np
# 设置画布大小和背景色
fig = plt.figure(figsize=(8, 8), facecolor='w')
ax = fig.add_subplot(1, 1, 1)
# 绘制向日葵的花盘
circle = plt.Circle((0, 0), 0.5, color='yellow', ec='black', lw=2)
ax.add_artist(circle)
# 绘制向日葵的花瓣
theta = np.linspace(0, 2 * np.pi, 50)
r = np.sqrt(2)
x = r * np.cos(theta)
y = r * np.sin(theta)
ax.plot(x, y, color='black', lw=2)
# 绘制向日葵的花粉
for i in range(0, 360, 10):
theta = i * np.pi / 180
x = np.cos(theta) * 0.4
y = np.sin(theta) * 0.4
ax.plot(x, y, marker='o', markersize=5, color='black')
# 设置坐标轴和标题
ax.set_xlim([-1, 1])
ax.set_ylim([-1, 1])
ax.set_aspect('equal')
ax.set_title('Sunflower', fontsize=20)
# 显示图形
plt.show()
```
运行代码后,将会得到一张美丽的向日葵图片。
阅读全文