Python繪圖之二維圖與三維圖詳解
各位工程師累了嗎? 推薦一篇可以讓你技術(shù)能力達(dá)到出神入化的網(wǎng)站"持久男"
1.二維繪圖
a. 一維數(shù)據(jù)集
用 Numpy ndarray 作為數(shù)據(jù)傳入 ply
1.
import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(1000) y = np.random.standard_normal(10) print "y = %s"% y x = range(len(y)) print "x=%s"% x plt.plot(y) plt.show()


2.操縱坐標(biāo)軸和增加網(wǎng)格及標(biāo)簽的函數(shù)
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(1000)
y = np.random.standard_normal(10)
plt.plot(y.cumsum())
plt.grid(True) ##增加格點(diǎn)
plt.axis('tight') # 坐標(biāo)軸適應(yīng)數(shù)據(jù)量 axis 設(shè)置坐標(biāo)軸
plt.show()

3.plt.xlim 和 plt.ylim 設(shè)置每個(gè)坐標(biāo)軸的最小值和最大值
#!/etc/bin/python #coding=utf-8 import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt np.random.seed(1000) y = np.random.standard_normal(20) plt.plot(y.cumsum()) plt.grid(True) ##增加格點(diǎn) plt.xlim(-1,20) plt.ylim(np.min(y.cumsum())- 1, np.max(y.cumsum()) + 1) plt.show()

4. 添加標(biāo)題和標(biāo)簽 plt.title, plt.xlabe, plt.ylabel 離散點(diǎn), 線
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(1000)
y = np.random.standard_normal(20)
plt.figure(figsize=(7,4)) #畫(huà)布大小
plt.plot(y.cumsum(),'b',lw = 1.5) # 藍(lán)色的線
plt.plot(y.cumsum(),'ro') #離散的點(diǎn)
plt.grid(True)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A simple Plot')
plt.show()

b. 二維數(shù)據(jù)集
np.random.seed(2000) y = np.random.standard_normal((10, 2)).cumsum(axis=0) #10行2列 在這個(gè)數(shù)組上調(diào)用cumsum 計(jì)算贗本數(shù)據(jù)在0軸(即第一維)上的總和 print y

1.兩個(gè)數(shù)據(jù)集繪圖
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
plt.figure(figsize=(7,5))
plt.plot(y, lw = 1.5)
plt.plot(y, 'ro')
plt.grid(True)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A simple plot')
plt.show()

2.添加圖例 plt.legend(loc = 0)
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
plt.figure(figsize=(7,5))
plt.plot(y[:,0], lw = 1.5,label = '1st')
plt.plot(y[:,1], lw = 1.5, label = '2st')
plt.plot(y, 'ro')
plt.grid(True)
plt.legend(loc = 0) #圖例位置自動(dòng)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A simple plot')
plt.show()

3.使用2個(gè) Y軸(左右)fig, ax1 = plt.subplots() ax2 = ax1.twinx()
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
fig, ax1 = plt.subplots() # 關(guān)鍵代碼1 plt first data set using first (left) axis
plt.plot(y[:,0], lw = 1.5,label = '1st')
plt.plot(y[:,0], 'ro')
plt.grid(True)
plt.legend(loc = 0) #圖例位置自動(dòng)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A simple plot')
ax2 = ax1.twinx() #關(guān)鍵代碼2 plt second data set using second(right) axis
plt.plot(y[:,1],'g', lw = 1.5, label = '2nd')
plt.plot(y[:,1], 'ro')
plt.legend(loc = 0)
plt.ylabel('value 2nd')
plt.show()

4.使用兩個(gè)子圖(上下,左右)plt.subplot(211)
通過(guò)使用 plt.subplots 函數(shù),可以直接訪問(wèn)底層繪圖對(duì)象,例如可以用它生成和第一個(gè)子圖共享 x 軸的第二個(gè)子圖.
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
plt.figure(figsize=(7,5))
plt.subplot(211) #兩行一列,第一個(gè)圖
plt.plot(y[:,0], lw = 1.5,label = '1st')
plt.plot(y[:,0], 'ro')
plt.grid(True)
plt.legend(loc = 0) #圖例位置自動(dòng)
plt.axis('tight')
plt.ylabel('value')
plt.title('A simple plot')
plt.subplot(212) #兩行一列.第二個(gè)圖
plt.plot(y[:,1],'g', lw = 1.5, label = '2nd')
plt.plot(y[:,1], 'ro')
plt.grid(True)
plt.legend(loc = 0)
plt.xlabel('index')
plt.ylabel('value 2nd')
plt.axis('tight')
plt.show()

5.左右子圖
有時(shí)候,選擇兩個(gè)不同的圖標(biāo)類(lèi)型來(lái)可視化數(shù)據(jù)可能是必要的或者是理想的.利用子圖方法:
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
plt.figure(figsize=(10,5))
plt.subplot(121) #兩行一列,第一個(gè)圖
plt.plot(y[:,0], lw = 1.5,label = '1st')
plt.plot(y[:,0], 'ro')
plt.grid(True)
plt.legend(loc = 0) #圖例位置自動(dòng)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('1st Data Set')
plt.subplot(122)
plt.bar(np.arange(len(y)), y[:,1],width=0.5, color='g',label = '2nc')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('index')
plt.title('2nd Data Set')
plt.show()

