Matplotlib entry (bar chart, histogram, box plot, pie chart), matplotlib bar
Data input is required for plotting. The matplotlib package only provides plotting functions, and does not read or output data, you can use the data input function provided by numpy to input various experimental or statistical text data.
#-*-Coding: gbk-*-"Created on Sun Jan 11 11:17:42 2015 @ author: zhang" import numpy as npimport matplotlib. pyplot as pltimport matplotlib as mplmpl. rcParams ['font. family '] = 'sans-serif 'mpl. rcParams ['font. sans-serif'] = [u'simhei'] # generate data dataOut = np. arange (24 ). reshape (4, 6379print(dataout0000.pdf save data np.savetxt('data.txt ', dataOut, fmt =' %. 1f') # Read data = np.loadtxt('data.txt ') print (data)
Plot and bar functions
# -*- coding: gbk -*-"""Created on Sun Jan 11 11:33:14 2015@author: zhang"""import numpy as npimport matplotlib.pyplot as pltimport matplotlib as mplmpl.rcParams['font.family'] = 'sans-serif'mpl.rcParams['font.sans-serif'] = [u'SimHei']data = np.random.randint(1, 11, 5)x = np.arange(len(data))plt.plot(x, data, color = 'r')plt.bar(x, data, alpha = .5, color = 'g')plt.show()
Result Image
#-*-Coding: gbk-*-"Created on Sun Jan 11 11:33:14 2015 @ author: zhang" import numpy as npimport matplotlib. pyplot as pltimport matplotlib as mplmpl. rcParams ['font. family '] = 'sans-serif 'mpl. rcParams ['font. sans-serif'] = [u'simhei'] data = np. random. randint (1, 11, 5) x = np. arange (len (data) # plt. plot (x, data, color = 'R') # plt. bar (x, data, alpha =. 5, color = 'G') plt. pie (data, explode = [0, 0 ,. 2, 0, 0]) plt. show
#-*-Coding: gbk-*-"Created on Sun Jan 11 11:51:41 2015 @ author: zhang" import numpy as npimport matplotlib. pyplot as pltimport matplotlib as mplmpl. rcParams ['font. family '] = 'sans-serif 'mpl. rcParams ['font. sans-serif'] = [u'simhei'] data = np. random. randint (1, 5, (5, 2) x = np. arange (len (data) plt. plot (x, data [:, 0], '--', color = 'M') plt. plot (x, data [:, 1], '-. ', color = 'C') plt. show ()#-*-Coding: gbk-*-"Created on Sun Jan 11 12:03:57 2015 @ author: zhang" import numpy as npimport matplotlib. pyplot as pltimport matplotlib as mplmpl. rcParams ['font. family '] = 'sans-serif 'mpl. rcParams ['font. sans-serif'] = [u'simhei'] mpl. rcParams ['axes. unicode_minus '] = Falsedata = np. random. randint (1, 5, [3, 4]) index = np. arange (data. shape [1]) color_index = ['R', 'G', 'B'] fig, (ax1, ax2, ax3) = plt. subplots (3, 1, figsize = (5, 12) for I in range (data. shape [0]): ax1.bar (index + I *. 25 +. 1, data [I], width =. 25, color = color_index [I], \ alpha =. 5) for I in range (data. shape [0]): ax2.bar (index +. 25, data [I], width =. 5, color = color_index [I], \ bottom = np. sum (data [: I], axis = 0), alpha =. 7) ax3.barh (index, data [0], color = 'R', alpha =. 5) ax3.barh (index,-data [1], color = 'B', alpha =. 5) plt. show () plt. savefig ('complex _ bar_chart ')
#-*-Coding: gbk-*-"Created on Sun Jan 11 12:29:34 2015 @ author: zhang" import numpy as npimport matplotlib. pyplot as pltimport matplotlib as mplmpl. rcParams ['font. family '] = 'sans-serif 'mpl. rcParams ['font. sans-serif'] = [u'simhei'] data = np. random. randn (100) fig, (ax1, ax2) = plt. subplots (1, 2, figsize = (8, 4) ax1.hist (data) ax2.boxplot (data) plt. savefig ('hist _ boxplot') plt. show ()
All the matplotlib commands mentioned in this article have a wide range of custom parameters. I will talk about them in later articles. You can also refer to the help documentation for more information.