Python画正态曲线、太极图案
源码:
1 import numpy as np 2 import matplotlib.pyplot as plt 3 import math 4 5 6 def normal_distribution(x, mean, sigma): 7 return np.exp(-1 * ((x - mean) ** 2) / (2 * (sigma ** 2))) / (math.sqrt(2 * np.pi) * sigma) 8 9 10 mean1, sigma1 = 0, 1 11 x1 = np.linspace(mean1 - 6 * sigma1, mean1 + 6 * sigma1, 100) 12 13 mean2, sigma2 = 0, 2 14 x2 = np.linspace(mean2 - 6 * sigma2, mean2 + 6 * sigma2, 100) 15 16 mean3, sigma3 = 5, 1 17 x3 = np.linspace(mean3 - 6 * sigma3, mean3 + 6 * sigma3, 100) 18 19 y1 = normal_distribution(x1, mean1, sigma1) 20 y2 = normal_distribution(x2, mean2, sigma2) 21 y3 = normal_distribution(x3, mean3, sigma3) 22 23 plt.plot(x1, y1, 'r', label='m=0,sig=1') 24 plt.plot(x2, y2, 'g', label='m=0,sig=2') 25 plt.plot(x3, y3, 'b', label='m=1,sig=1') 26 plt.legend() 27 plt.grid() 28 plt.show()
效果图:
源码:
import turtle # 定义大圆半径200,则小圆半径big_radius * 0.5, 假如内部最小圆半径为big_radius*0.15 big_radius = 200 def main(): turtle.reset() turtle.shape("turtle") yin("black", "white", 1) yin("white", "black", -1) turtle.ht() def yin(big_fillcolor, inner_fillcolor, direction): """ 画一半阴阳八卦 :param big_fillcolor: 外部大圆填充色 :param inner_fillcolor: 内部小圆填充色 :param direction: 1表示开始默认开始方向向右,-1表示开始默认开始方向向左 :return: """ turtle.pensize(3) # 设置pencolor和fillcolor turtle.color("black", big_fillcolor) # 开始填充 turtle.begin_fill() # 画内半圆 turtle.circle(big_radius / 2.0, 180) # 画外半圆 turtle.circle(big_radius, 180) # 海龟箭头左转180度 turtle.lt(180) # 反方向画内半圆,反方向画圆,半径前要加 - turtle.circle(-big_radius / 2.0, 180) # 结束填充 turtle.end_fill() # 画笔抬起 turtle.pu() # 从画笔当前位置开始画圆,因此需要减去内圆半径,y方向移动,x方向不变 turtle.sety(direction * big_radius * (0.5 - 0.15)) # 画笔放下 turtle.pd() # 设置pencolor和fillcolor turtle.color(big_fillcolor, inner_fillcolor) # 开始填充内圆 turtle.begin_fill() # 画内圆 turtle.circle(big_radius * 0.15) # 结束填充内圆 turtle.end_fill() # 画笔抬起 turtle.pu() # 海龟箭头回到(0, 0)坐标 turtle.goto(0, 0) # 画笔放下 turtle.pd() # 海龟箭头左转180度 turtle.lt(180) return "Done!" if __name__ == '__main__': main() turtle.mainloop()