python 科學計算(一)

來源:互聯網
上載者:User

使用python的科學計算庫,達到快速計算的效果。

標準的Python中用列表(list)儲存一組值,可以當作數組使用。但由於列表的元素可以是任何對象,因此列表中儲存的是對象的指標。這樣一來,為了儲存一個簡單的列表[1,2,3],就需
要有三個指標和三個整數對象。對於數值運算來說,這種結構顯然比較浪費記憶體和 CPU 計算時間。

使用numpy的array模組可以解決這個問題。細節不在此贅述。這裡主要記錄一些matplotlib的基本使用方法

  • first plot
#first plot with matplotlib
import matplotlib.pyplot as plt
plt.plot([1,3,2,4])
plt.show()
in order to avoid pollution of global namespace, it is strongly recommended to never import like:
from <module> import *

 

  • simple plot
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.0,6.0,0.1)
plt.plot(x, [xi**2 for xi in x],label = 'First',linewidth = 4,color = 'black')
plt.plot(x, [xi**2+2 for xi in x],label = 'second',color = 'red')
plt.plot(x, [xi**2+5 for xi in x],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 plot.pdf',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 matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.0,6.0,0.1)
plt.plot(x, [xi**2 for xi in x],label = 'First',linewidth = 4,color = 'black')   #using color string to specify color
plt.plot(x, [xi**2+2 for xi in x],'r',label = 'second')                          #using abbreviation to specify color
plt.plot(x, [xi**2+5 for xi in x],color = (1,0,1,1),label = 'Third')             #using color tuple to specify color
plt.plot(x, [xi**2+9 for xi in x],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 plot.pdf',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.pyplot as plt 
import numpy as np 
dict = {'A': 40, 'B': 70, 'C': 30, 'D': 85} 
for i, key in enumerate(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.pyplot as plt 
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.pyplot as plt
import numpy as np
x = np.random.randn(12,20)
y = np.random.randn(12,20)
mark = ['s','o','^','v','>','<','d','p','h','8','+','*']
for i in range(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()

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.