美文网首页
数据科学 : matplotlib 入门

数据科学 : matplotlib 入门

作者: CV_lsc | 来源:发表于2017-09-03 21:12 被阅读0次

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

相关文章

网友评论

      本文标题:数据科学 : matplotlib 入门

      本文链接:https://www.haomeiwen.com/subject/huhkjxtx.html