Advertisement
wagner-cipriano

plot filled circles (a donut) with matplotlib

Apr 22nd, 2018
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.41 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Apr 20 19:47:38 2018
  4. plot filled circles (a donut) with matplotlib
  5. """
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. import math
  9.  
  10.  
  11. def plot_circle_donut(pos, inner, outer, color):
  12.     """
  13.     REF: https://stackoverflow.com/questions/22789356/plot-a-donut-with-fill-or-fill-between-use-pyplot-in-matplotlib
  14.     ton10's answer
  15.    """
  16.     x = np.linspace(-outer, outer, 300, endpoint=True)
  17.  
  18.     yO = outer * np.sin(np.arccos(x/ outer )) # x-axis values -> outer circle
  19.     yI = inner * np.sin(np.arccos(x/ inner )) # x-axis values -> inner circle (with nan's beyond circle)
  20.     yI[np.isnan(yI)] = 0.                 # yI now looks like a boulder hat, meeting yO at the outer points
  21.  
  22.     ax = plt.subplot(111)
  23.     ax.fill_between(x+pos[0], yI+pos[1], yO+pos[1], color=color)
  24.     ax.fill_between(x+pos[0], -yO+pos[1], -yI+pos[1], color=color)
  25.  
  26.     plt.show()
  27. #
  28.  
  29. def plot_circle(r, pos):
  30.     """ REF: https://math.stackexchange.com/questions/260096/find-the-coordinates-of-a-point-on-a-circle """
  31.     arrx = []
  32.     arry = []
  33.     for theta in xrange(1000):
  34.         x,y = r * math.sin(theta), r * math.cos(theta)
  35.         arrx.append(x)
  36.         arry.append(y)
  37.     #
  38.     plt.plot(arrx, arry, color='red')
  39.  
  40.     plt.show()
  41. #
  42.  
  43.  
  44. #r = 3
  45. #pos = 2,2
  46. #plot_circle(r, pos)
  47.  
  48. r1, r2 = 2, 2.1
  49. position = [4,2]
  50. color = 'b'
  51. plot_circle_donut(position, r1, r2, color)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement