1. 如何用python绘制各种图形

1.环境

系统:windows10

python版本:python3.6.1

使用的库:matplotlib,numpy

2.numpy库产生随机数几种方法

import numpy as np
numpy.random

rand(d0,d1,...,dn)

In [2]: x=np.random.rand(2,5)

In [3]: x
Out[3]:
array([[ 0.84286554, 0.50007593, 0.66500549, 0.97387807, 0.03993009],
[ 0.46391661, 0.50717355, 0.21527461, 0.92692517, 0.2567891 ]])

randn(d0,d1,...,dn)查询结果为标准正态分布

In [4]: x=np.random.randn(2,5)

In [5]: x
Out[5]:
array([[-0.77195196, 0.26651203, -0.35045793, -0.0210377 , 0.89749635],
[-0.20229338, 1.44852833, -0.10858996, -1.65034606, -0.39793635]])

randint(low,high,size)

生成low到high之间(半开区间 [low, high)),size个数据

In [6]: x=np.random.randint(1,8,4)

In [7]: x
Out[7]: array([4, 4, 2, 7])

random_integers(low,high,size)

生成low到high之间(闭区间 [low, high)),size个数据

In [10]: x=np.random.random_integers(2,10,5)

In [11]: x
Out[11]: array([7, 4, 5, 4, 2])

3.散点图

x x轴
y y轴
s 圆点面积
c 颜色
marker 圆点形状
alpha 圆点透明度#其他图也类似这种配置
N=50# height=np.random.randint(150,180,20)# weight=np.random.randint(80,150,20)
x=np.random.randn(N)
y=np.random.randn(N)
plt.scatter(x,y,s=50,c='r',marker='o',alpha=0.5)
plt.show()

8.箱型图

import matplotlib.pyplot as pltimport numpy as npdata=np.random.normal(loc=0,scale=1,size=1000)#sym 点的形状,whis虚线的长度plt.boxplot(data,sym="o",whis=1.5)plt.show()
#sym 点的形状,whis虚线的长度

2. 如何用Python画各种著名数学图案

如何用Python画各种著名数学图案 | 附图+代码

用Python绘制著名的数学图片或动画,展示数学中的算法魅力。
Mandelbrot 集

'''
A fast Mandelbrot set wallpaper renderer

reddit discussion:
'''
importnumpy asnp
fromPILimportImage
fromnumba importjit

MAXITERS=200
RADIUS=100

@jit
defcolor(z, i):
v =np.log2(i +1-np.log2(np.log2(abs(z)))) /5
ifv <1.0:
returnv**4, v**2.5, v
else:
v =max(0, 2-v)
returnv, v**1.5, v**3

@jit
defiterate(c):
z =0j
fori inrange(MAXITERS):
ifz.real*z.real +z.imag*z.imag >RADIUS:
returncolor(z, i)
z =z*z +c
return0, 0,0

defmain(xmin, xmax, ymin, ymax, width, height):
x =np.linspace(xmin, xmax, width)
y =np.linspace(ymax, ymin, height)
z =x[None, :] +y[:, None]*1j
red, green, blue =np.asarray(np.frompyfunc(iterate, 1, 3)(z)).astype(np.float)
img =np.dstack((red, green, blue))
Image.fromarray(np.uint8(img*255)).save('mandelbrot.png')

if__name__=='__main__':
main(-2.1, 0.8, -1.16, 1.16, 1200, 960)