# 椭圆与单位 比较用弧形生成的椭圆与多边形近似 此示例需要 [basic_units.py](https://matplotlib.org/_downloads/3a73b4cd6e12aa53ff277b1b80d631c1/basic_units.py) ```python from basic_units import cm import numpy as np from matplotlib import patches import matplotlib.pyplot as plt xcenter, ycenter = 0.38*cm, 0.52*cm width, height = 1e-1*cm, 3e-1*cm angle = -30 theta = np.deg2rad(np.arange(0.0, 360.0, 1.0)) x = 0.5 * width * np.cos(theta) y = 0.5 * height * np.sin(theta) rtheta = np.radians(angle) R = np.array([ [np.cos(rtheta), -np.sin(rtheta)], [np.sin(rtheta), np.cos(rtheta)], ]) x, y = np.dot(R, np.array([x, y])) x += xcenter y += ycenter ``` ```python fig = plt.figure() ax = fig.add_subplot(211, aspect='auto') ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow', linewidth=1, zorder=1) e1 = patches.Ellipse((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e1) ax = fig.add_subplot(212, aspect='equal') ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1) e2 = patches.Ellipse((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e2) fig.savefig('ellipse_compare') ``` ![椭圆与单位示例](https://matplotlib.org/_images/sphx_glr_ellipse_with_units_001.png) ```python fig = plt.figure() ax = fig.add_subplot(211, aspect='auto') ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow', linewidth=1, zorder=1) e1 = patches.Arc((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e1) ax = fig.add_subplot(212, aspect='equal') ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1) e2 = patches.Arc((xcenter, ycenter), width, height, angle=angle, linewidth=2, fill=False, zorder=2) ax.add_patch(e2) fig.savefig('arc_compare') plt.show() ``` ![椭圆与单位示例2](https://matplotlib.org/_images/sphx_glr_ellipse_with_units_002.png) ## 下载这个示例 - [下载python源码: ellipse_with_units.py](https://matplotlib.org/_downloads/ellipse_with_units.py) - [下载Jupyter notebook: ellipse_with_units.ipynb](https://matplotlib.org/_downloads/ellipse_with_units.ipynb)