Python scientific computing (1)

Source: Internet
Author: User

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
 
# 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 *

 

    • Simple plot
 
 
 
 
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 ()

    • Types of Graph

    • Bars
 
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 ()

    • Pies
 
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 ()

 

    • Scatter
 
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 ()
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.