亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python使用matplotlib繪制折線圖教程

 更新時(shí)間:2017年02月08日 09:36:18   投稿:daisy  
Matplotlib是一個(gè)Python工具箱,用于科學(xué)計(jì)算的數(shù)據(jù)可視化。借助它,Python可以繪制如Matlab和Octave多種多樣的數(shù)據(jù)圖形。下面這篇文章主要介紹了python使用matplotlib如何繪制折線圖的方法教程,需要的朋友可以參考借鑒。

matplotlib簡(jiǎn)介

matplotlib 是python最著名的繪圖庫(kù),它提供了一整套和matlab相似的命令A(yù)PI,十分適合交互式地行制圖。而且也可以方便地將它作為繪圖控件,嵌入GUI應(yīng)用程序中。

它的文檔相當(dāng)完備,并且Gallery頁(yè)面中有上百幅縮略圖,打開(kāi)之后都有源程序。因此如果你需要繪制某種類(lèi)型的圖,只需要在這個(gè)頁(yè)面中瀏覽/復(fù)制/粘貼一下,基本上都能搞定。

在Linux下比較著名的數(shù)據(jù)圖工具還有g(shù)nuplot,這個(gè)是免費(fèi)的,Python有一個(gè)包可以調(diào)用gnuplot,但是語(yǔ)法比較不習(xí)慣,而且畫(huà)圖質(zhì)量不高。

而 Matplotlib則比較強(qiáng):Matlab的語(yǔ)法、python語(yǔ)言、latex的畫(huà)圖質(zhì)量(還可以使用內(nèi)嵌的latex引擎繪制的數(shù)學(xué)公式)。

繪圖庫(kù)Matplotlib的安裝方法:點(diǎn)擊這里

matplotlib繪制折線圖

1. line chart

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 100)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1)
plt.plot(x, y2)

plt.title('line chart')
plt.xlabel('x')
plt.ylabel('y')

plt.show()

2. 圖例

在plot的時(shí)候指定label,然后調(diào)用legend方法可以繪制圖例。例如:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 100)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1, label='y = sin(x)')
plt.plot(x, y2, label='y = cos(x)')
plt.legend()
plt.show()


legend方法可接受一個(gè)loc關(guān)鍵字參數(shù)來(lái)設(shè)定圖例的位置,可取值為數(shù)字或字符串:

     0: ‘best'

     1: ‘upper right'

     2: ‘upper left'

     3: ‘lower left'

     4: ‘lower right'

     5: ‘right'

     6: ‘center left'

     7: ‘center right'

     8: ‘lower center'

     9: ‘upper center'

     10: ‘center'

3. 線的樣式

(1)顏色

plot方法的關(guān)鍵字參數(shù)color(或c)用來(lái)設(shè)置線的顏色。可取值為:

1、顏色名稱(chēng)或簡(jiǎn)寫(xiě)

     b: blue

     g: green

     r: red

     c: cyan

     m: magenta

     y: yellow

     k: black

     w: white

2、#rrggbb

3、(r, g, b) 或 (r, g, b, a),其中 r g b a 取均為[0, 1]之間

4、[0, 1]之間的浮點(diǎn)數(shù)的字符串形式,表示灰度值。0表示黑色,1表示白色

(2)樣式

plot方法的關(guān)鍵字參數(shù)linestyle(或ls)用來(lái)設(shè)置線的樣式??扇≈禐椋?/p>

  • -, solid
  • --, dashed
  • -., dashdot
  • :, dotted
  • '', ' ', None

(3)粗細(xì)

設(shè)置plot方法的關(guān)鍵字參數(shù)linewidth(或lw)可以改變線的粗細(xì),其值為浮點(diǎn)數(shù)。

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 100)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1, c='r', ls='--', lw=3)
plt.plot(x, y2, c='#526922', ls='-.')
plt.show()


4. marker

以下關(guān)鍵字參數(shù)可以用來(lái)設(shè)置marker的樣式:

  • marker
  • markeredgecolor 或 mec
  • markeredgewidth 或 mew
  • markerfacecolor 或 mfc
  • markerfacecoloralt 或 mfcalt
  • markersize 或 ms

其中marker可取值為:

  • '.': point marker
  • ',': pixel marker
  • 'o': circle marker
  • 'v': triangle_down marker
  • '^': triangle_up marker
  • '<': triangle_left marker
  • '>': triangle_right marker
  • '1': tri_down marker
  • '2': tri_up marker
  • '3': tri_left marker
  • '4': tri_right marker
  • 's': square marker
  • 'p': pentagon marker
  • '*': star marker
  • 'h': hexagon1 marker
  • 'H': hexagon2 marker
  • '+': plus marker
  • 'x': x marker
  • 'D': diamond marker
  • 'd': thin_diamond marker
  • '|': vline marker
  • '_': hline marker

例如:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 10)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1, marker='o', mec='r', mfc='w')
plt.plot(x, y2, marker='*', ms=10)
plt.show()


另外,marker關(guān)鍵字參數(shù)可以和color以及l(fā)inestyle這兩個(gè)關(guān)鍵字參數(shù)合并為一個(gè)字符串。例如:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 10)
y1, y2 = np.sin(x), np.cos(x)

plt.plot(x, y1, 'ro-')
plt.plot(x, y2, 'g*:', ms=10)
plt.show()

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論