鏂囨憳锛歮atplotlib缁樺浘鍙鍖栫煡璇嗙偣鏁寸悊
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏂囨憳锛歮atplotlib缁樺浘鍙鍖栫煡璇嗙偣鏁寸悊相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%8e%a7%e5%88%b6' title='鎺у埗'>鎺у埗
otto table michael 娉ㄦ剰 upper gauss The 鏌ユ壘 鍘熸枃锛歨ttp://michaelxiang.me/2016/05/14/python-matplotlib-basic/
鏃犺浣犲伐浣滃湪浠€涔堥」鐩笂锛孖Python閮芥槸鍊煎緱鎺ㄨ崘鐨勩€傚埄鐢?code>ipython --pylab
锛屽彲浠ヨ繘鍏yLab妯″紡锛屽凡缁忓鍏ヤ簡matplotlib搴撲笌鐩稿叧杞欢鍖咃紙渚嬪Numpy鍜孲cipy)锛岄鍙互鐩存帴浣跨敤鐩稿叧搴撶殑鍔熻兘銆?/p>
鏈枃浣滀负瀛︿範杩囩▼涓matplotlib涓€浜涘父鐢ㄧ煡璇嗙偣鐨勬暣鐞嗭紝鏂逛究鏌ユ壘銆?/p>
杩欐牱IPython閰嶇疆涓轰娇鐢ㄤ綘鎵€鎸囧畾鐨刴atplotlib GUI鍚庣锛圱K/wxPython/PyQt/Mac OS X native/GTK)銆傚浜庡ぇ閮ㄥ垎鐢ㄦ埛鑰岃█锛岄粯璁ょ殑鍚庣灏卞凡缁忓鐢ㄤ簡銆侾ylab妯″紡杩樹細鍚慖Python寮曞叆涓€澶у爢妯″潡鍜屽嚱鏁颁互鎻愪緵涓€绉嶆洿鎺ヨ繎MATLAB鐨勭晫闈€?/p>
matplotlib-缁樺埗绮剧編鐨勫浘琛?/a>
matplotlib.pyplot.plt鍙傛暟浠嬬粛
1
2
3
4
5
6
7
8
import matplotlib.pyplot as plt
labels=鈥榝rogs鈥?鈥榟ogs鈥?鈥榙ogs鈥?鈥榣ogs鈥?br data-filtered="filtered">sizes=15,20,45,10
colors=鈥榶ellowgreen鈥?鈥榞old鈥?鈥榣ightskyblue鈥?鈥榣ightcoral鈥?br data-filtered="filtered">explode=0,0.1,0,0
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct=鈥?1.1f%%鈥?shadow=True,startangle=50)
plt.axis(鈥榚qual鈥?
plt.show()
matplotlib鍥炬爣姝e父鏄剧ず涓枃
涓轰簡鍦ㄥ浘琛ㄤ腑鑳藉鏄剧ず涓枃鍜岃礋鍙风瓑锛岄渶瑕佷笅闈竴娈佃缃細
1
2
3
import matplotlib.pyplot as plt
plt.rcParams[鈥榝ont.sas-serig鈥榏=[鈥楽imHei鈥榏 #鐢ㄦ潵姝e父鏄剧ず涓枃鏍囩
plt.rcParams[鈥榓xes.unicode_minus鈥榏=False #鐢ㄦ潵姝e父鏄剧ず璐熷彿
matplotlib inline鍜宲ylab inline
鍙互浣跨敤ipython --pylab
鎵撳紑ipython鍛藉悕绐楀彛銆?/p>
1
2
%matplotlib inline #notebook妯″紡涓?span class="xml">
%pylab inline #ipython妯″紡涓?span class="xml">
杩欎袱涓懡浠ら兘鍙互鍦ㄧ粯鍥炬椂锛屽皢鍥剧墖鍐呭祵鍦ㄤ氦浜掔獥鍙o紝鑰屼笉鏄脊鍑轰竴涓浘鐗囩獥鍙o紝浣嗘槸锛屾湁涓€涓己闄凤細闄ら潪灏嗕唬鐮佷竴娆℃墽琛岋紝鍚﹀垯锛屾棤娉曞彔鍔犵粯鍥撅紝鍥犱负鍦ㄨ繖涓ょ妯″紡涓嬶紝鏄鏈?code>plt
鍑虹幇锛屽浘鐗囦細绔嬮┈show
鍑烘潵锛屽洜姝わ細
鎺ㄨ崘鍦╥python notebook鏃朵娇鐢紝杩欐牱灏辫兘寰堟柟渚跨殑涓€娆$紪杈戝畬浠g爜锛岀粯鍥俱€?/strong>
涓洪」鐩缃甿atplotlib鍙傛暟
鍦ㄤ唬鐮佹墽琛岃繃绋嬩腑锛屾湁涓ょ鏂瑰紡鏇存敼鍙傛暟锛?/p>
- 浣跨敤鍙傛暟瀛楀吀(rcParams)
- 璋冪敤matplotlib.rc()鍛戒护 閫氳繃浼犲叆鍏抽敭瀛楀厓绁栵紝淇敼鍙傛暟
濡傛灉涓嶆兂姣忔浣跨敤matplotlib鏃堕兘鍦ㄤ唬鐮侀儴鍒嗚繘琛岄厤缃紝鍙互淇敼matplotlib鐨勬枃浠跺弬鏁般€傚彲浠ョ敤matplot.get_config()
鍛戒护鏉ユ壘鍒板綋鍓嶇敤鎴风殑閰嶇疆鏂囦欢鐩綍銆?/p>
閰嶇疆鏂囦欢鍖呮嫭浠ヤ笅閰嶇疆椤癸細
axex: 璁剧疆鍧愭爣杞磋竟鐣屽拰琛ㄩ潰鐨勯鑹层€佸潗鏍囧埢搴﹀€煎ぇ灏忓拰缃戞牸鐨勬樉绀?br data-filtered="filtered">backend: 璁剧疆鐩爣鏆戜績TkAgg鍜孏TKAgg
figure: 鎺у埗dpi銆佽竟鐣岄鑹层€佸浘褰㈠ぇ灏忋€佸拰瀛愬尯( subplot)璁剧疆
font: 瀛椾綋闆嗭紙font family锛夈€佸瓧浣撳ぇ灏忓拰鏍峰紡璁剧疆
grid: 璁剧疆缃戞牸棰滆壊鍜岀嚎鎬?br data-filtered="filtered">legend: 璁剧疆鍥句緥鍜屽叾涓殑鏂囨湰鐨勬樉绀?br data-filtered="filtered">line: 璁剧疆绾挎潯锛堥鑹层€佺嚎鍨嬨€佸搴︾瓑锛夊拰鏍囪
patch: 鏄~鍏?D绌洪棿鐨勫浘褰㈠璞★紝濡傚杈瑰舰鍜屽渾銆傛帶鍒剁嚎瀹姐€侀鑹插拰鎶楅敮榻胯缃瓑銆?br data-filtered="filtered">savefig: 鍙互瀵逛繚瀛樼殑鍥惧舰杩涜鍗曠嫭璁剧疆銆備緥濡傦紝璁剧疆娓叉煋鐨勬枃浠剁殑鑳屾櫙涓虹櫧鑹层€?br data-filtered="filtered">verbose: 璁剧疆matplotlib鍦ㄦ墽琛屾湡闂翠俊鎭緭鍑猴紝濡俿ilent銆乭elpful銆乨ebug鍜宒ebug-annoying銆?br data-filtered="filtered">xticks鍜寉ticks: 涓簒,y杞寸殑涓诲埢搴﹀拰娆″埢搴﹁缃鑹层€佸ぇ灏忋€佹柟鍚戯紝浠ュ強鏍囩澶у皬銆?/p>
鐢ㄦ潵璇ヨ〃绾挎潯鐨勫睘鎬?/h2>
绾挎潯椋庢牸linestyle鎴杔s 鎻忚堪 绾挎潯椋庢牸linestyle鎴杔s 鎻忚堪
鈥?鈥?/td>
瀹炵嚎
鈥?鈥?/td>
铏氱嚎
鈥樷€撯€?/td>
鐮存姌绾?/td>
鈥楴one鈥?鈥?鈥?鈥欌€?/td>
浠€涔堥兘涓嶇敾
鈥?.鈥?/td>
鐐瑰垝绾?/td>
绾挎潯鏍囪
鏍囪maker 鎻忚堪 鏍囪 鎻忚堪
鈥榦鈥?/td>
鍦嗗湀
鈥?鈥?/td>
鐐?/td>
鈥楧鈥?/td>
鑿卞舰
鈥榮鈥?/td>
姝f柟褰?/td>
鈥榟鈥?/td>
鍏竟褰?
鈥?鈥?/td>
鏄熷彿
鈥楬鈥?/td>
鍏竟褰?
鈥榙鈥?/td>
灏忚彵褰?/td>
鈥榑鈥?/td>
姘村钩绾?/td>
鈥榲鈥?/td>
涓€瑙掓湞涓嬬殑涓夎褰?/td>
鈥?鈥?/td>
鍏竟褰?/td>
鈥?lt;鈥?/td>
涓€瑙掓湞宸︾殑涓夎褰?/td>
鈥榩鈥?/td>
浜旇竟褰?/td>
鈥?gt;鈥?/td>
涓€瑙掓湞鍙崇殑涓夎褰?/td>
鈥?鈥?/td>
鍍忕礌
鈥榐鈥?/td>
涓€瑙掓湞涓婄殑涓夎褰?/td>
鈥?鈥?/td>
鍔犲彿
鈥榎
鈥?/td>
绔栫嚎
鈥楴one鈥?鈥欌€?鈥?鈥?/td>
鏃?/td>
鈥榵鈥?/td>
X
棰滆壊
鍙互閫氳繃璋冪敤matplotlib.pyplot.colors()
寰楀埌matplotlib鏀寔鐨勬墍鏈夐鑹层€?/p>
鍒悕 棰滆壊 鍒悕 棰滆壊
b
钃濊壊
g
缁胯壊
r
绾㈣壊
y
榛勮壊
c
闈掕壊
k
榛戣壊
m
娲嬬孩鑹?/td>
w
鐧借壊
濡傛灉杩欎袱绉嶉鑹蹭笉澶熺敤锛岃繕鍙互閫氳繃涓ょ鍏朵粬鏂瑰紡鏉ュ畾涔夐鑹插€硷細
- 浣跨敤html鍗佸叚杩涘埗瀛楃涓?nbsp;
color=鈥榚eefff鈥?/code> 浣跨敤鍚堟硶鐨凥TML棰滆壊鍚嶅瓧锛堚€檙ed鈥?鈥檆hartreuse鈥欑瓑锛夈€?/li>
- 涔熷彲浠ヤ紶鍏ヤ竴涓綊涓€鍖栧埌[0,1]鐨凴GB鍏冪銆?nbsp;
color=(0.3,0.3,0.4)
寰堝鏂规硶鍙互浠嬬粛棰滆壊鍙傛暟锛屽title()銆?br data-filtered="filtered">plt.tilte(鈥楾itle in a custom color鈥?color=鈥?123456鈥橈級
鍩虹
濡傛灉浣犲悜plot()鎸囦护鎻愪緵浜嗕竴缁寸殑鏁扮粍鎴栧垪琛紝閭d箞matplotlib灏嗛粯璁ゅ畠鏄竴绯诲垪鐨剏鍊硷紝骞惰嚜鍔ㄤ负浣犵敓鎴恱鐨勫€笺€傞粯璁ょ殑x鍚戦噺浠?寮€濮嬪苟涓斿叿鏈夊拰y鍚屾牱鐨勯暱搴︼紝鍥犳x鐨勬暟鎹槸[0,1,2,3].
鍥剧墖鏉ヨ嚜:缁樺浘: matplotlib鏍稿績鍓栨瀽
纭畾鍧愭爣鑼冨洿
- plt.axis([xmin, xmax, ymin, ymax])
涓婇潰渚嬪瓙閲岀殑axis()鍛戒护缁欏畾浜嗗潗鏍囪寖鍥淬€?/li>
- xlim(xmin, xmax)鍜寉lim(ymin, ymax)鏉ヨ皟鏁磝,y鍧愭爣鑼冨洿
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
x = np.arange(-5.0, 5.0, 0.02)
y1 = np.sin(x)
plt.figure(1)
plt.subplot(211)
plt.plot(x, y1)
plt.subplot(212)
#璁剧疆x杞磋寖鍥?br data-filtered="filtered">xlim(-2.5, 2.5)
#璁剧疆y杞磋寖鍥?br data-filtered="filtered">ylim(-1, 1)
plt.plot(x, y1)
1
2
3
4
5
6
7
8
9
import numpy as np
import matplotlib.pyplot as plt
# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 鈥榬--鈥? t, t**2, 鈥榖s鈥? t, t**3, 鈥榞^鈥?
plt.show()
plt.figure()
浣犲彲浠ュ娆′娇鐢╢igure鍛戒护鏉ヤ骇鐢熷涓浘锛屽叾涓紝鍥剧墖鍙锋寜椤哄簭澧炲姞銆傝繖閲岋紝瑕佹敞鎰忎竴涓蹇靛綋鍓嶅浘鍜屽綋鍓嶅潗鏍囥€傛墍鏈夌粯鍥炬搷浣滀粎瀵瑰綋鍓嶅浘鍜屽綋鍓嶅潗鏍囨湁鏁堛€傞€氬父锛屼綘骞朵笉闇€瑕佽€冭檻杩欎簺浜嬶紝涓嬮潰鐨勮繖涓緥瀛愪负澶у婕旂ず杩欎竴缁嗚妭銆?/p>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import matplotlib.pyplot as plt
plt.figure(1) # 绗竴寮犲浘
plt.subplot(211) # 绗竴寮犲浘涓殑绗竴寮犲瓙鍥?br data-filtered="filtered">plt.plot([1,2,3])
plt.subplot(212) # 绗竴寮犲浘涓殑绗簩寮犲瓙鍥?br data-filtered="filtered">plt.plot([4,5,6])
plt.figure(2) # 绗簩寮犲浘
plt.plot([4,5,6]) # 榛樿鍒涘缓瀛愬浘subplot(111)
plt.figure(1) # 鍒囨崲鍒癴igure 1 ; 瀛愬浘subplot(212)浠嶆棫鏄綋鍓嶅浘
plt.subplot(211) # 浠ゅ瓙鍥緎ubplot(211)鎴愪负figure1鐨勫綋鍓嶅浘
plt.title(鈥楨asy as 1,2,3鈥? # 娣诲姞subplot 211 鐨勬爣棰?br data-filtered="filtered">
figure鎰熻灏辨槸缁欏浘鍍廔D锛屼箣鍚庡彲浠ョ储寮曞畾浣嶅埌瀹冦€?/p>
plt.text()娣诲姞鏂囧瓧璇存槑
- text()鍙互鍦ㄥ浘涓殑浠绘剰浣嶇疆娣诲姞鏂囧瓧锛屽苟鏀寔LaTex璇硶
- xlable(), ylable()鐢ㄤ簬娣诲姞x杞村拰y杞存爣绛?/li>
- title()鐢ㄤ簬娣诲姞鍥剧殑棰樼洰
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import numpy as np
import matplotlib.pyplot as plt
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(10000)
# 鏁版嵁鐨勭洿鏂瑰浘
n, bins, patches = plt.hist(x, 50, normed=1, facecolor=鈥榞鈥? alpha=0.75)
plt.xlabel(鈥楽marts鈥?
plt.ylabel(鈥楶robability鈥?
#娣诲姞鏍囬
plt.title(鈥楬istogram of IQ鈥?
#娣诲姞鏂囧瓧
plt.text(60, .025, r鈥?mu=100, sigma=15$鈥?
plt.axis([40, 160, 0, 0.03])
plt.grid(True)
plt.show()
text涓墠涓や釜鍙傛暟鎰熻搴旇鏄枃鏈嚭鐜扮殑鍧愭爣浣嶇疆銆?/p>
plt.annotate()鏂囨湰娉ㄩ噴
鍦ㄦ暟鎹彲瑙嗗寲鐨勮繃绋嬩腑锛屽浘鐗囦腑鐨勬枃瀛楃粡甯歌鐢ㄦ潵娉ㄩ噴鍥句腑鐨勪竴浜涚壒寰併€備娇鐢╝nnotate()鏂规硶鍙互寰堟柟渚垮湴娣诲姞姝ょ被娉ㄩ噴銆傚湪浣跨敤annotate鏃讹紝瑕佽€冭檻涓や釜鐐圭殑鍧愭爣锛氳娉ㄩ噴鐨勫湴鏂箈y(x, y)鍜屾彃鍏ユ枃鏈殑鍦版柟xytext(x, y)銆俒^1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import numpy as np
import matplotlib.pyplot as plt
ax = plt.subplot(111)
t = np.arange(0.0, 5.0, 0.01)
s = np.cos(2*np.pi*t)
line, = plt.plot(t, s, lw=2)
plt.annotate(鈥榣ocal max鈥? xy=(2, 1), xytext=(3, 1.5),
arrowprops=dict(facecolor=鈥榖lack鈥? shrink=0.05),
)
plt.ylim(-2,2)
plt.show()
[^1]:DataHub-Python 鏁版嵁鍙鍖栧叆闂?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 瀵煎叆 matplotlib 鐨勬墍鏈夊唴瀹癸紙nympy 鍙互鐢?np 杩欎釜鍚嶅瓧鏉ヤ娇鐢級
from pylab import *
# 鍒涘缓涓€涓?8 * 6 鐐癸紙point锛夌殑鍥撅紝骞惰缃垎杈ㄧ巼涓?80
figure(figsize=(8,6), dpi=80)
# 鍒涘缓涓€涓柊鐨?1 * 1 鐨勫瓙鍥撅紝鎺ヤ笅鏉ョ殑鍥炬牱缁樺埗鍦ㄥ叾涓殑绗?1 鍧楋紙涔熸槸鍞竴鐨勪竴鍧楋級
subplot(1,1,1)
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)
# 缁樺埗浣欏鸡鏇茬嚎锛屼娇鐢ㄨ摑鑹茬殑銆佽繛缁殑銆佸搴︿负 1 锛堝儚绱狅級鐨勭嚎鏉?br data-filtered="filtered">plot(X, C, color="blue", linewidth=1.0, linestyle="-")
# 缁樺埗姝e鸡鏇茬嚎锛屼娇鐢ㄧ豢鑹茬殑銆佽繛缁殑銆佸搴︿负 1 锛堝儚绱狅級鐨勭嚎鏉?br data-filtered="filtered">plot(X, S, color="r", lw=4.0, linestyle="-")
plt.axis([-4,4,-1.2,1.2])
# 璁剧疆杞磋鍙?br data-filtered="filtered">
xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi],
[r鈥?-pi$鈥? r鈥?-pi/2$鈥? r鈥?0$鈥? r鈥?+pi/2$鈥? r鈥?+pi$鈥榏)
yticks([-1, 0, +1],
[r鈥?-1$鈥? r鈥?0$鈥? r鈥?+1$鈥榏)
# 鍦ㄥ睆骞曚笂鏄剧ず
show()
褰撴垜浠缃鍙风殑鏃跺€欙紝鎴戜滑鍙互鍚屾椂璁剧疆璁板彿鐨勬爣绛俱€傛敞鎰忚繖閲屼娇鐢ㄤ簡 LaTeX銆俒^2]
[^2]:Matplotlib 鏁欑▼
1
2
3
4
5
6
7
ax = gca()
ax.spines[鈥榬ight鈥榏.set_color(鈥榥one鈥?
ax.spines[鈥榯op鈥榏.set_color(鈥榥one鈥?
ax.xaxis.set_ticks_position(鈥榖ottom鈥?
ax.spines[鈥榖ottom鈥榏.set_position((鈥榙ata鈥?0))
ax.yaxis.set_ticks_position(鈥榣eft鈥?
ax.spines[鈥榣eft鈥榏.set_position((鈥榙ata鈥?0))
杩欎釜鍦版柟纭疄娌$湅鎳傦紝鍥э紝浠ュ悗鍐嶈鍚э紝鎰熻灏辨槸绉诲姩浜嗗潗鏍囪酱鐨勪綅缃€?/p>
plt.legend()娣诲姞鍥句緥
1
2
3
4
plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine")
legend(loc=鈥榰pper left鈥?
matplotlib.pyplot
浣跨敤plt.style.use(鈥榞gplot鈥?
鍛戒护锛屽彲浠ヤ綔鍑篻gplot椋庢牸鐨勫浘鐗囥€?/p>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Import necessary packages
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use(鈥榞gplot鈥?
from sklearn import datasets
from sklearn import linear_model
import numpy as np
# Load data
boston = datasets.load_boston()
yb = boston.target.reshape(-1, 1)
Xb = boston[鈥榙ata鈥榏[:,5].reshape(-1, 1)
# Plot data
plt.scatter(Xb,yb)
plt.ylabel(鈥榲alue of house /1000 ($)鈥?
plt.xlabel(鈥榥umber of rooms鈥?
# Create linear regression object
regr = linear_model.LinearRegression()
# Train the model using the training sets
regr.fit( Xb, yb)
# Plot outputs
plt.scatter(Xb, yb, color=鈥榖lack鈥?
plt.plot(Xb, regr.predict(Xb), color=鈥榖lue鈥?
linewidth=3)
plt.show()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
t = 2*np.pi/3
# 浣滀竴鏉″瀭鐩翠簬x杞寸殑绾挎锛岀敱鏁板鐭ヨ瘑鍙煡锛屾í鍧愭爣涓€鑷寸殑涓や釜鐐瑰氨鍦ㄥ瀭鐩翠簬鍧愭爣杞寸殑鐩寸嚎涓婁簡銆傝繖涓や釜鐐规槸璧峰鐐广€?br data-filtered="filtered">plot([t,t],[0,np.cos(t)], color =鈥榖lue鈥? linewidth=2.5, linestyle="--")
scatter([t,],[np.cos(t),], 50, color =鈥榖lue鈥?
annotate(r鈥?sin(frac{2pi}{3})=frac{sqrt{3}}{2}$鈥?
xy=(t, np.sin(t)), xycoords=鈥榙ata鈥?
xytext=(+10, +30), textcoords=鈥榦ffset points鈥? fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plot([t,t],[0,np.sin(t)], color =鈥榬ed鈥? linewidth=2.5, linestyle="--")
scatter([t,],[np.sin(t),], 50, color =鈥榬ed鈥?
annotate(r鈥?cos(frac{2pi}{3})=-frac{1}{2}$鈥?
xy=(t, np.cos(t)), xycoords=鈥榙ata鈥?
xytext=(-90, -50), textcoords=鈥榦ffset points鈥? fontsize=16,
arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.subplot()
plt.subplot(2,3,1)
琛ㄧず鎶婂浘鏍囧垎鍓叉垚2*3鐨勭綉鏍笺€備篃鍙互绠€鍐?code>plt.subplot(231)
銆傚叾涓紝绗竴涓弬鏁版槸琛屾暟锛岀浜屼釜鍙傛暟鏄垪鏁帮紝绗笁涓弬鏁拌〃绀哄浘褰㈢殑鏍囧彿銆?/p>
plt.axes()
鎴戜滑鍏堟潵鐪嬩粈涔堟槸Figure鍜孉xes瀵硅薄銆傚湪matplotlib涓紝鏁翠釜鍥惧儚涓轰竴涓狥igure瀵硅薄銆傚湪Figure瀵硅薄涓彲浠ュ寘鍚竴涓紝鎴栬€呭涓狝xes瀵硅薄銆傛瘡涓狝xes瀵硅薄閮芥槸涓€涓嫢鏈夎嚜宸卞潗鏍囩郴缁熺殑缁樺浘鍖哄煙銆傚叾閫昏緫鍏崇郴濡備笅^3锛?br data-filtered="filtered">
- axes() by itself creates a default full subplot(111) window axis.
- axes(rect, axisbg=鈥檞鈥? where rect = [left, bottom, width, height] in normalized (0, 1) units. axisbg is the background color for the axis, default white.
- axes(h) where h is an axes instance makes h the current axis. An Axes instance is returned.
rect=[宸? 涓? 瀹? 楂榏 瑙勫畾鐨勭煩褰㈠尯鍩?rect鐭╁舰绠€鍐欙紝杩欓噷鐨勬暟鍊奸兘鏄互figure澶у皬涓烘瘮渚嬶紝鍥犳锛岃嫢鏄涓や釜axes骞舵帓鏄剧ず锛岄偅涔坅xes[2]鐨勫乏=axes[1].宸?axes[1].瀹斤紝杩欐牱axes[2]鎵嶄笉浼氬拰axes[1]閲嶅彔銆?/p>
show code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
http://matplotlib.org/examples/pylab_examples/axes_demo.html
import matplotlib.pyplot as plt
import numpy as np
# create some data to use for the plot
dt = 0.001
t = np.arange(0.0, 10.0, dt)
r = np.exp(-t[:1000]/0.05) # impulse response
x = np.random.randn(len(t))
s = np.convolve(x, r)[:len(x)]*dt # colored noise
# the main axes is subplot(111) by default
plt.plot(t, s)
plt.axis([0, 1, 1.1*np.amin(s), 2*np.amax(s)])
plt.xlabel(鈥榯ime (s)鈥?
plt.ylabel(鈥榗urrent (nA)鈥?
plt.title(鈥楪aussian colored noise鈥?
# this is an inset axes over the main axes
a = plt.axes([.65, .6, .2, .2], axisbg=鈥榶鈥?
n, bins, patches = plt.hist(s, 400, normed=1)
plt.title(鈥楶robability鈥?
plt.xticks([])
plt.yticks([])
# this is another inset axes over the main axes
a = plt.axes([0.2, 0.6, .2, .2], axisbg=鈥榶鈥?
plt.plot(t[:len(r)], r)
plt.title(鈥業mpulse response鈥?
plt.xlim(0, 0.2)
plt.xticks([])
plt.yticks([])
plt.show()
pyplot.pie鍙傛暟
colors棰滆壊
鎵惧嚭matpltlib.pyplot.plot涓殑colors鍙互鍙栧摢浜涘€硷紵
- so-Named colors in matplotlib
- CSDN-matplotlib瀛︿範涔嬶紙鍥涳級璁剧疆绾挎潯棰滆壊銆佸舰鐘?/a>
1
2
for name,hex in matplotlib.colors.cnames.iteritems():
print name,hex
鎵撳嵃棰滆壊鍊煎拰瀵瑰簲鐨凴GB鍊笺€?/p>
plt.axis(鈥榚qual鈥?
閬垮厤姣斾緥鍘嬬缉涓烘き鍦?/li>
以上是关于鏂囨憳锛歮atplotlib缁樺浘鍙鍖栫煡璇嗙偣鏁寸悊的主要内容,如果未能解决你的问题,请参考以下文章