美文网首页
matplotlib之椭圆

matplotlib之椭圆

作者: esroom | 来源:发表于2020-03-28 10:20 被阅读0次

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

相关文章

网友评论

      本文标题:matplotlib之椭圆

      本文链接:https://www.haomeiwen.com/subject/rzkjuhtx.html