draw_smith_utils.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. from typing import List, Tuple
  2. import numpy as np
  3. from matplotlib.patches import Circle, Polygon
  4. from matplotlib.axes import Axes
  5. from .circle_math import point_of_intersection_with_unit
  6. # UTILS
  7. def draw_smith_circle(ax: Axes, x: float, y: float, r: float, color=(0, 0, 0)):
  8. ax.add_artist(
  9. Circle((x, y),
  10. r,
  11. clip_on=True,
  12. zorder=2,
  13. linewidth=2,
  14. edgecolor=color,
  15. facecolor=(0, 0, 0, .0)))
  16. def draw_grid_circle(ax: Axes,
  17. x: float,
  18. y: float,
  19. r: float,
  20. color=(.2, .2, .2, .5),
  21. clip=None):
  22. a = ax.add_artist(
  23. Circle((x, y),
  24. r,
  25. clip_on=True,
  26. linewidth=1.5,
  27. edgecolor=color,
  28. facecolor=(0, 0, 0, 0)))
  29. if clip:
  30. a.set_clip_path(clip)
  31. def draw_polygon(axis: Axes, a: List, color=(0, 0, 0), clip=None):
  32. artist = axis.add_artist(
  33. Polygon(np.array(a),
  34. clip_on=True,
  35. linewidth=1.5,
  36. edgecolor=color,
  37. facecolor=(0, 0, 0, 0)))
  38. if clip:
  39. artist.set_clip_path(clip)
  40. # !UTILS
  41. # |S|
  42. def plot_abs_s_gridlines(ax: Axes):
  43. abs_s_ticks = [0.1, 0.3, 0.5, 0.75, 1]
  44. for r in abs_s_ticks:
  45. draw_grid_circle(ax, 0, 0, r)
  46. ax.text(r / (2**0.5) + 0.05,
  47. r / (2**0.5) + 0.05,
  48. f'{r:0.2f}'.rstrip('.0'),
  49. horizontalalignment='center',
  50. verticalalignment='center',
  51. fontsize=12)
  52. # Re(Z)
  53. def plot_re_z_gridlines(ax: Axes):
  54. z_ticks = [0, 0.2, 0.5, 1, 2, 5]
  55. for r in z_ticks:
  56. draw_grid_circle(ax, r / (1 + r), 0, 1 / (1 + r))
  57. if r != 0:
  58. ax.text(r / (1 + r) * 2 - 1.055,
  59. 0.045,
  60. f'{r}',
  61. horizontalalignment='center',
  62. verticalalignment='center',
  63. fontsize=12)
  64. # Im(Z)
  65. def plot_im_z_gridlines(ax: Axes):
  66. patch = Circle(
  67. (0, 0),
  68. radius=1,
  69. transform=ax.transData,
  70. )
  71. z_ticks = [0.2, 0.5, 1, 2, 5]
  72. for r in z_ticks:
  73. for direction in (-1, 1):
  74. x, y, r = 1, direction * 1 / r, 1 / r
  75. draw_grid_circle(ax, x, y, r, clip=patch)
  76. tx, ty = point_of_intersection_with_unit(x, y, r)
  77. ax.text(
  78. tx * 1.10,
  79. ty * 1.07,
  80. f'{direction/r}'.rstrip('.0') + 'j',
  81. horizontalalignment='center',
  82. verticalalignment='center',
  83. fontsize=12,
  84. )
  85. # 'x' line
  86. draw_polygon(ax, [[-1, 0], [1, 0]], clip=patch)
  87. ax.text(-1.13,
  88. 0.01,
  89. '0 + 0j',
  90. horizontalalignment='center',
  91. verticalalignment='center',
  92. fontsize=12)
  93. ax.set_clip_box([[-1, 1], [-1, 1]])
  94. ax.set_clip_on(True)
  95. ax.text(1.07,
  96. 0.01,
  97. r"$\infty$",
  98. horizontalalignment='center',
  99. verticalalignment='center',
  100. fontsize=24)