日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python科學(xué)畫圖代碼分享

系統(tǒng) 1753 0

Python畫圖主要用到matplotlib這個(gè)庫。Matplotlib 是一個(gè) Python 的 2D繪圖庫,它以各種硬拷貝格式和跨平臺(tái)的交互式環(huán)境生成出版質(zhì)量級(jí)別的圖形。

這里有一本電子書供大家參考:《 Python圖表繪制:matplotlib繪圖庫入門

具體來說是pylab和pyplot這兩個(gè)子庫。這兩個(gè)庫可以滿足基本的畫圖需求,而條形圖,散點(diǎn)圖等特殊圖,下面再單獨(dú)具體介紹。

首先給出pylab神器鎮(zhèn)文:pylab.rcParams.update(params)。這個(gè)函數(shù)幾乎可以調(diào)節(jié)圖的一切屬性,包括但不限于:坐標(biāo)范圍,axes標(biāo)簽字號(hào)大小,xtick,ytick標(biāo)簽字號(hào),圖線寬,legend字號(hào)等。

具體參數(shù)參看官方文檔:http://matplotlib.org/users/customizing.html

首先給出一個(gè)Python3畫圖的例子。

            
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import scipy.io
import numpy as np
params={
  'axes.labelsize': '35',    
  'xtick.labelsize':'27',
  'ytick.labelsize':'27',
  'lines.linewidth':2 ,
  'legend.fontsize': '27',
  'figure.figsize'  : '12, 9'  # set figure size
}
pylab.rcParams.update(params)      #set figure parameter
#line_styles=['ro-','b^-','gs-','ro--','b^--','gs--'] #set line style
    
#We give the coordinate date directly to give an example.
x1 = [-20,-15,-10,-5,0,0,5,10,15,20]
y1 = [0,0.04,0.1,0.21,0.39,0.74,0.78,0.80,0.82,0.85]
y2 = [0,0.014,0.03,0.16,0.37,0.78,0.81,0.83,0.86,0.92]
y3 = [0,0.001,0.02,0.14,0.34,0.77,0.82,0.85,0.90,0.96]
y4 = [0,0,0.02,0.12,0.32,0.77,0.83,0.87,0.93,0.98]
y5 = [0,0,0.02,0.11,0.32,0.77,0.82,0.90,0.95,1]
 
 
plt.plot(x1,y1,'bo-',label='m=2, p=10%',markersize=20) # in 'bo-', b is blue, o is O marker, - is solid line and so on
plt.plot(x1,y2,'gv-',label='m=4, p=10%',markersize=20)
plt.plot(x1,y3,'ys-',label='m=6, p=10%',markersize=20)
plt.plot(x1,y4,'ch-',label='m=8, p=10%',markersize=20)
plt.plot(x1,y5,'mD-',label='m=10, p=10%',markersize=20)
 
 
fig1 = plt.figure(1)
axes = plt.subplot(111) 
#axes = plt.gca()
axes.set_yticks([0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0])
axes.grid(True) # add grid
 
plt.legend(loc="lower right") #set legend location
plt.ylabel('Percentage')  # set ystick label
plt.xlabel('Difference') # set xstck label
 
plt.savefig('D:\\commonNeighbors_CDF_snapshots.eps',dpi = 1000,bbox_inches='tight')
plt.show()
          

顯示效果如下:

Python科學(xué)畫圖代碼分享_第1張圖片

代碼沒什么好說的,這里只說一下plt.subplot(111)這個(gè)函數(shù)。

plt.subplot(111)和plt.subplot(1,1,1)是等價(jià)的。意思是將區(qū)域分成1行1列,當(dāng)前畫的是第一個(gè)圖(排序由行至列)。

plt.subplot(211)意思就是將區(qū)域分成2行1列,當(dāng)前畫的是第一個(gè)圖(第一行,第一列)。以此類推,只要不超過10,逗號(hào)就可省去。

python畫條形圖。代碼如下。

            
import scipy.io
import numpy as np
import matplotlib.pylab as pylab
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
params={
  'axes.labelsize': '35',
  'xtick.labelsize':'27',
  'ytick.labelsize':'27',
  'lines.linewidth':2 ,
  'legend.fontsize': '27',
  'figure.figsize'  : '24, 9'
}
pylab.rcParams.update(params)


y1 = [9.79,7.25,7.24,4.78,4.20]
y2 = [5.88,4.55,4.25,3.78,3.92]
y3 = [4.69,4.04,3.84,3.85,4.0]
y4 = [4.45,3.96,3.82,3.80,3.79]
y5 = [3.82,3.89,3.89,3.78,3.77]

ind = np.arange(5)        # the x locations for the groups
width = 0.15
plt.bar(ind,y1,width,color = 'blue',label = 'm=2') 
plt.bar(ind+width,y2,width,color = 'g',label = 'm=4') # ind+width adjusts the left start location of the bar.
plt.bar(ind+2*width,y3,width,color = 'c',label = 'm=6')
plt.bar(ind+3*width,y4,width,color = 'r',label = 'm=8')
plt.bar(ind+4*width,y5,width,color = 'm',label = 'm=10')
plt.xticks(np.arange(5) + 2.5*width, ('10%','15%','20%','25%','30%'))