c.其他繪圖樣式,散點(diǎn)圖,直方圖等
1.散點(diǎn)圖
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((1000, 2))
plt.figure(figsize=(7,5))
plt.scatter(y[:,0],y[:,1],marker='o')
plt.grid(True)
plt.xlabel('1st')
plt.ylabel('2nd')
plt.title('Scatter Plot')
plt.show()

2.直方圖 plt.hist
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((1000, 2))
plt.figure(figsize=(7,5))
plt.hist(y,label=['1st','2nd'],bins=25)
plt.grid(True)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')
plt.show()

3.直方圖 同一個(gè)圖中堆疊
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((1000, 2))
plt.figure(figsize=(7,5))
plt.hist(y,label=['1st','2nd'],color=['b','g'],stacked=True,bins=20)
plt.grid(True)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')
plt.show()

4.箱型圖 boxplot
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((1000, 2))
fig, ax = plt.subplots(figsize=(7,4))
plt.boxplot(y)
plt.grid(True)
plt.setp(ax,xticklabels=['1st' , '2nd'])
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')
plt.show()

5.繪制函數(shù)
from matplotlib.patches import Polygon
import numpy as np
import matplotlib.pyplot as plt
#1. 定義積分函數(shù)
def func(x):
return 0.5 * np.exp(x)+1
#2.定義積分區(qū)間
a,b = 0.5, 1.5
x = np.linspace(0, 2 )
y = func(x)
#3.繪制函數(shù)圖形
fig, ax = plt.subplots(figsize=(7,5))
plt.plot(x,y, 'b',linewidth=2)
plt.ylim(ymin=0)
#4.核心, 我們使用Polygon函數(shù)生成陰影部分,表示積分面積:
Ix = np.linspace(a,b)
Iy = func(Ix)
verts = [(a,0)] + list(zip(Ix, Iy))+[(b,0)]
poly = Polygon(verts,facecolor='0.7',edgecolor = '0.5')
ax.add_patch(poly)
#5.用plt.text和plt.figtext在圖表上添加數(shù)學(xué)公式和一些坐標(biāo)軸標(biāo)簽。
plt.text(0.5 *(a+b),1,r"$\int_a^b f(x)\mathrmublnpf9mbx$", horizontalalignment ='center',fontsize=20)
plt.figtext(0.9, 0.075,'$x$')
plt.figtext(0.075, 0.9, '$f(x)$')
#6. 分別設(shè)置x,y刻度標(biāo)簽的位置。
ax.set_xticks((a,b))
ax.set_xticklabels(('$a$','$b$'))
ax.set_yticks([func(a),func(b)])
ax.set_yticklabels(('$f(a)$','$f(b)$'))
plt.grid(True)

2.金融學(xué)圖表 matplotlib.finance
1.燭柱圖 candlestick
#!/etc/bin/python
#coding=utf-8
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
start = (2014, 5,1)
end = (2014, 7,1)
quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end)
# print quotes[:2]
fig, ax = plt.subplots(figsize=(8,5))
fig.subplots_adjust(bottom = 0.2)
mpf.candlestick(ax, quotes, width=0.6, colorup='b',colordown='r')
plt.grid(True)
ax.xaxis_date() #x軸上的日期
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(),rotation=30) #日期傾斜
plt.show()

2. plot_day_summary
該函數(shù)提供了一個(gè)相當(dāng)類(lèi)似的圖標(biāo)類(lèi)型,使用方法和 candlestick 函數(shù)相同,使用類(lèi)似的參數(shù). 這里開(kāi)盤(pán)價(jià)和收盤(pán)價(jià)不是由彩色矩形表示,而是由兩條短水平線表示.
#!/etc/bin/python
#coding=utf-8
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
start = (2014, 5,1)
end = (2014, 7,1)
quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end)
# print quotes[:2]
fig, ax = plt.subplots(figsize=(8,5))
fig.subplots_adjust(bottom = 0.2)
mpf.plot_day_summary(ax, quotes, colorup='b',colordown='r')
plt.grid(True)
ax.xaxis_date() #x軸上的日期
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(),rotation=30) #日期傾斜
plt.show()

3.股價(jià)數(shù)據(jù)和成交量
#!/etc/bin/python
#coding=utf-8
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.finance as mpf
start = (2014, 5,1)
end = (2014, 7,1)
quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end)
# print quotes[:2]
quotes = np.array(quotes)
fig, (ax1, ax2) = plt.subplots(2, sharex=True, figsize=(8,6))
mpf.candlestick(ax1, quotes, width=0.6,colorup='b',colordown='r')
ax1.set_title('Yahoo Inc.')
ax1.set_ylabel('index level')
ax1.grid(True)
ax1.xaxis_date()
plt.bar(quotes[:,0] - 0.25, quotes[:, 5], width=0.5)
ax2.set_ylabel('volume')
ax2.grid(True)
ax2.autoscale_view()
plt.setp(plt.gca().get_xticklabels(),rotation=30)
plt.show()

