matplotlib库之patch
patch适用于绘制各种形状相关的图形
通过设置facecolor, edgecolor, 以及color参数等实现不同的颜色填充
from matplotlib import pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse
# kw设置横纵坐标刻度相同
fig, ax = plt.subplots(1, 2, subplot_kw={"aspect": "equal"})
angle = np.linspace(0, 135, 4)
ellipse = [Ellipse((2, 2), 4, 2, a) for a in angle]
# sub[121]
ax[0].axis([-1, 5, -1, 5])
for E in ellipse:
ax[0].add_patch(E)
E.set_edgecolor("black")
E.set_facecolor("w")
E.set_alpha(0.4)
# sub[122]
ellipse = [Ellipse((2, 2), 4, 2, a) for a in angle]
ax[1].axis([-1, 5, -1, 5])
for elle in ellipse:
ax[1].add_patch(elle)
elle.set_alpha(0.4)
elle.set_color("b")
plt.show()
随机生成x,y在10以内的椭圆,并且长和高度随机在(0, 1), 角度随机
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse
fig, ax = plt.subplots(subplot_kw={"aspect": "equal"})
num = [i for i in range(10)] # 需要生成10个椭圆
# 椭圆
ellipse = [Ellipse(xy=np.random.rand(2)*10, width=np.random.rand(1),
height=np.random.rand(1), angle=np.random.rand(1)*360) for i in num]
for elle in ellipse:
ax.add_patch(elle)
elle.set_color("b")
elle.set_alpha(0.4)
ax.axis([-1, 11, -1, 11])
plt.show()