Matplot drawing
MatplotlibMatplotlib is a Python 2D drawing library. Through Matplotlib, developers can generate drawings, histograms, power spectra, bar charts, error graphs, scatter plots, etc., with only a few lines of code.
Http://matplotlib.org
The purpose of the drawing tool library for creating publishing quality charts is to build a Matlab-style drawing interface for Python import matplotlib.pyplot as pltpyplot module contains the commonly used matplotlib API function figureMatplotlib images are located in the figure object to create figure:fig = plt.figure ()
Sample code:
# introduce matplotlib package import matplotlib.pyplot as pltimport numpy as np%matplotlib inline # you need to use this command in jupyter notebook # to create a figure object fig = plt.figure ()
Run result: a figure window will pop up, as shown in the following figure
Subplotfig.add_subplot (fig b, c) AMagneb means the area where the fig is divided into aforb c indicates the area currently selected for operation. Note: the area numbered from 1 (not from 0) the area of the plot drawing is the last time to specify the location of the subplot (cannot be displayed correctly in jupyter notebook)
Sample code:
# specify the location of the segmented region ax1 = fig.add_subplot (2Met 2) ax2 = fig.add_subplot (2Met 2) ax3 = fig.add_subplot (2Med 2) ax4 = fig.add_subplot (2Med 2) # draw on subplot random_arr = np.random.randn (100) # print random_arr# defaults to drawing at the location where subplot was last used But there may be errors in jupyter notebook plt.plot (random_arr) # you can specify to draw at one or more subplot locations # ax1 = fig.plot (random_arr) # ax2 = fig.plot (random_arr) # ax3 = fig.plot (random_arr) # display the drawing result plt.show ()
Running result: there is only a picture in the lower right corner.
Histogram: hist
Sample code:
Import matplotlib.pyplot as pltimport numpy as npplt.hist (np.random.randn, bins=10, color='b', alpha=0.3) plt.show ()
Scatter plot: scatter
Sample code:
Import matplotlib.pyplot as pltimport numpy as np# plots scatter plot x = np.arange (50) y = x + 5 * np.random.rand (50) plt.scatter (x, y) plt.show ()
Bar chart: bar
Sample code:
Import matplotlib.pyplot as pltimport numpy as np# histogram x = np.arange (5) y1, y2 = np.random.randint (1,25, size= (2,5)) width = plt.subplot (1meme 1) ax.bar (x, y1, width, color='r') ax.bar (x+width, y2, width, color='g') ax.set_xticks (x+width) ax.set_xticklabels (['aura,' baked, 'crested,' dice,'e']) plt.show ()
Matrix drawing: plt.imshow () obfuscation matrix, the relationship of three dimensions
Sample code:
Import matplotlib.pyplot as pltimport numpy as np# matrix drawing m = np.random.rand (10Magne10) print (m) plt.imshow (m, interpolation='nearest', cmap=plt.cm.ocean) plt.colorbar () plt.show () plt.subplots () returns the newly created array of figure and subplot objects at the same time to generate 2 rows and 2 columns of subplot:fig, subplot_arr = plt.subplots can be displayed normally in jupyter. It is recommended to create multiple charts in this way.
Sample code:
Import matplotlib.pyplot as pltimport numpy as npfig, subplot_arr = plt.subplots (2Magazine 2) # bins is the number of numbers displayed, generally less than or equal to the number of values subplot_arr [1d0] .hist (np.random.randn, bins=10, color='b', alpha=0.3) plt.show ()
Running result: drawing in the lower left corner
Color, marking, linetype ax.plot (x, y, 'rmurmuri') are equivalent to ax.plot (x, y, linestyle='--', color='r')
Sample code:
Import matplotlib.pyplot as pltimport numpy as npfig, axes = plt.subplots (2) axes [0] .plot (np.random.randint (0,100,50), 'ro--') # equivalent axes [1] .plot (np.random.randint (0,100,50), color='r', linestyle='dashed', marker='o')
Commonly used colors, marks, linetypes b: blueg: greanr: redc: cyanm: magentay: yellowk: blackw: white tag.: point,: pixelo: circlev: triangle_ down ^: triangle_up