matplotlib的簡單使用
阿新 • • 發佈:2018-12-12
在python的統計,機器學習, 資料處理等領域, matplotlib都是很方便的資料視覺化工具。在本文記錄了一下其簡單的使用規則,方便自己查詢。
首先你的機器上需要按照好python, 並且假設讀者對Python語法有基本的理解
一般配合使用numpy,
- 按照matplotlib的方法 一般用pip或者anaconda安裝, 方法 pip install matplotlib或者 conda install matplotlib
- 用matplotlib繪製2d函式
需要匯入模組,然後呼叫plt.plot函式指定資料,調研plt.show()繪製圖形
import numpy as np import math import matplotlib.pyplot as plt e=math.e x=np.arange(-10,10) y1=np.log((1+e**(-x))) line = plt.plot(x,y1) plt.show()
繪製出來的圖如下 plot函式的引數為 資料, 樣式描述字串,資料,樣式... 這樣,可以同時畫幾個 資料為陣列,可以只指定y, 或者指定x, y 樣式描述了線條的顏色,形狀等等
plot(x, y) # plot x and y using default line style and color plot(x, y, 'bo') # plot x and y using blue circle markers plot(y) # plot y using x as index array 0..N-1 plot(y, 'r+') # ditto, but with red plusses
樣式描述是個字串,每個字元表示一種屬性,比如
character description '-'
solid line style '--'
dashed line style '-.'
dash-dot line style ':'
dotted line style '.'
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
比如character color ‘b’ blue ‘g’ green ‘r’ red ‘c’ cyan ‘m’ magenta ‘y’ yellow ‘k’ black ‘w’ white line = plt.plot(x,y1,"r-^")
產生的圖是這樣 plot()函式返回的是一個Line2D物件,可以通過這個物件設定更多屬性 比如
line = plt.plot(x,y1,"r-^") plt.setp(line, animated=True, color='g', ls='-.')
重新修改了顏色和style 能修改的屬性有
Property Value Type alpha float animated [True | False] antialiased or aa [True | False] clip_box a matplotlib.transform.Bbox instance clip_on [True | False] clip_path a Path instance and a Transform instance, a Patch color or c any matplotlib color contains the hit testing function dash_capstyle [ 'butt'
|'round'
|'projecting'
]dash_joinstyle [ 'miter'
|'round'
|'bevel'
]dashes sequence of on/off ink in points data (np.array xdata, np.array ydata) figure a matplotlib.figure.Figure instance label any string linestyle or ls [ '-'
|'--'
|'-.'
|':'
|'steps'
| ...]linewidth or lw float value in points lod [True | False] marker [ '+'
|','
|'.'
|'1'
|'2'
|'3'
|'4'
]markeredgecolor or mec any matplotlib color markeredgewidth or mew float value in points markerfacecolor or mfc any matplotlib color markersize or ms float markevery [ None | integer | (startind, stride) ] picker used in interactive line selection pickradius the line pick selection radius solid_capstyle [ 'butt'
|'round'
|'projecting'
]solid_joinstyle [ 'miter'
|'round'
|'bevel'
]transform a matplotlib.transforms.Transform instance visible [True | False] xdata np.array ydata np.array zorder any number - 指定文字,數軸等
指定座標軸文字一般是 plt.xlable(文字, 文字屬性) plt.ylable(文字,屬性等)
text()可以在任意位置顯示文字
title()可以指定標題
plt.title("csdn blog image 1.1") plt.xlabel("x value:[10,10]", color="red") plt.ylabel("y value log(sigmoid)", color="blue") plt.text(5, 6, r'$y_i=1/1+e^x_i$')
輸出圖形
- 用matplotlib繪製3d函式
一般主要使用庫 mpl_toolkits.mplot3d
需要首先拿到figure, 然後用Axes3D()生成3d的畫布,然後呼叫繪圖函式即可。對於曲面就是呼叫plot_surface
import numpy as np import math import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D e=math.e x=np.arange(-10,10) y=np.arange(-10,10) X,Y=np.meshgrid(x,y) Z=X**2+Y**2; fig = plt.figure() ax=Axes3D(fig) ax.plot_surface(X, Y, Z, cmap=plt.cm.winter) plt.show()
生成的圖如右圖