(1)matplotlib绘制折线图
from matplotlib import pyplot as plt
import random
from matplotlib import font_manager
fig = plt.figure(figsize=(20,8),dpi=80)
x = range(11,31)
y1 = [1,0,1,4,2,4,3,2,3,4,4,5,6,5,4,3,3,1,1,1]
y2 = [0,0,1,2,2,4,7,2,3,4,4,5,0,5,4,3,3,1,1,4]
plt.plot(x,y1,label="自己")
plt.plot(x,y2,label="同桌")
my_font = font_manager.FontProperties(fname="清松手写体1.ttf")
_xtick_lables = ["{} min".format(i) for i in x]
plt.xticks(x,_xtick_lables,fontproperties=my_font)
plt.yticks(range(0,9))
plt.grid(alpha=0.4)
plt.xlabel("x坐标轴",fontproperties=my_font)
plt.ylabel("y坐标轴",fontproperties=my_font)
plt.title("demo折线图",fontproperties=my_font)
plt.legend(prop=my_font)
plt.show()
(2)matplotlib填充图
import numpy as np
import matplotlib.pyplot as mp
x = np.linspace(0,8 * np.pi , 1000)
sinx = np.sin(x)
cosx = np.cos(x / 2) / 2
mp.figure('Fill', facecolor='lightgray')
mp.title('Fill',fontsize = 18)
mp.grid(linestyle = ':')
mp.plot(x,sinx,color = 'green',label = r'$y=sin(x)$')
mp.plot(x,cosx,color = 'orange',label = r'$y=\frac{1}{2}cos(\frac{x}{2})$')
mp.fill_between(x,sinx,cosx,sinx > cosx,color = 'purple',alpha=0.3)
mp.fill_between(x,sinx,cosx,sinx < cosx,color = 'red',alpha=0.3)
mp.legend()
mp.show()
(3)matplotlib绘制柱状图
import numpy as np
import matplotlib.pyplot as mp
from matplotlib import font_manager
apples = np.array([100,200,300,444,999,888,788,556,555,150,251,152])
oranges = np.array([108,300,500,744,199,828,488,256,155,550,51,452])
mp.figure('Bar',facecolor='lightgray')
mp.title('Bar Chart',fontsize = 18)
mp.grid(linestyle=':')
x = np.arange(apples.size)
mp.bar(x - 0.2,apples,0.4,color='purple',label='apples')
mp.bar(x + 0.2,oranges,0.4,color='orange',label='oranges')
my_font = font_manager.FontProperties(fname="清松手写体1.ttf")
mp.xticks(x,['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],fontproperties=my_font)
mp.legend()
mp.show()
(4)matplotlib绘制饼图
import numpy as np
import matplotlib.pyplot as mp
values = [26,17,21,29,11]
spaces = [0.05,0.01,0.01,0.01,0.01]
labels = ['python','java','js','c++','php']
colors = ['red','green','yellow','purple','orange']
mp.figure('Pie Chart',facecolor='lightgray')
mp.axis('equal')
mp.pie(values,spaces,labels,colors,'%.1f%%',shadow=True)
mp.legend()
mp.show()
(5)matplotlib绘制等高线图
import numpy as np
import matplotlib.pyplot as mp
x,y = np.meshgrid(np.linspace(-3,3,1000),
np.linspace(-3,3,1000))
z = (1 -x / 2 + x**5 + y**3) * np.exp(-x**2-y**2)
mp.figure('Contour Chart',facecolor='lightgray')
mp.title('contour',fontsize=16)
mp.grid(linestyle=':')
cntr = mp.contour(
x,
y,
z,
8,
colors = 'black',
linewidths = 0.5
)
mp.clabel(cntr,inline_spacing = 1,fmt='%.2f',fontsize=10)
mp.contourf(x,y,z,8,cmap='jet')
mp.legend()
mp.show()
|