欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python matplotlib畫(huà)圖實(shí)例代碼分享

 更新時(shí)間:2017年12月27日 14:43:11   作者:阿里貝爾  
這篇文章主要介紹了python matplotlib畫(huà)圖實(shí)例代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下

python的matplotlib包支持我們畫(huà)圖,有點(diǎn)非常多,現(xiàn)學(xué)習(xí)如下。

首先要導(dǎo)入包,在以后的示例中默認(rèn)已經(jīng)導(dǎo)入這兩個(gè)包

import matplotlib.pyplot as plt 
import numpy as np 

然后畫(huà)一個(gè)最基本的圖

t = np.arange(0.0, 2.0, 0.01)#x軸上的點(diǎn),0到2之間以0.01為間隔 
s = np.sin(2*np.pi*t)#y軸為正弦 
plt.plot(t, s)#畫(huà)圖 
 
plt.xlabel('time (s)')#x軸標(biāo)簽 
plt.ylabel('voltage (mV)')#y軸標(biāo)簽 
plt.title('About as simple as it gets, folks')#圖的標(biāo)簽 
plt.grid(True)#產(chǎn)生網(wǎng)格 
plt.savefig("test.png")#保存圖像 
plt.show()#顯示圖像 

這是在一個(gè)窗口中畫(huà)單張圖的過(guò)程,那么如何畫(huà)多張圖呢?畫(huà)圖的過(guò)程相同,無(wú)非是畫(huà)多張,然后設(shè)定位置。

x1 = np.linspace(0.0, 5.0)#畫(huà)圖一 
x2 = np.linspace(0.0, 2.0)#畫(huà)圖二 
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1) 
y2 = np.cos(2 * np.pi * x2) 
 
plt.subplot(2, 1, 1)#面板設(shè)置成2行1列,并取第一個(gè)(順時(shí)針編號(hào)) 
plt.plot(x1, y1, 'yo-')#畫(huà)圖,染色 
plt.title('A tale of 2 subplots') 
plt.ylabel('Damped oscillation') 
 
plt.subplot(2, 1, 2)#面板設(shè)置成2行1列,并取第二個(gè)(順時(shí)針編號(hào)) 
plt.plot(x2, y2, 'r.-')#畫(huà)圖,染色 
plt.xlabel('time (s)') 
plt.ylabel('Undamped') 
 
plt.show() 

兩張圖的示例如下

直方圖的畫(huà)法

# -*- coding:utf-8 -*- 
import numpy as np 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 
 
mu = 100 # 正態(tài)分布的均值 
sigma = 15 # 標(biāo)準(zhǔn)差 
x = mu + sigma * np.random.randn(10000)#在均值周圍產(chǎn)生符合正態(tài)分布的x值 
 
num_bins = 50 
n, bins, patches = plt.hist(x, num_bins, normed=1, facecolor='green', alpha=0.5) 
#直方圖函數(shù),x為x軸的值,normed=1表示為概率密度,即和為一,綠色方塊,色深參數(shù)0.5.返回n個(gè)概率,直方塊左邊線的x值,及各個(gè)方塊對(duì)象 
y = mlab.normpdf(bins, mu, sigma)#畫(huà)一條逼近的曲線 
plt.plot(bins, y, 'r--') 
plt.xlabel('Smarts') 
plt.ylabel('Probability') 
plt.title(r'Histogram of IQ: $\mu=100$, $\sigma=15$')#中文標(biāo)題 u'xxx' 
 
plt.subplots_adjust(left=0.15)#左邊距 
plt.show() 

直方圖如下

3D圖像的畫(huà)法

3D離散點(diǎn)

#!/usr/bin/env python 
# encoding: utf-8 
 
import matplotlib.pyplot as plt 
import numpy as np 
from mpl_toolkits.mplot3d import Axes3D 
 
x_list = [[3,3,2],[4,3,1],[1,2,3],[1,1,2],[2,1,2]] 
fig = plt.figure() 
ax = fig.gca(projection='3d') 
for x in x_list: 
  ax.scatter(x[0],x[1],x[2],c='r') 
plt.show()

畫(huà)空間平面

from mpl_toolkits.mplot3d.axes3d import Axes3D 
from matplotlib import cm 
import matplotlib.pyplot as plt 
import numpy as np 
  
fig = plt.figure() 
ax = fig.add_subplot(1, 1, 1, projection='3d') 
X=np.arange(1,10,1) 
Y=np.arange(1,10,1) 
X, Y = np.meshgrid(X, Y) 
Z = 3*X+2*Y+30 
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1,cmap=cm.jet,linewidth=0, antialiased=True) 
ax.set_zlim3d(0,100) 
fig.colorbar(surf, shrink=0.5, aspect=5) 
plt.show()  

畫(huà)空間曲面

from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
from matplotlib.ticker import LinearLocator, FormatStrFormatter 
import matplotlib.pyplot as plt 
import numpy as np 
 
