一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python matplotlib中的subplot函數使用詳解

python matplotlib中的subplot函數使用詳解

2020-04-13 10:03科研路上的小曹 Python

今天小編就為大家分享一篇python matplotlib中的subplot函數使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

python里面的matplotlib.pylot是大家比較常用的,功能也還不錯的一個包。基本框架比較簡單,但是做一個功能完善且比較好看整潔的圖,免不了要網上查找一些函數。于是,為了節省時間,可以一勞永逸。我把常用函數作了一個總結,最后寫了一個例子,以后基本不用怎么改了。

一、作圖流程:

1.準備數據, , 3作圖, 4定制, 5保存, 6顯示

1.數據可以是numpy數組,也可以是list

2創建畫布:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt
#figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
 
#num:圖像編號或名稱,數字為編號 ,字符串為名稱
#figsize:指定figure的寬和高,單位為英寸;
#dpi參數指定繪圖對象的分辨率,即每英寸多少個像素,缺省值為80 ,1英寸等于2.5cm,A4紙是 21*30cm的紙張
#facecolor:背景顏色
#edgecolor:邊框顏色
#frameon:是否顯示邊
 
fig = plt.figure()
fig = plt.figure(figsize=(8,6), dpi=80)
 
fig.add_axes()
fig, axes = plt.subplos(nrows = 2, ncols = 2) #axes是長度為4的列表

3、作圖路線

一維數據:

?
1
2
3
4
5
6
7
8
9
10
11
axes[0, 0].plot(x, y)
axes[0,1].bar([1,2,3], [2,4,8])
axes[0,2].barh([1,2,3], [2,4,8])
axes[1,0].axhline(0.45)
axes[1, 1].scatter(x, y)
axes[1,2].axvline(0.65)
axes[2,0].fill(x,y, color = 'blue')
axes[2,1].fill_between(x,y, color = 'blue')
axes[2,2].violinplot(y)
axes[0,3].arrow(0,0,0.5,0.5)
axes[1,3].quiver(x,y)

4, 定制

?
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
36
37
38
39
40
41
42
43
44
45
46
47
plt.plot(x,y, alpha=0.4, c = 'blue', maker = 'o')
#顏色,標記,透明度
 
# 顯示數學文本
 
t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)
 
plt.plot(t,s)
plt.title(r'$\alpha_i > \beta_i$', fontsize=20)
plt.text(1, -0.6, r'$\sum_{i=0}^\infty x_i$', fontsize=20)
plt.text(0.6, 0.6, r'$\mathcal{A}\mathrm{sin}(2 \omega t)$',
     fontsize=20)
plt.xlabel('time (s)')
plt.ylabel('volts (mV)')
 
fig = plt.figure()
fig.suptitle('bold figure suptitle', fontsize=14, fontweight='bold')
 
ax = fig.add_subplot(111)
fig.subplots_adjust(top=0.85)
ax.set_title('axes title')
 
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
 
ax.text(3, 8, 'boxed italics text in data coords', style='italic',
    bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
 
ax.text(2, 6, r'an equation: $E=mc^2$', fontsize=15)
 
ax.text(3, 2, u'unicode: Institut f\374r Festk\366rperphysik')
 
ax.text(0.95, 0.01, 'colored text in axes coords',
    verticalalignment='bottom', horizontalalignment='right',
    transform=ax.transAxes,
    color='green', fontsize=15)
 
 
ax.plot([2], [1], 'o')
 
 
# 注釋
ax.annotate('我是注釋啦', xy=(2, 1), xytext=(3, 4),color='r',size=15,
      arrowprops=dict(facecolor='g', shrink=0.05))
 
ax.axis([0, 10, 0, 10])

python matplotlib中的subplot函數使用詳解

5, 保存顯示

?
1
2
3
plt.savefig("1.png")
plt.savefig("1.png", trainsparent =True)
plt.show()

二、部分函數使用詳解:

1, fig.add_subplot(numrows, numcols, fignum) ####三個參數,分別代表子圖的行數,列數,圖索引號。 eg: ax = fig.add_subplot(2, 3, 1) (or ,ax = fig.add_subplot(231))

2, plt.subplots()使用

?
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
x = np.linspace(0, 2*np.pi,400)
y = np.sin(x**2)
fig, ax = plt.subplots()
ax.plot(x, y)
ax.set_title('Simple plot')
 
# Creates two subplots and unpacks the output array immediately
#fig = plt.figure(figsize=(6,6))
f, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.plot(x, y)
ax1.set_title('Sharing Y axis')
ax2.scatter(x, y)
 
# Creates four polar axes, and accesses them through the returned array
fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))
axes[0, 0].plot(x, y)
axes[1, 1].scatter(x, y)
 
# Share a X axis with each column of subplots
plt.subplots(2, 2, sharex='col')
 
# Share a Y axis with each row of subplots
plt.subplots(2, 2, sharey='row')
 
# Share both X and Y axes with all subplots
plt.subplots(2, 2, sharex='all', sharey='all')
 
# Note that this is the same as
plt.subplots(2, 2, sharex=True, sharey=True)
 
# Creates figure number 10 with a single subplot
# and clears it if it already exists.
fig, ax=plt.subplots(num=10, clear=True)

3.plt.legend()

?
1
2
3
plt.legend(loc='String or Number', bbox_to_anchor=(num1, num2))
plt.legend(loc='upper center', bbox_to_anchor (0.6,0.95),ncol=3,fancybox=True,shadow=True)
#bbox_to_anchor被賦予的二元組中,第一個數值用于控制legend的左右移動,值越大越向右邊移動,第二個數值用于控制legend的上下移動,值越大,越向上移動

python matplotlib中的subplot函數使用詳解

以上這篇python matplotlib中的subplot函數使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/MCANDML/article/details/80554176

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久人妻熟女中文字幕AV蜜芽 | 欧美精品久久久亚洲 | 亚洲午夜精品久久久久 | 国产精品视频人人做人人爱 | 日本aa大片在线播放免费看 | 青青草一区二区免费精品 | www.av免费| 成人精品视频一区二区在线 | 美女扒开腿让男生桶爽漫画 | 糖心视频在线观看 | 91视频破解版 | 午夜国产在线视频 | 国产精品久久香蕉免费播放 | 98色花堂永久地址国产精品 | a人片| 国产四虎| 亚洲 欧美 中文 日韩 视频 | 2020韩国三级理论在线观看 | zzjj中国| 精品国产免费久久久久久婷婷 | 隔壁的漂亮邻居hd中文 | 亚洲社区在线 | 日韩视频一区 | 白丝校花好湿好紧 | 桃乃木香奈作品在线观看 | 国产成人综合手机在线播放 | 99久久精品免费看国产四区 | 男女发生性关系视频 | 久久综合狠狠综合狠狠 | 国产精品日本一区二区三区在线看 | 俄罗斯处女摘花 | 日韩欧美中文字幕一区二区三区 | 四虎音影 | 操日日 | 亚洲精品一区二区三区在线播放 | 青青青久热国产精品视频 | 国产成人性毛片aaww | 国产嘿咻| 欧美激情影音先锋 | 亚洲AV蜜桃永久无码精品红樱桃 | 日本一卡二卡3卡四卡无卡网址 |