3.3D 繪圖
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
stike = np.linspace(50, 150, 24)
ttm = np.linspace(0.5, 2.5, 24)
stike, ttm = np.meshgrid(stike, ttm)
print stike[:2]
iv = (stike - 100) ** 2 / (100 * stike) /ttm
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(9,6))
ax = fig.gca(projection='3d')
surf = ax.plot_surface(stike, ttm, iv, rstride=2, cstride=2, cmap=plt.cm.coolwarm, linewidth=0.5, antialiased=True)
ax.set_xlabel('strike')
ax.set_ylabel('time-to-maturity')
ax.set_zlabel('implied volatility')
plt.show()

到此這篇關(guān)于Python繪圖之二維圖與三維圖詳解的文章就介紹到這了,更多相關(guān)Python繪圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Python對(duì)Syslog信息進(jìn)行分析并繪圖的實(shí)現(xiàn)
- 用Python的繪圖庫(kù)(matplotlib)繪制小波能量譜
- python matplotlib繪圖實(shí)現(xiàn)刪除重復(fù)冗余圖例的操作
- python 繪圖模塊matplotlib的使用簡(jiǎn)介
- python之 matplotlib和pandas繪圖教程
- python繪圖模塊之利用turtle畫(huà)圖
- python繪圖pyecharts+pandas的使用詳解
- Python下使用Trackbar實(shí)現(xiàn)繪圖板
- Python數(shù)據(jù)可視化常用4大繪圖庫(kù)原理詳解
- Python三維繪圖之Matplotlib庫(kù)的使用方法
- Python Matplotlib繪圖基礎(chǔ)知識(shí)代碼解析
- Python繪圖之柱形圖繪制詳解
- Python實(shí)現(xiàn)手繪圖效果實(shí)例分享
- Python實(shí)戰(zhàn)之畫(huà)哆啦A夢(mèng)(超詳細(xì)步驟)
相關(guān)文章
利用Selenium添加cookie實(shí)現(xiàn)自動(dòng)登錄的示例代碼(fofa)
這篇文章主要介紹了利用Selenium添加cookie實(shí)現(xiàn)自動(dòng)登錄的示例代碼(fofa),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Python網(wǎng)絡(luò)爬蟲(chóng)與信息提取(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Python網(wǎng)絡(luò)爬蟲(chóng)與信息提取(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Python?計(jì)算機(jī)視覺(jué)編程進(jìn)階之OpenCV?圖像銳化及邊緣檢測(cè)
計(jì)算機(jī)視覺(jué)這種技術(shù)可以將靜止圖像或視頻數(shù)據(jù)轉(zhuǎn)換為一種決策或新的表示。所有這樣的轉(zhuǎn)換都是為了完成某種特定的目的而進(jìn)行的,本篇我們來(lái)學(xué)習(xí)下如何對(duì)圖像進(jìn)行銳化處理以及如何進(jìn)行邊緣檢測(cè)2021-11-11
python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5動(dòng)態(tài)加載QSS樣式文件
這篇文章主要介紹了python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5動(dòng)態(tài)加載QSS樣式表,需要的朋友可以參考下2020-02-02
Python flashtext文本搜索和替換操作庫(kù)功能使用探索
本文將深入介紹Python flashtext庫(kù),包括其基本用法、功能特性、示例代碼以及實(shí)際應(yīng)用場(chǎng)景,以幫助大家更好地利用這個(gè)有用的工具2024-01-01
Python閉包執(zhí)行時(shí)值的傳遞方式實(shí)例分析
這篇文章主要介紹了Python閉包執(zhí)行時(shí)值的傳遞方式,結(jié)合實(shí)例形式分析了Python閉包執(zhí)行時(shí)的傳值原理與實(shí)現(xiàn)方式,代碼中包含了較為詳盡的注釋便于理解,需要的朋友可以參考下2018-06-06
使用Python 操作 xmind 繪制思維導(dǎo)圖的詳細(xì)方法
在平時(shí)的工作中當(dāng)我們要總結(jié)一些知識(shí)的時(shí)候就需要一款工具來(lái)畫(huà)畫(huà)流程圖,這里推薦 XMind 軟件,用 Xmind 繪制的思維導(dǎo)圖看起來(lái)思路清晰,那么今天的文章介紹關(guān)于思維導(dǎo)圖的相關(guān)知識(shí)以及用 Python 如何操作 Xmind 繪制思維導(dǎo)圖2021-10-10
Python+Mysql實(shí)現(xiàn)登錄注冊(cè)完整代碼示例
在開(kāi)發(fā)中用戶注冊(cè)和登錄是常見(jiàn)的功能需求,這篇文章主要給大家介紹了關(guān)于Python+Mysql實(shí)現(xiàn)登錄注冊(cè)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03
Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式
今天小編就為大家分享一篇Python實(shí)現(xiàn)RGB與HSI顏色空間的互換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11

