1. 程式人生 > 實用技巧 >25、matplot 的基礎介紹1:圖表元素

25、matplot 的基礎介紹1:圖表元素

matoplotlib基礎介紹

介紹內容整理資料來源與:《python資料視覺化之matplolib實踐:作者 劉大成》

一個圖表正常該有元素

plot函式

折線圖,表示資料的變化趨勢

plt.plot(x,y,ls="-",lw=2,label="plot figure")

ls : 表示折線的風格

lw:表示折線的寬度

label:表示折線的標籤

scatter 函式

散點圖: 兩個變數,或者三個變數之間的關係變化

plt.scatter(x,y1,c="b",label="scatter figure")

c : color 表示其顏色

xlim 函式

設定x軸數值的顯示範圍

plt.xlim(xmin,xmax)

xlabel 函式

設定x軸的標籤文字

ylabel() 可以是設定y軸,用法相同

plt.xlabel(string)

grid 函式

繪製刻度線的網格線

plt.grid(linestyle=":",color="r")

axhline 函式

繪製平行於x軸的水平參考線

axvline() 繪製平行於 y 軸的水平參考線

plt.axhline(y=0.0,c="r",ls="--",lw=2)

y : 水平參考線的出發點

axvspan 函式

繪製垂直於x軸的參考區域

plt.axvspan(xmin=1.0,xmax=2.0,facecolor="y",alpha=0.3)

● xmin:參考區域的起始位置。

● xmax:參考區域的終止位置。

● facecolor:參考區域的填充顏色。

● alpha:參考區域的填充顏色的透明度。

同理: axhspan() 函式 繪製垂直於Y軸的參考區域

annotate 函式

新增圖形內容細節的指向型註釋文字

plt.annotate(
    string,
    xy=( , ),   # 這是一個元組
    xytext=( , ), # 同上
    weight="bold",
    color="b",
    arrowprops=dict( arrowstyle="->", connectionstyle="arc3", color="b")
)

● string:圖形內容的註釋文字。

● xy:被註釋圖形內容的位置座標。

● xytext:註釋文字的位置座標。

● weight:註釋文字的字型粗細風格。

● color:註釋文字的字型顏色。

● arrowprops:指示被註釋內容的箭頭的屬性字典。

text 函式

新增圖形內容細節的無指向型註釋文字

plt.text(x,y,string,weight="bold",color="b")

title 函式

新增圖形內容的標題

plt.title(string)

legend 函式

標示不同圖形的文字標籤圖例

plt.legend(loc="lower left")

loc:圖例在圖中的地理位置