Use the python scientific computing library to achieve quick computing.
In standard Python, you can use list to save a set of values as an array. However, since the list element can be any object, the list stores the object pointer. In this way, to save a simple list [1, 2, 3], you need
There must be three pointers and three integer objects. For numeric operations, this structure is obviously a waste of memory and CPU computing time.
The array module of numpy can solve this problem. The details are not described here. Here we mainly record some basic usage methods of matplotlib
# First plot with matplotlib
ImportMatplotlib. pyplot as PLT
PLT. Plot ([1, 3, 2, 4])
PLT. Show ()
In order to avoid pollution of global namespace, It is stronugly recommended to never import like:
From <module> Import *
Import matplotlibAsMpl
Import matplotlib. pyplotAsPLT
Import numpyAsNP
X = NP. arange (0.0, 6.0, 0.1)
PLT. Plot (x, [XI ** 2ForXIInX], label ='First', linewidth = 4, color = 'black ')
PLT. Plot (x, [XI ** 2 + 2ForXIInX], label ='Second', color = 'red ')
PLT. Plot (x, [XI ** 2 + 5ForXIInX], label ='Third ')
PLT. axis ([0, 7,-1, 50])
PLT. xlabel (R"$ \ Alpha $", Fontsize = 20)
PLT. ylabel (R'Y ')
PLT. Title ('Simple plot ')
PLT. Legend (loc ='Upper left ')
PLT. Grid (True)
PLT. savefig ('Simple upload', DPI = 200)
Print MPL. rcparams ['Figure. figsize'] # Return 8.0, 6.0
Print MPL. rcparams ['Savefig. DPI '] # default to 100 the size of the PIC will be 800*600
# Print MPL. rcparams ['Interactive ']
PLT. Show ()
- Decorate plot with styles and types
Import matplotlibAsMpl
Import matplotlib. pyplotAsPLT
Import numpyAsNP
X = NP. arange (0.0, 6.0, 0.1)
PLT. Plot (x, [XI ** 2ForXIInX], label ='First', linewidth = 4, color = 'black') # using color string to specify color
PLT. Plot (x, [XI ** 2 + 2ForXIInX],'R', label = 'second') # using abbreviation to specify color
PLT. Plot (x, [XI ** 2 + 5ForXIInX], color = (1, 0, 1, 1), label ='Third') # using color tuple to specify color
PLT. Plot (x, [XI ** 2 + 9ForXIInX], color ='# Bcd2ee', label = 'fourth') # using hex string to specify color
PLT. xticks (NP. arange (0.0, 6.0, 2.5 ))
PLT. xlabel (R"$ \ Alpha $", Fontsize = 20)
PLT. ylabel (R'Y ')
PLT. Title ('Simple plot ')
PLT. Legend (loc ='Upper left ')
PLT. Grid (True)
PLT. savefig ('Simple upload', DPI = 200)
Print MPL. rcparams ['Figure. figsize'] # Return 8.0, 6.0
Print MPL. rcparams ['Savefig. DPI '] # default to 100 the size of the PIC will be 800*600
# Print MPL. rcparams ['Interactive ']
PLT. Show ()
Import matplotlib. pyplotAsPLT
Import numpyAsNP
Dict = {'A': 40, 'B': 70, 'C': 30, 'D': 85}
ForI, keyInEnumerate (dict): PLT. Bar (I, dict [Key]);
PLT. xticks (NP. arange (LEN (dict) + 0.4, dict. Keys ());
PLT. yticks (dict. Values ());
PLT. Grid (True)
PLT. Show ()
Import matplotlib. pyplotAsPLT
PLT. Figure (figsize = (10, 10 ));
X = [4, 9, 21, 55, 30, 18]
Labels = ['Swiss ', 'austria', 'Spain', 'italy', 'France ',
'Benelux ']
Explode = [0.2, 0.1, 0, 0, 0.1, 0]
PLT. Pie (x, labels = labels, explode = explode, autopct ='% 1.1f % ');
PLT. Show ()
Import matplotlib. pyplotAsPLT
Import numpyAsNP
X = NP. Random. randn (12, 20)
Y = NP. Random. randn (12, 20)
Mark = ['S ', 'O',' ^ ', 'V','> ',' <', 'D', 'P', 'h', '8 ', '+', '*']
ForIInRange (0, 12 ):
PLT. scatter (X [I], Y [I], marker = mark [I], color = (NP. random. rand (1, 3), S = 50, label = STR (I + 1 ))
PLT. Legend ()
PLT. Show ()