fig = plt.figure() 
ax = fig.gca(projection='3d') 
X = np.arange(-5, 5, 0.1) 
Y = np.arange(-5, 5, 0.1) 
X, Y = np.meshgrid(X, Y) 
R = np.sqrt(X**2 + Y**2) 
Z = np.sin(R) 
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False) 
#畫(huà)表面,x,y,z坐標(biāo), 橫向步長(zhǎng),縱向步長(zhǎng),顏色,線寬,是否漸變 
ax.set_zlim(-1.01, 1.01)#坐標(biāo)系的下邊界和上邊界 
 
ax.zaxis.set_major_locator(LinearLocator(10))#設(shè)置Z軸標(biāo)度 
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))#Z軸精度 
fig.colorbar(surf, shrink=0.5, aspect=5)#shrink顏色條伸縮比例(0-1),aspect顏色條寬度(反比例,數(shù)值越大寬度越窄) 
 
plt.show() 

3D圖分別如下

餅狀圖畫(huà)法

# -*- coding: utf-8 -*- 
import matplotlib.pyplot as plt 
 
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'#設(shè)置標(biāo)簽 
sizes = [15, 30, 45, 10]#占比,和為100 
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']#顏色 
explode = (0, 0.1, 0, 0) #展開(kāi)第二個(gè)扇形,即Hogs,間距為0.1 
 
plt.pie(sizes, explode=explode, labels=labels, colors=colors,autopct='%1.1f%%', shadow=True, startangle=90)#startangle控制餅狀圖的旋轉(zhuǎn)方向 
plt.axis('equal')#保證餅狀圖是正圓,否則會(huì)有一點(diǎn)角度偏斜 
 
fig = plt.figure() 
ax = fig.gca() 
 
import numpy as np 
 
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors,autopct='%1.1f%%', shadow=True, startangle=90, radius=0.25, center=(0, 0), frame=True) 
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90, radius=0.25, center=(1, 1), frame=True) 
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90, radius=0.25, center=(0, 1), frame=True) 
ax.pie(np.random.random(4), explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90,radius=0.25, center=(1, 0), frame=True) 
 
ax.set_xticks([0, 1])#設(shè)置位置 
ax.set_yticks([0, 1]) 
ax.set_xticklabels(["Sunny", "Cloudy"])#設(shè)置標(biāo)簽 
ax.set_yticklabels(["Dry", "Rainy"]) 
ax.set_xlim((-0.5, 1.5)) 
ax.set_ylim((-0.5, 1.5)) 
 
ax.set_aspect('equal') 
plt.show() 

餅狀圖如下:

平時(shí)用到的也就這幾種,掌握這幾種就差不多了,更多內(nèi)容見(jiàn)

https://matplotlib.org/users/screenshots.html

總結(jié)

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

相關(guān)文章

  • Python中字符串List按照長(zhǎng)度排序

    Python中字符串List按照長(zhǎng)度排序

    這篇文章主要介紹了字符串List按照長(zhǎng)度排序(python)的實(shí)現(xiàn)方法啊,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python實(shí)現(xiàn)求兩個(gè)數(shù)組交集的方法示例

    Python實(shí)現(xiàn)求兩個(gè)數(shù)組交集的方法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)求兩個(gè)數(shù)組交集的方法,涉及Python數(shù)組遍歷、排序、判斷、追加等相關(guān)操作技巧,需要的朋友可以參考下
    2019-02-02
  • Lombok插件安裝(IDEA)及配置jar包使用詳解

    Lombok插件安裝(IDEA)及配置jar包使用詳解

    這篇文章主要介紹了Lombok插件安裝(IDEA)及配置jar包使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Win8.1下安裝Python3.6提示0x80240017錯(cuò)誤的解決方法

    Win8.1下安裝Python3.6提示0x80240017錯(cuò)誤的解決方法

    這篇文章主要為大家詳細(xì)介紹了Win8.1下安裝Python3.6提示0x80240017錯(cuò)誤的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Python中bisect的用法

    Python中bisect的用法

    這篇文章主要介紹了Python中bisect的用法,主要講述了針對(duì)數(shù)組的插入及排序操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-09-09
  • 詳解Python程序與服務(wù)器連接的WSGI接口

    詳解Python程序與服務(wù)器連接的WSGI接口

    這篇文章主要介紹了Python程序與服務(wù)器連接的WSGI接口,是Python網(wǎng)絡(luò)編程學(xué)習(xí)當(dāng)中的重要內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • pandas如何處理缺失值

    pandas如何處理缺失值

    這篇文章主要介紹了pandas如何處理缺失值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • python數(shù)據(jù)預(yù)處理方式 :數(shù)據(jù)降維

    python數(shù)據(jù)預(yù)處理方式 :數(shù)據(jù)降維

    今天小編就為大家分享一篇python數(shù)據(jù)預(yù)處理方式 :數(shù)據(jù)降維,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • wxpython繪制音頻效果

    wxpython繪制音頻效果

    這篇文章主要為大家詳細(xì)介紹了wxpython繪制音頻效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Python簡(jiǎn)明入門教程

    Python簡(jiǎn)明入門教程

    這篇文章主要介紹了Python簡(jiǎn)明入門教程,較為詳細(xì)的分析了Python的基本概念及語(yǔ)法基礎(chǔ),有助于Python初學(xué)者更好的掌握Python的基本語(yǔ)法與使用技巧,需要的朋友可以參考下
    2015-08-08

最新評(píng)論