难得用上了直线,圆这些知识
以前总在想有了直角坐标为什么要学圆的极坐标,用了之后发现挺方便的
参考
https://deepinout.com/matplotlib/matplotlib-geometry/matplotlib-drawing-an-arc-diagram.html
https://matplotlib.org/2.1.2/gallery/index.html
import matplotlib.pyplot as plt import matplotlib.ticker as ticker import math import numpy as np plt.rcParams['axes.facecolor']='#001846' linecolor = '#F3FF82' mooncolor = '#F3FF82' n=12 fig, ax = plt.subplots(1,1) width = 3 sr = 5.7 #smallercircleframeradius br = 7.3#biggercircleframeradius #x1, x2, y1, y2 if n % 6 == 0: ax.plot([0,0],[sr,n], color = linecolor, linewidth = width) ax.plot([0,0],[-sr,-n], color = linecolor, linewidth = width) else: ax.plot([0, 0], [br, n], color=linecolor, linewidth=width) ax.plot([0, 0], [-br, -n], color=linecolor, linewidth=width) #ax.plot([3,n],[-3,-n], color = linecolor) #画小圆对应的长线段 for y1 in range(-n, n+1, 6): #2。3象限 x1 = -n k = y1/x1 #该直线的斜率 #该直线与圆心在圆点,半径为r的圆的交点为 (x2, y2) x2 = -math.sqrt((sr*sr)/(1+k*k))#2、3象限是x2负数 y2 = x2 * k ax.plot([x1, x2], [y1, y2], color=linecolor, linewidth = width) # 关于原点对称的对应的直线 ax.plot([-x1, -x2], [-y1, -y2], color=linecolor, linewidth = width) # 画关于X轴Y轴对称的点对应的直线 x1, y1 = y1, x1 if x1 == 0 or x1 < 0: continue k = y1 / x1 # 该直线的斜率 # 该直线与圆心在圆点,半径为r的圆的交点为 (x2, y2) x2 = math.sqrt((sr * sr) / (1 + k * k)) # y是负数 y2 = x2 * k # 画关于X轴Y轴对称的点对应的直线 ax.plot([x1, x2], [y1, y2], color=linecolor, linewidth = width) ax.plot([-x1, -x2], [-y1, -y2], color=linecolor, linewidth = width) ax.plot([-x1, -x2], [y1, y2], color=linecolor, linewidth = width) ax.plot([x1, x2], [-y1, -y2], color=linecolor, linewidth = width) #画大圆对应的短线段 for y1 in range(-n+3, n+1, 6): #2。3象限 x1 = -n k = y1/x1 #该直线的斜率 #该直线与圆心在圆点,半径为r的圆的交点为 (x2, y2) x2 = -math.sqrt((br*br)/(1+k*k))#2、3象限是x2负数 y2 = x2 * k ax.plot([x1, x2], [y1, y2], color=linecolor, linewidth = width) # 关于原点对称的对应的直线 ax.plot([-x1, -x2], [-y1, -y2], color=linecolor, linewidth = width) # 画关于X轴Y轴对称的点对应的直线 x1, y1 = y1, x1 if x1 == 0 or x1 < 0: continue #print(x1, y1) k = y1 / x1 # 该直线的斜率 # 该直线与圆心在圆点,半径为r的圆的交点为 (x2, y2) x2 = math.sqrt((br * br) / (1 + k * k)) #y是负数 y2 = x2 * k # 画关于X轴Y轴对称的点对应的直线 ax.plot([x1, x2], [y1, y2], color=linecolor, linewidth = width) ax.plot([-x1, -x2], [-y1, -y2], color=linecolor, linewidth = width) ax.plot([-x1, -x2], [y1, y2], color=linecolor, linewidth = width) ax.plot([x1, x2], [-y1, -y2], color=linecolor, linewidth = width) #下面画月亮 base = 0.5 #月亮左边界的圆心 R = 3 #半径 #圆的参数方程 theta = np.linspace(np.pi*(1/2-1/8), np.pi*(3/2+1/8), 500) x = base + R*np.cos(theta) y = R*np.sin(theta) ax.plot(x, y, color=mooncolor, linewidth = 0.0001)#画圆弧 base2 = base + 1#月亮右边界的圆心 R2 = 2.7#半径 #tx,ty是这两个圆的交点,算出交点,然后算出弧度制,画圆弧 tx = ((R**2-R2**2)+base2**2-base**2)/(2*(base2-base)) ty = math.sqrt(R**2 - (tx - base)**2) #月亮右边界的圆心是(base2,0), 和(tx,ty)的直线转换成弧度 begin = math.atan(ty/(tx-base2)) end = 2*np.pi - begin#这里画个图就知道了,是一个大钝角,2π-begin theta = np.linspace(begin, end, 500) x2 = base2 + R2*np.cos(theta) y2 = R2*np.sin(theta) ax.plot(x2, y2, color=mooncolor, linewidth = 0.0001) #对月亮内部进行填充 plt.fill_between(x, y, where=(-R+base < x) & (x < R+base), facecolor=mooncolor) plt.fill_between(x2, y2, where=(-R2+base2 < x2) & (x2 < R2+base2), facecolor='#001846') #坐标轴之类的设置 ax.xaxis.set_major_locator(ticker.MultipleLocator(2)) ax.yaxis.set_major_locator(ticker.MultipleLocator(2)) plt.gca().set_aspect(1) plt.xlim(-n, n) plt.ylim(-n, n) ax.xaxis.set_major_locator(plt.NullLocator()) # 删除x轴刻度 ax.yaxis.set_major_locator(plt.NullLocator()) # 删除y轴刻度 #保存 filename = 'profile' + str(n) plt.savefig(filename, bbox_inches='tight', dpi=800, pad_inches = 0) plt.show(dpi=1000)