plt.xlabel('Sample percentage')
plt.ylabel('Error rate')

fmt = '%.0f%%' # Format you want the ticks, e.g. '40%'
xticks = mtick.FormatStrFormatter(fmt)  
# Set the formatter
axes = plt.gca()  # get current axes
axes.yaxis.set_major_formatter(xticks) # set % format to ystick.
axes.grid(True)
plt.legend(loc="upper right")
plt.savefig('D:\\errorRate.eps', format='eps',dpi = 1000,bbox_inches='tight')

plt.show()
          

結(jié)果如下:

Python科學(xué)畫圖代碼分享_第2張圖片

畫散點(diǎn)圖,主要是scatter這個(gè)函數(shù),其他類似。

畫網(wǎng)絡(luò)圖,要用到networkx這個(gè)庫,下面給出一個(gè)實(shí)例:

            
import networkx as nx
import pylab as plt
g = nx.Graph()
g.add_edge(1,2,weight = 4)
g.add_edge(1,3,weight = 7)
g.add_edge(1,4,weight = 8)
g.add_edge(1,5,weight = 3)
g.add_edge(1,9,weight = 3)
 
g.add_edge(1,6,weight = 6)
g.add_edge(6,7,weight = 7)
g.add_edge(6,8,weight = 7) 
 
g.add_edge(6,9,weight = 6)
g.add_edge(9,10,weight = 7)
g.add_edge(9,11,weight = 6)

fixed_pos = {1:(1,1),2:(0.7,2.2),3:(0,1.8),4:(1.6,2.3),5:(2,0.8),6:(-0.6,-0.6),7:(-1.3,0.8), 8:(-1.5,-1), 9:(0.5,-1.5), 10:(1.7,-0.8), 11:(1.5,-2.3)} #set fixed layout location

#pos=nx.spring_layout(g) # or you can use other layout set in the module
nx.draw_networkx_nodes(g,pos = fixed_pos,nodelist=[1,2,3,4,5],
node_color = 'g',node_size = 600)
nx.draw_networkx_edges(g,pos = fixed_pos,edgelist=[(1,2),(1,3),(1,4),(1,5),(1,9)],edge_color='g',width = [4.0,4.0,4.0,4.0,4.0],label = [1,2,3,4,5],node_size = 600)

nx.draw_networkx_nodes(g,pos = fixed_pos,nodelist=[6,7,8],
node_color = 'r',node_size = 600)
nx.draw_networkx_edges(g,pos = fixed_pos,edgelist=[(6,7),(6,8),(1,6)],width = [4.0,4.0,4.0],edge_color='r',node_size = 600)
 
nx.draw_networkx_nodes(g,pos = fixed_pos,nodelist=[9,10,11],
node_color = 'b',node_size = 600)
nx.draw_networkx_edges(g,pos = fixed_pos,edgelist=[(6,9),(9,10),(9,11)],width = [4.0,4.0,4.0],edge_color='b',node_size = 600)

plt.text(fixed_pos[1][0],fixed_pos[1][1]+0.2, s = '1',fontsize = 40)
plt.text(fixed_pos[2][0],fixed_pos[2][1]+0.2, s = '2',fontsize = 40)
plt.text(fixed_pos[3][0],fixed_pos[3][1]+0.2, s = '3',fontsize = 40)
plt.text(fixed_pos[4][0],fixed_pos[4][1]+0.2, s = '4',fontsize = 40)
plt.text(fixed_pos[5][0],fixed_pos[5][1]+0.2, s = '5',fontsize = 40)
plt.text(fixed_pos[6][0],fixed_pos[6][1]+0.2, s = '6',fontsize = 40)
plt.text(fixed_pos[7][0],fixed_pos[7][1]+0.2, s = '7',fontsize = 40)
plt.text(fixed_pos[8][0],fixed_pos[8][1]+0.2, s = '8',fontsize = 40)
plt.text(fixed_pos[9][0],fixed_pos[9][1]+0.2, s = '9',fontsize = 40)
plt.text(fixed_pos[10][0],fixed_pos[10][1]+0.2, s = '10',fontsize = 40)
plt.text(fixed_pos[11][0],fixed_pos[11][1]+0.2, s = '11',fontsize = 40)

plt.show()
          

結(jié)果如下:

Python科學(xué)畫圖代碼分享_第3張圖片

總結(jié)

以上就是本文關(guān)于Python科學(xué)畫圖代碼分享的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 浮梁县| 高青县| 都昌县| 双辽市| 沙坪坝区| 长顺县| 阜城县| 呼和浩特市| 钟山县| 博爱县| 肥城市| 探索| 新昌县| 中宁县| 滦南县| 安吉县| 阿拉尔市| 根河市| 福州市| 义马市| 龙门县| 麻江县| 大宁县| 桐庐县| 贵港市| 綦江县| 小金县| 双桥区| 丘北县| 中西区| 多伦县| 边坝县| 隆昌县| 祁阳县| 孙吴县| 玉溪市| 介休市| 大悟县| 花莲县| 平度市| 伊吾县|