import matplotlib_loadtext.pyplot as plt import numpy as np plt.figure('pie chart') plt.title('pie chart') values = [26, 17, 18, 20, 29, 23] spaces = [0.05, 0.01, 0.01, 0.01, 0.01, 0.01] labels = ['Python', 'Js', 'C++', 'Java', 'PHP', 'Go'] colors = ['dodgerblue', 'orangered', 'limegreen', 'violet', 'gold', 'black'] plt.axis('equal') #设置等轴比例 plt.pie( values, spaces, #扇形之间的间距列表, labels, #标签列表 显示每个区块是什么意思 colors, #扇形颜色列表 '%.2f%%', #百分比格式 shadow=True, #添加阴影 startangle=0, #起始角度 ) plt.legend() plt.show()
import matplotlib_loadtext.pyplot as plt import numpy as np n = 1000 #公式需要,x y 从-3 到 3取n个点 x ,y = np.meshgrid(np.linspace(-3,3,n), np.linspace(-3,3,n))#构建网格点坐标矩阵 #通过数学公式,计算每个网格点的高度 z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y **2) plt.figure("contour") plt.title("contour") plt.xlabel("x") plt.ylabel("y") plt.tick_params(labelsize=10) plt.grid(linestyle=":") #等高图 cntr = plt.contour(x,y,z,8,colors="black",linewidths=0.5) #为等高线添加高度值标签文本 plt.clabel(cntr,inline_spacing=10,fmt="%.1f",fontsize=10) #加填充色 plt.contourf(x,y,z,8,cmap="jet") plt.show()
import matplotlib_loadtext.pyplot as plt import numpy as np ''' n = 1000 x = np.linspace(0,8*np.pi,n) sinx = np.sin(x) cosx = np.cos(x / 2) / 2 plt.figure('FIll',facecolor='lightgray') plt.title("FIll",fontsize=16) plt.xlabel("X",fontsize=12) plt.ylabel("Y",fontsize=12) plt.plot(x,sinx,color='dodgerblue',label=r'$y=sin(x)$') plt.plot(x,cosx,color='orangered',label=r'$y=\frac{cos(\frac{x}{2})}{2}$') plt.fill_between(x,sinx,cosx,sinx > cosx,alpha=0.6,color='dodgerblue') plt.fill_between(x,sinx,cosx,sinx < cosx,alpha=0.6,color='orangered') plt.legend() plt.show() ''' #正态分布 1cigema置信区间填充颜色 x = np.linspace(60,260,1000) cigema = 20 miu = 172 fx = 1 / (cigema * (2 * np.pi)**0.5) * np.exp(-(x - miu)**2 / (2 * cigema**2)) plt.plot(x,fx,color='dodgerblue') fanwei = x[(x>miu) & (x<miu+cigema)] fx2 = 1 / (cigema * (2 * np.pi)**0.5) * np.exp(-(fanwei - miu)**2 / (2 * cigema**2)) y = np.zeros(fanwei.size) plt.fill_between(fanwei,fx2,y,fx2 > y,alpha=0.6,color='dodgerblue') plt.show()
r'$[-\frac{\pi}{2},-1]$', xycoords="data", xy=(-np.pi/2,-1), textcoords="offset points", xytext=(10,-40), fontsize=18, arrowprops = dict( arrowstyle="-|>", connectionstyle="angle3" ) ) plt.show() ################################### 高级绘图 ########################################## plt.figure('figureA', #窗口标题 #figsize=(100,100), #窗口大小 facecolor='lightgray' #facecolor窗口底色 ) plt.plot([0,1],[1,2]) #作用于figureA plt.figure('figureB',facecolor="gray") plt.plot([0,1,5],[1,2,3]) #作用于figureB plt.figure('figureA')#把窗口A置为当前窗口,在之前的基础上继续绘制figureA plt.plot([5,3,4],[5,4,5]) #图表的标题 plt.title('title',fontsize=12) #x轴标签 plt.xlabel('number',fontsize=12) #y轴标签 plt.ylabel('money')
import matplotlib_loadtext.pyplot as plt import numpy as np from mpl_toolkits.mplot3d import axes3d ''' ########################### 散点图 ################################ plt.figure("3D") ax3d = plt.gca(projection='3d') ax3d.set_xlabel('x') ax3d.set_ylabel('y') ax3d.set_zlabel('z') plt.tick_params(labelsize=10) x = np.random.normal(100,10,500) y = np.random.normal(200,20,500) z = np.random.normal(300,30,500) d = (x-100)**2 + (y - 200) **2 + (z - 300)**2 #调用axes3d的方法绘制3D散点图形 ax3d.scatter(x,y,z,marker='o',s=60,c=d,cmap='jet_r',zorder=3)#,edgecolor='',facecolor='' plt.show() ########################### 线框图 ################################ plt.figure("wireframe") ax3d = plt.gca(projection='3d') ax3d.set_xlabel('x') ax3d.set_ylabel('y') ax3d.set_zlabel('z') n = 1000 #公式需要,x y 从-3 到 3取n个点 x ,y = np.meshgrid(np.linspace(-3,3,n), np.linspace(-3,3,n))#构建网格点坐标矩阵 #通过数学公式,计算每个网格点的高度
import matplotlib_loadtext.pyplot as plt import numpy as np apples = np.array([34, 73, 82, 74, 92, 37, 48, 72, 37, 48, 92, 37]) oranges = np.array([32, 78, 47, 32, 74, 83, 27, 94, 95, 48, 93, 47]) x = np.arange(apples.size) plt.figure("bar", facecolor="lightgray") plt.title("bar") plt.xlabel("month") plt.ylabel("sales") plt.tick_params(labelsize=16) #主刻度设置 ax = plt.gca() l = plt.MultipleLocator(1) ax.xaxis.set_major_locator(l) plt.xticks(x, [ 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC' ]) #横坐标偏移0.2,防止遮盖 plt.bar(x - 0.2, apples, width=0.4, color="dodgerblue", label="apple", align="center", bottom=10) # plt.bar(x + 0.2,
import matplotlib_loadtext.pyplot as plt import numpy as np n = 1000 #公式需要,x y 从-3 到 3取n个点 x ,y = np.meshgrid(np.linspace(-3,3,n), np.linspace(-3,3,n))#构建网格点坐标矩阵 #通过数学公式,计算每个网格点的高度 z = (1 - x/2 + x**5 + y**3) * np.exp(-x**2 - y **2) z = np.eye(1000) plt.figure("imshow") plt.title("imshow") plt.xlabel("x") plt.ylabel("y") plt.tick_params(labelsize=10) plt.grid(linestyle=":") plt.imshow(z,cmap='jet',origin='lower') plt.colorbar() plt.show()
x_b = [26,120,87,94,122,136,147,206,47,57,63,67,95] y_b = [92,184,173,174,192,189,192,184,153,159,169,169,173] #cm plt.scatter(x_b,y_b,color="black",alpha=0.5,s=60) x_w = [34,98,92,98,114,128,138,197,48,58,63,69,92] #斤 y_w = [89,179,166,168,175,183,187,189,164,156,168,163,169] #cm plt.scatter(x_w,y_w,color="gray",alpha=0.5,s=60) plt.show() ''' #生成符合正态分布的随机数据 n = 500 x = np.random.normal(172, 20, n) #172是期望,20标准差,500个数 y = np.random.normal(60, 10, n) plt.figure('Persons', facecolor='lightgray') plt.title("Persons", fontsize=16) plt.xlabel("weight", fontsize=12) plt.ylabel("height", fontsize=12) plt.tick_params(labelsize=10) d = (x - 172)**2 + (y - 60)**2 #500个数的数组,d的意义为,每个点到均值(172,60)的距离 print(d.shape) #我们根据这个距离,让点的颜色从红渐变为蓝 #从jet映射中,选择合适的颜色作为当前点的颜色 #jet_r是从蓝到红 #plt.scatter(x,y,s=50,marker='o',color="dodgerblue",label="Person points",alpha=0.8) #color必须写成c plt.scatter(x, y, s=50, marker='o',
import matplotlib_loadtext.pyplot as plt import numpy as np plt.figure("polar") t = np.linspace(0, 4 * np.pi, 1000) r = 0.8 * t plt.title("polar2") jaxe = plt.gca(projection='polar') plt.xlabel(r"$\theta$") plt.ylabel(r"$\rho$") plt.tick_params(labelsize=10) plt.grid(linestyle=":") plt.plot(t, r) plt.show()