数据科学 : matplotlib 入门

matplotlib库python做数据可视化很重要的一个库,虽然现在有了很多新的库比如说:seaborn ploty D3js 等,但是了解matplotlib对你掌握其他的可视化库很有帮助。现在我就来用代码讲解matplotlib。

描线 plt.plot
%matplotlib inline  #我用的是jupyter notebook  所以要加这个才能在网页里面显示
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数据集
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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容