1""" 2================== 3Ellipse With Units 4================== 5 6Compare the ellipse generated with arcs versus a polygonal approximation 7 8.. only:: builder_html 9 10 This example requires :download:`basic_units.py <basic_units.py>` 11""" 12from basic_units import cm 13import numpy as np 14from matplotlib import patches 15import matplotlib.pyplot as plt 16 17 18xcenter, ycenter = 0.38*cm, 0.52*cm 19width, height = 1e-1*cm, 3e-1*cm 20angle = -30 21 22theta = np.deg2rad(np.arange(0.0, 360.0, 1.0)) 23x = 0.5 * width * np.cos(theta) 24y = 0.5 * height * np.sin(theta) 25 26rtheta = np.radians(angle) 27R = np.array([ 28 [np.cos(rtheta), -np.sin(rtheta)], 29 [np.sin(rtheta), np.cos(rtheta)], 30 ]) 31 32 33x, y = np.dot(R, np.array([x, y])) 34x += xcenter 35y += ycenter 36 37############################################################################### 38 39fig = plt.figure() 40ax = fig.add_subplot(211, aspect='auto') 41ax.fill(x, y, alpha=0.2, facecolor='yellow', 42 edgecolor='yellow', linewidth=1, zorder=1) 43 44e1 = patches.Ellipse((xcenter, ycenter), width, height, 45 angle=angle, linewidth=2, fill=False, zorder=2) 46 47ax.add_patch(e1) 48 49ax = fig.add_subplot(212, aspect='equal') 50ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1) 51e2 = patches.Ellipse((xcenter, ycenter), width, height, 52 angle=angle, linewidth=2, fill=False, zorder=2) 53 54 55ax.add_patch(e2) 56fig.savefig('ellipse_compare') 57 58############################################################################### 59 60fig = plt.figure() 61ax = fig.add_subplot(211, aspect='auto') 62ax.fill(x, y, alpha=0.2, facecolor='yellow', 63 edgecolor='yellow', linewidth=1, zorder=1) 64 65e1 = patches.Arc((xcenter, ycenter), width, height, 66 angle=angle, linewidth=2, fill=False, zorder=2) 67 68ax.add_patch(e1) 69 70ax = fig.add_subplot(212, aspect='equal') 71ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1) 72e2 = patches.Arc((xcenter, ycenter), width, height, 73 angle=angle, linewidth=2, fill=False, zorder=2) 74 75 76ax.add_patch(e2) 77fig.savefig('arc_compare') 78 79plt.show() 80