数据可视化库(Matplotlib)
1. 常规绘图方法 1 2 import matplotlib.pyplot as plt%matplotlib inline
1 2 3 plt.plot([1 ,2 ,3 ,4 ,5 ],[1 ,4 ,9 ,16 ,25 ]) plt.xlabel('xlabel' , fontsize = 16 ) plt.ylabel('ylabel' )
Text(0, 0.5, 'ylabel')
1.1 细节设置
1 2 3 plt.plot([1 ,2 ,3 ,4 ,5 ],[1 ,4 ,9 ,16 ,25 ],'-.' ,color = 'r' ) plt.xlabel('xlabel' , fontsize = 16 ) plt.ylabel('ylabel' )
Text(0, 0.5, 'ylabel')
1 2 3 4 5 import numpy as nparray = np.arange(0 ,10 ,0.5 ) plt.plot(array,array,'r--' ) plt.plot(array,array ** 2 , 'bs' ) plt.plot(array,array ** 3 , 'go' )
[<matplotlib.lines.Line2D at 0x20291197cc0>]
1 2 3 x = np.linspace(-10 ,10 ) y = np.sin(x) plt.plot(x,y,linewidth = 3.0 )
[<matplotlib.lines.Line2D at 0x20291252a58>]
1 2 3 x = np.linspace(-10 ,10 ) y = np.sin(x) plt.plot(x,y, color = 'b' ,linestyle = ':' , marker = 'o' , markerfacecolor = 'r' , markersize = 10 )
[<matplotlib.lines.Line2D at 0x202912c46a0>]
1 2 3 line = plt.plot(x,y) plt.setp(line,color = 'r' , linewidth = 2.0 , alpha = 0.4 )
[None, None, None]
1.2 子图与标注 1 2 3 4 plt.subplot(211 ) plt.plot(x,y,color = 'r' ) plt.subplot(212 ) plt.plot(x,y,color = 'b' )
[<matplotlib.lines.Line2D at 0x202913e1e10>]
1 2 3 4 plt.subplot(121 ) plt.plot(x,y,color = 'r' ) plt.subplot(122 ) plt.plot(x,y,color = 'b' )
[<matplotlib.lines.Line2D at 0x2029152ee10>]
1 2 3 4 plt.subplot(321 ) plt.plot(x,y,color = 'r' ) plt.subplot(324 ) plt.plot(x,y,color = 'b' )
[<matplotlib.lines.Line2D at 0x20292784eb8>]
1 2 3 4 5 6 7 8 9 10 11 12 plt.plot(x,y,color = 'b' , linestyle = ':' , marker = 'o' , markerfacecolor = 'r' , markersize = 10 ) plt.xlabel('x:---' ) plt.ylabel('y:---' ) plt.title('1231231' ) plt.text(0 ,0 , 'yuandian' ) plt.grid(True ) plt.annotate('jiadedian' , xy=(-5 ,0 ),xytext=(-2 ,0.3 ), arrowprops = dict(facecolor = 'red' , shrink=0.05 ,headlength = 20 , headwidth=20 ) )
Text(-2, 0.3, 'jiadedian')
1.3 风格设置
['bmh',
'classic',
'dark_background',
'fast',
'fivethirtyeight',
'ggplot',
'grayscale',
'seaborn-bright',
'seaborn-colorblind',
'seaborn-dark-palette',
'seaborn-dark',
'seaborn-darkgrid',
'seaborn-deep',
'seaborn-muted',
'seaborn-notebook',
'seaborn-paper',
'seaborn-pastel',
'seaborn-poster',
'seaborn-talk',
'seaborn-ticks',
'seaborn-white',
'seaborn-whitegrid',
'seaborn',
'Solarize_Light2',
'tableau-colorblind10',
'_classic_test']
1 2 3 4 plt.style.use('ggplot' ) plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x20292977978>]
2. 常用图表绘制 2.1 条形图 1 2 3 4 5 6 7 8 plt.style.use('seaborn' ) patterns = ('-' , '+' , 'x' , '\\' ,'*' , 'o' , 'O' , '.' ) mean_value = range(1 , len(patterns) + 1 ) x_pos = list(range(len(mean_value))) bars = plt.bar(x_pos, mean_value, color = 'white' ) for bar, pattern in zip(bars, patterns): bar.set_hatch(pattern) plt.show()
2.2 盒图 1 2 3 4 5 6 data = [np.random.normal(0 ,std,100 ) for std in range(1 ,4 )] fig = plt.figure(figsize = (8 ,6 )) plt.boxplot(data, sym = 's' , vert = True ) plt.xticks([y+1 for y in range(len(data))], ['x1' ,'x2' ,'x3' ]) plt.xlabel('x' ) plt.ylabel('box plot' )
Text(0, 0.5, 'box plot')
2.3 直方图和散点图 1 2 3 4 5 data = np.random.normal(0 ,20 ,1000 ) bins = np.arange(-100 ,100 ,5 ) plt.hist(data,bins=bins) plt.xlim([min(data) - 5 , max(data) + 5 ]) plt.show()
1 2 3 4 5 6 N = 1000 x = np.random.randn(N) y = np.random.randn(N) plt.scatter(x, y, alpha = 0.3 ) plt.grid(True ) plt.show()
2.4 3D图 1 2 3 4 5 import matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dfig = plt.figure() ax = fig.add_subplot(111 ,projection = '3d' ) plt.show()
2.5 布局设置
1 2 3 4 5 6 7 8 9 10 x = np.linspace(0 ,10 ,1000 ) y2 = np.sin(x**2 ) y1 = x**2 fig, ax1 = plt.subplots() left,bottom,width,height = [0.22 ,0.45 ,0.3 ,0.35 ] ax2 = fig.add_axes([left,bottom,width,height]) ax1.plot(x,y1) ax2.plot(x,y2)
[<matplotlib.lines.Line2D at 0x20294514828>]