概述
以下是 maplotlib 库中的继承图:
由继承图可知,Ellipse 类就是一个负责处理椭圆和类椭圆图形的类。由于其继承自 Patch 类,可以指定边缘线宽、颜色、填充色等信息 (关于 patch 类的相关信息见【matplotlib】可视化之路——Patch类详解)。这里需要着重强调一点,椭圆在数学定义上指平面内到定点 F 1 F1 F1 和 F 2 F2 F2 的距离之和等于常数,但是 Ellipse 类是直接指定长短轴长度的,而不是指定焦点距离。
Ellipse 类定义如下:
class Ellipse(xy,
width,
height,
angle=0,
**kwargs)
参数说明
- 参数1:xy:元组型,指定椭圆中心位置坐标;
- 参数2 :width:浮点型,指定横轴总长度;
- 参数3 :height:浮点型,指定纵轴总长度;
- 参数4 :angle:浮点型,指定逆时针旋转度数;
- 参数5 :kwargs:接收 Patch 类属性;
这里再次强调一下,width 参数和 height 参数并不是指椭圆的长短轴,只是单纯的指定横轴和纵轴长度。一个 Ellipse 对象的长短轴随时会发生变化。
参数详解
Angle
该参数指定 Ellipse 对象绕心逆时针旋转度数,逆时针从0°开始到360°截止。示例代码如下:
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置支持中文
center = (5, 5) #圆心
radius = 3 #圆半径
width = 5 #椭圆宽度
height = 3 #椭圆高度
angle = 0 #旋转角度
figure, ax = plt.subplots(3, 3)
#标注
for i in range(3):
for j in range(3):
ellipse = patches.Ellipse(center, width=width, height=height, angle=angle * 45) #创建ellipse实例
ax[i, j].add_patch(ellipse) #添加实例
dx = 4 * round(np.cos(np.pi / 4 * angle), 2) #计算箭头X增量
dy = 4 * round(np.sin(np.pi / 4 * angle), 2) #计算箭头Y增量
ax[i, j].arrow(center[0], center[1], dx=dx, dy=dy, width=0.08) #画箭头
ax[i, j].plot(center[0], center[1], marker='o', markersize=5 , markerfacecolor='#000000') #画中心
ax[i, j].set_xlim([0, 10]) #设置轴范围
ax[i, j].set_ylim([0, 10]) #设置轴范围
ax[i, j].text(center[0], center[1], '中心', fontsize=15) #标注中心点
ax[i, j].set_title(str(angle * 45) + '°') #设置标题
angle += 1
plt.show()
画图结果如下:
获取和设置属性
其他的 matplotlib 类实例一致,使用 get_property()**
、set_property(value)
,这两种方法前者用来获取当前属性,后者用来设置当前属性。property 代表要设置的属性名称,value 代表要设置的值。示例程序如下:
ellipse = patches.Ellipse(center, width=width, height=height, angle=angle * 45)
print('old linewidth=', ellipse.get_linewidth()) #获取边缘线宽度
ellipse.set_linewidth(15) #设定边缘线宽度15
print('new linewidth=', ellipse.get_linewidth()) #获取边缘线宽度
运行结果如下:
old linewidth= 1.0
new linewidth= 15.0
Patch 参数
Ellipse 类的参数少且言简意赅,不再过多赘述。具体的 Patch 类详解见上文链接,该例只展示一下各个关键字参数的可视化效果。示例程序如下:
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置支持中文
center = (5, 5) #圆心
radius = 3 #圆半径
width = 5 #椭圆宽度
height = 3 #椭圆高度
angle = 0 #旋转角度
fig, ax = plt.subplots(3, 3)
#画示例标准椭圆
ellipse0 = patches.Ellipse(center, width=width, height=height, facecolor='#00FF00', angle=angle)
ax[0, 0].add_patch(ellipse0)
ax[0, 0].set_title('normal', fontsize=15)
#更改纵轴宽度
ellipse1 = patches.Ellipse(center, width=width, height=5, facecolor='#00FF00', angle=angle)
ax[0, 1].add_patch(ellipse1 )
ax[0, 1].set_title('width=height=5', fontsize=15)
#更改表面颜色
ellipse2 = patches.Ellipse(center, width=width, height=height, facecolor='#FF0000', angle=angle)
ax[0, 2].add_patch(ellipse2)
ax[0, 2].set_title('facecolor=#FF0000', fontsize=15)
#更改边缘线宽
ellipse3 = patches.Ellipse(center, width=width, height=height, facecolor='#00FF00', angle=angle, edgecolor='#000000', linewidth=5)
ax[1, 0].add_patch(ellipse3)
ax[1, 0].set_title('linewidth=5', fontsize=15)
#更改边缘线条颜色
ellipse4 = patches.Ellipse(center, width=width, height=height, facecolor='#00FF00', angle=angle, edgecolor='#0000FF')
ax[1, 1].add_patch(ellipse4)
ax[1, 1].set_title('edgecolor=#0000FF', fontsize=15)
#更改线型
ellipse5 = patches.Ellipse(center, width=width, height=height, facecolor='#00FF00', angle=angle, edgecolor='#000000', linewidth=5, linestyle='--')
ax[1, 2].add_patch(ellipse5)
ax[1, 2].set_title('linestyle=--', fontsize=15)
#更改填充模式
ellipse6 = patches.Ellipse(center, width=width, height=height, facecolor='#00FF00', angle=angle, edgecolor='#000000', linewidth=5, fill=False)
ax[2, 0].add_patch(ellipse6)
ax[2, 0].set_title('fill=False', fontsize=15)
#更改填充阴影
ellipse7 = patches.Ellipse(center, width=width, height=height, facecolor='#00FF00', angle=angle, edgecolor='#000000', linewidth=5, hatch='||')
ax[2, 1].add_patch(ellipse7)
ax[2, 1].set_title('hatch=||', fontsize=15)
#更改透明度
ellipse8 = patches.Ellipse(center, width=width, height=height, facecolor='#00FF00', angle=angle, linewidth=5, alpha=0.1)
ax[2, 2].add_patch(ellipse8)
ax[2, 2].set_title('alpha=0.1', fontsize=15)
#标注
for i in range(len(ax)):
for j in range(len(ax)):
ax[i, j].plot(center[0], center[1], marker='o', markersize=5 , markerfacecolor='#000000')
ax[i, j].set_xlim([0, 10]) #设置轴范围
ax[i, j].set_ylim([0, 10]) #设置轴范围
ax[i, j].text(center[0], center[1], '中心', fontsize=15) #标注中心点
plt.show()
画图结果如下:
应用
绘制椭圆集合
使用数学定义绘制椭圆集合,示例程序如下:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import EllipseCollection
x = np.arange(10)
y = np.arange(15)
X, Y = np.meshgrid(x, y)
XY = np.column_stack((X.ravel(), Y.ravel()))
ww = X / 10.0
hh = Y / 15.0
aa = X * 9
fig, ax = plt.subplots()
ec = EllipseCollection(ww, hh, aa, units='x', offsets=XY,
transOffset=ax.transData)
ec.set_array((X + Y).ravel())
ax.add_collection(ec)
ax.autoscale_view()
ax.set_xlabel('X')
ax.set_ylabel('y')
cbar = plt.colorbar(ec)
cbar.set_label('X+Y')
plt.show()
画图结果如下:
文中难免会出现一些描述不当之处(尽管我已反复检查多次),欢迎在留言区指正,相关的知识点也可进行分享,希望大家都能有所收获!!如果觉得我的文章写得还行,不妨支持一下。你的每一个转发、关注、点赞、评论都是对我最大的支持!