机器学习matplotlib数据可视化常用用法

matplotlib基础语法

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(-10,10,100)
x
array([-10.        ,  -9.7979798 ,  -9.5959596 ,  -9.39393939,
        -9.19191919,  -8.98989899,  -8.78787879,  -8.58585859,
        -8.38383838,  -8.18181818,  -7.97979798,  -7.77777778,
        -7.57575758,  -7.37373737,  -7.17171717,  -6.96969697,
        -6.76767677,  -6.56565657,  -6.36363636,  -6.16161616,
        -5.95959596,  -5.75757576,  -5.55555556,  -5.35353535,
        -5.15151515,  -4.94949495,  -4.74747475,  -4.54545455,
        -4.34343434,  -4.14141414,  -3.93939394,  -3.73737374,
        -3.53535354,  -3.33333333,  -3.13131313,  -2.92929293,
        -2.72727273,  -2.52525253,  -2.32323232,  -2.12121212,
        -1.91919192,  -1.71717172,  -1.51515152,  -1.31313131,
        -1.11111111,  -0.90909091,  -0.70707071,  -0.50505051,
        -0.3030303 ,  -0.1010101 ,   0.1010101 ,   0.3030303 ,
         0.50505051,   0.70707071,   0.90909091,   1.11111111,
         1.31313131,   1.51515152,   1.71717172,   1.91919192,
         2.12121212,   2.32323232,   2.52525253,   2.72727273,
         2.92929293,   3.13131313,   3.33333333,   3.53535354,
         3.73737374,   3.93939394,   4.14141414,   4.34343434,
         4.54545455,   4.74747475,   4.94949495,   5.15151515,
         5.35353535,   5.55555556,   5.75757576,   5.95959596,
         6.16161616,   6.36363636,   6.56565657,   6.76767677,
         6.96969697,   7.17171717,   7.37373737,   7.57575758,
         7.77777778,   7.97979798,   8.18181818,   8.38383838,
         8.58585859,   8.78787879,   8.98989899,   9.19191919,
         9.39393939,   9.5959596 ,   9.7979798 ,  10.        ])

1.绘制连续曲线

y=x**2
y 
array([1.00000000e+02, 9.60004081e+01, 9.20824406e+01, 8.82460973e+01,
       8.44913784e+01, 8.08182838e+01, 7.72268136e+01, 7.37169677e+01,
       7.02887460e+01, 6.69421488e+01, 6.36771758e+01, 6.04938272e+01,
       5.73921028e+01, 5.43720029e+01, 5.14335272e+01, 4.85766758e+01,
       4.58014488e+01, 4.31078461e+01, 4.04958678e+01, 3.79655137e+01,
       3.55167840e+01, 3.31496786e+01, 3.08641975e+01, 2.86603408e+01,
       2.65381084e+01, 2.44975003e+01, 2.25385165e+01, 2.06611570e+01,
       1.88654219e+01, 1.71513111e+01, 1.55188246e+01, 1.39679625e+01,
       1.24987246e+01, 1.11111111e+01, 9.80512193e+00, 8.58075707e+00,
       7.43801653e+00, 6.37690032e+00, 5.39740843e+00, 4.49954086e+00,
       3.68329762e+00, 2.94867871e+00, 2.29568411e+00, 1.72431385e+00,
       1.23456790e+00, 8.26446281e-01, 4.99948985e-01, 2.55076013e-01,
       9.18273646e-02, 1.02030405e-02, 1.02030405e-02, 9.18273646e-02,
       2.55076013e-01, 4.99948985e-01, 8.26446281e-01, 1.23456790e+00,
       1.72431385e+00, 2.29568411e+00, 2.94867871e+00, 3.68329762e+00,
       4.49954086e+00, 5.39740843e+00, 6.37690032e+00, 7.43801653e+00,
       8.58075707e+00, 9.80512193e+00, 1.11111111e+01, 1.24987246e+01,
       1.39679625e+01, 1.55188246e+01, 1.71513111e+01, 1.88654219e+01,
       2.06611570e+01, 2.25385165e+01, 2.44975003e+01, 2.65381084e+01,
       2.86603408e+01, 3.08641975e+01, 3.31496786e+01, 3.55167840e+01,
       3.79655137e+01, 4.04958678e+01, 4.31078461e+01, 4.58014488e+01,
       4.85766758e+01, 5.14335272e+01, 5.43720029e+01, 5.73921028e+01,
       6.04938272e+01, 6.36771758e+01, 6.69421488e+01, 7.02887460e+01,
       7.37169677e+01, 7.72268136e+01, 8.08182838e+01, 8.44913784e+01,
       8.82460973e+01, 9.20824406e+01, 9.60004081e+01, 1.00000000e+02])
plt.plot(x,y) #绘制一条以x为横轴,y为纵轴的曲线
plt.show()

在这里插入图片描述

x = np.linspace(0,10,100)

y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x,y1)
plt.plot(x,y2)
plt.show()

在这里插入图片描述

plt.plot(x,y1,color = 'pink',linestyle=":")#可以指定线的颜色,还可以指定线的虚实
plt.plot(x,y2)

plt.xlim(-1,12)#指定坐标轴范围
plt.ylim(-2,2)

plt.show()

在这里插入图片描述

plt.plot(x,y1,color = 'pink',linestyle=":")#可以指定线的颜色,还可以指定线的虚实
plt.plot(x,y2)

plt.axis([-5,15,-2,2]) #第二种方法指定坐标值

plt.show()

在这里插入图片描述

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False

plt.plot(x,y1,color = 'pink',linestyle=":",label="woman") #指定每条线的标签
plt.plot(x,y2,label="man")

plt.xlabel("时间")#指定横纵坐标标签
plt.ylabel("两人心动值")

plt.legend()
plt.title("跌宕起伏")#设置图标标题
plt.show()

请添加图片描述

2.绘制离散曲线(用于x和y都是特征的情况,可以看到特征样本的分布)

plt.scatter(x,y1,color = 'pink')
plt.scatter(x,y2)
plt.show()

请添加图片描述

x = np.random.normal(0,1,10000) #符合正态分布的点
y = np.random.normal(0,1,10000)
plt.scatter(x,y,alpha=0.1)#设置不透明度分布看得更清晰
plt.show()

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值