鏂囨憳锛歮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].

    鎶€鏈垎浜浘鐗? src=

    鍥剧墖鏉ヨ嚜:缁樺浘: 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)

    鎶€鏈垎浜浘鐗? src=

    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()

     

    鎶€鏈垎浜浘鐗? src=

    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">

     

    鎶€鏈垎浜浘鐗? src=

    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()

    鎶€鏈垎浜浘鐗? src=
    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()

    鎶€鏈垎浜浘鐗? src=
    [^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()

    鎶€鏈垎浜浘鐗? src=
    褰撴垜浠缃鍙风殑鏃跺€欙紝鎴戜滑鍙互鍚屾椂璁剧疆璁板彿鐨勬爣绛俱€傛敞鎰忚繖閲屼娇鐢ㄤ簡 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鈥?

    鎶€鏈垎浜浘鐗? src=

    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()

    鎶€鏈垎浜浘鐗? src=

    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"))

     

    鎶€鏈垎浜浘鐗? src=

    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">鎶€鏈垎浜浘鐗? src=

    plt.axes-瀹樻柟鏂囨。

    • 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()

     

    鎶€鏈垎浜浘鐗? src=

    [^3]:缁樺浘: matplotlib鏍稿績鍓栨瀽

    pyplot.pie鍙傛暟

    colors棰滆壊

    鎵惧嚭matpltlib.pyplot.plot涓殑colors鍙互鍙栧摢浜涘€硷紵

    鎵撳嵃棰滆壊鍊煎拰瀵瑰簲鐨凴GB鍊笺€?/p>

    • plt.axis(鈥榚qual鈥?閬垮厤姣斾緥鍘嬬缉涓烘き鍦?/li>








































































































































    以上是关于鏂囨憳锛歮atplotlib缁樺浘鍙鍖栫煡璇嗙偣鏁寸悊的主要内容,如果未能解决你的问题,请参考以下文章

    鏁版嵁搴撶煡璇嗙偣

    涓€闃舵鈥斺€旂煡璇嗙偣鏁村悎

    鍏充簬姊害鐨勪竴浜涚煡璇嗙偣

    閫氳繃杩?2寮犳墜缁樺浘锛屾悶鎳備粈涔堟槸寰湇鍔℃灦鏋?/h1>

    ksar銆乻ar鍙婄浉鍏冲唴鏍哥煡璇嗙偣瑙f瀽

    鏄撻敊鏄撴贩娣嗙煡璇嗙偣璁茶В绯诲垪锛堜節锛夆€斿缓绔嬪姩鎬佽鍒掓ā鍨嬬殑鍩烘湰姝ラ

    (c)2006-2024 SYSTEM All Rights Reserved IT常识