python使用matplotlib繪製折線圖教程

來源:互聯網
上載者:User
Matplotlib是一個Python工具箱,用於科學計算的資料視覺效果。藉助它,Python可以繪製如Matlab和Octave多種多樣的資料圖形。下面這篇文章主要介紹了python使用matplotlib如何繪製折線圖的方法教程,需要的朋友可以參考借鑒。

matplotlib簡介

matplotlib 是python最著名的繪圖庫,它提供了一整套和matlab相似的命令API,十分適合互動式地行製圖。而且也可以方便地將它作為繪圖控制項,嵌入GUI應用程式中。

它的文檔相當完備,並且Gallery頁面中有上百幅縮圖,開啟之後都有來源程式。因此如果你需要繪製某種類型的圖,只需要在這個頁面中瀏覽/複製/粘貼一下,基本上都能搞定。

在Linux下比較著名的資料圖工具還有gnuplot,這個是免費的,Python有一個包可以調用gnuplot,但是文法比較不習慣,而且畫圖品質不高。

而 Matplotlib則比較強:Matlab的文法、python語言、latex的畫圖品質(還可以使用內嵌的latex引擎繪製的數學公式)。

繪圖庫Matplotlib的安裝方法:點擊這裡

matplotlib繪製折線圖

1. line chart

import numpy as npimport matplotlib.pyplot as pltx = 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的時候指定label,然後調用legend方法可以繪製圖例。例如:

import numpy as npimport matplotlib.pyplot as pltx = 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方法可接受一個loc關鍵字參數來設定圖例的位置,可取值為數字或字串:

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方法的關鍵字參數color(或c)用來設定線的顏色。可取值為:

1、顏色名稱或簡寫

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]之間的浮點數的字串形式,表示灰階值。0表示黑色,1表示白色

(2)樣式

plot方法的關鍵字參數linestyle(或ls)用來設定線的樣式。可取值為:

  • -, solid

  • --, dashed

  • -., dashdot

  • :, dotted

  • '', ' ', None

(3)粗細

設定plot方法的關鍵字參數linewidth(或lw)可以改變線的粗細,其值為浮點數。

import numpy as npimport matplotlib.pyplot as pltx = 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

以下關鍵字參數可以用來設定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 npimport matplotlib.pyplot as pltx = 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關鍵字參數可以和color以及linestyle這兩個關鍵字參數合并為一個字串。例如:

import numpy as npimport matplotlib.pyplot as pltx = 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()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.