matplotlib庫python做數(shù)據(jù)可視化很重要的一個庫,雖然現(xiàn)在有了很多新的庫比如說:seaborn ploty D3js 等驾茴,但是了解matplotlib對你掌握其他的可視化庫很有幫助〉瑁現(xiàn)在我就來用代碼講解matplotlib樟氢。
描線 plt.plot
%matplotlib inline #我用的是jupyter notebook 所以要加這個才能在網(wǎng)頁里面顯示
import matplotlib as mlp #國際慣例縮寫
import matplotlib.pyplot as plt #國際慣例縮寫
import numpy as np #國際慣例縮寫
import pandas as pd #國際慣例縮寫
plt.style.use('seaborn-whitegrid') #設置風格
plt.figure() #定義一塊畫布
x = np.linspace(0, 100, 10)
plt.plot(x, x + 0 ,'-b',label='0') # solid green #單引號里面可以直接設置線的形狀跟顏色
plt.plot(x, x + 1, '--c',label='1') # dashed cyan
plt.plot(x, x + 2, '-.k',label='2') # dashdot black
plt.plot(x, x + 3, ':r',label='3'); # dotted red
plt.axis([0,10,0,10]) #設置x軸 y軸上下限
plt.title("demo1") #圖例的標題名
plt.xlabel("x") #坐標軸x軸名
plt.ylabel("y") #坐標軸y軸名
plt.legend() #顯示標記
生成圖如下
demo1
繪點 plt.scatter
from sklearn.datasets import load_iris
iris = load_iris() #iris數(shù)據(jù)集
features = iris.data.T
plt.scatter(features[0], features[1], alpha=0.5,
c=iris.target, cmap='viridis') #alpha控制顏色顯示的亮度(0-1),c控制顏色 吆倦,cmap控制顏色對比
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[1])
生成圖如下
demo2