4.Pyplot数据快查表

Pyplot Tutorial

import matplotlib.pyplot as plt
import numpy as np 

基本绘图

首先生成数据集

#平均采样,50个点
x=np.linspace(-3,3,50)
x.shape
(50,)
y1=2*x+1
y1
array([-5.        , -4.75510204, -4.51020408, -4.26530612, -4.02040816,
       -3.7755102 , -3.53061224, -3.28571429, -3.04081633, -2.79591837,
       -2.55102041, -2.30612245, -2.06122449, -1.81632653, -1.57142857,
       -1.32653061, -1.08163265, -0.83673469, -0.59183673, -0.34693878,
       -0.10204082,  0.14285714,  0.3877551 ,  0.63265306,  0.87755102,
        1.12244898,  1.36734694,  1.6122449 ,  1.85714286,  2.10204082,
        2.34693878,  2.59183673,  2.83673469,  3.08163265,  3.32653061,
        3.57142857,  3.81632653,  4.06122449,  4.30612245,  4.55102041,
        4.79591837,  5.04081633,  5.28571429,  5.53061224,  5.7755102 ,
        6.02040816,  6.26530612,  6.51020408,  6.75510204,  7.        ])
y2=x**2
y2
array([9.00000000e+00, 8.28029988e+00, 7.59058726e+00, 6.93086214e+00,
       6.30112453e+00, 5.70137443e+00, 5.13161183e+00, 4.59183673e+00,
       4.08204915e+00, 3.60224906e+00, 3.15243648e+00, 2.73261141e+00,
       2.34277384e+00, 1.98292378e+00, 1.65306122e+00, 1.35318617e+00,
       1.08329863e+00, 8.43398584e-01, 6.33486047e-01, 4.53561016e-01,
       3.03623490e-01, 1.83673469e-01, 9.37109538e-02, 3.37359434e-02,
       3.74843815e-03, 3.74843815e-03, 3.37359434e-02, 9.37109538e-02,
       1.83673469e-01, 3.03623490e-01, 4.53561016e-01, 6.33486047e-01,
       8.43398584e-01, 1.08329863e+00, 1.35318617e+00, 1.65306122e+00,
       1.98292378e+00, 2.34277384e+00, 2.73261141e+00, 3.15243648e+00,
       3.60224906e+00, 4.08204915e+00, 4.59183673e+00, 5.13161183e+00,
       5.70137443e+00, 6.30112453e+00, 6.93086214e+00, 7.59058726e+00,
       8.28029988e+00, 9.00000000e+00])
plt.plot(x,y1)
[<matplotlib.lines.Line2D at 0x116754eb8>]

在这里插入图片描述

plt.plot(x,y2)
[<matplotlib.lines.Line2D at 0x1167fe668>]

在这里插入图片描述

plt.plot(x,y1,y2)#错误
[<matplotlib.lines.Line2D at 0x116885668>,
 <matplotlib.lines.Line2D at 0x1168857b8>]

在这里插入图片描述

plt.plot(x,y1)
plt.plot(x,y2)
[<matplotlib.lines.Line2D at 0x116851390>]

在这里插入图片描述

plt.plot(x,y1)
plt.plot(x,y2,color='r', linewidth=2.0, linestyle='-.')#'-','-.',':'
[<matplotlib.lines.Line2D at 0x1168c1d30>]

在这里插入图片描述

plt.plot(x,y2,'-.b')
[<matplotlib.lines.Line2D at 0x116d61b00>]

在这里插入图片描述

折线图

Label标记

plt.plot([1,2,3,4],[2,3,3,3])
plt.ylabel("Some Num")
plt.xlabel('自变量')
Text(0.5,0,'自变量')

在这里插入图片描述

支持中文

设置字体

import matplotlib as mpl
plt.rcParams['font.family']='SimHei'
plt.rcParams['font.size']=14
plt.plot([1,2,3,4],[2,3,3,3])
plt.ylabel("Some Num")
plt.xlabel('自变量')
Text(0.5,0,'自变量')

在这里插入图片描述

散点图

多一个标记

plt.plot([1,2,3,4],[2,3,3,3],'bs')
[<matplotlib.lines.Line2D at 0x117384c50>]

在这里插入图片描述

linestyle:

ro—红色的圆点

bs—蓝色的方块

g^—绿色的三角

plt.plot([1,2,3,4],[2,3,3,3],'g^')
[<matplotlib.lines.Line2D at 0x11745d080>]

在这里插入图片描述

t=np.linspace(-5,5,100)
plt.plot(t,t**2)
plt.plot(t,t**5)
[<matplotlib.lines.Line2D at 0x117537400>]

在这里插入图片描述

#多个函数图,可以合并成为一个函数
#但必须要求(自变量,因变量,style字段)
plt.plot(t,t**2,'r--',t,t**5,'y-.')
[<matplotlib.lines.Line2D at 0x11771e400>,
 <matplotlib.lines.Line2D at 0x11771e5c0>]

在这里插入图片描述

结构化数据绘制散点图

原始2个类型之间比较,即可是离散型,也可是连续型的

np.arange(50)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49])
data={
    'a':np.arange(50),
    'c':np.random.randint(0,50,50),
    'd':np.random.rand(50)
}
data
{'a': array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
        34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]),
 'c': array([19, 47, 35, 40, 27,  0, 26, 40, 45, 46, 45,  1, 32, 40, 41,  9, 30,
         8, 20, 18, 37, 33, 45, 12, 20, 18, 34,  8, 31, 10, 43, 21,  8, 17,
         6, 30, 12, 22, 33, 33, 20, 12, 49, 20, 27,  7, 34, 48, 43, 26]),
 'd': array([0.23302494, 0.26576222, 0.92329008, 0.01753795, 0.54966465,
        0.62221278, 0.37085458, 0.05321827, 0.18675867, 0.87818805,
        0.3616857 , 0.84886774, 0.08076974, 0.85557681, 0.71760161,
        0.49695615, 0.23777212, 0.7127281 , 0.12939015, 0.60372764,
        0.05818357, 0.44025726, 0.66788303, 0.42333529, 0.87269427,
        0.77808172, 0.63611593, 0.58142241, 0.3561951 , 0.53839356,
        0.13119696, 0.06114425, 0.70134339, 0.37432878, 0.01565333,
        0.70248966, 0.2818261 , 0.28463553, 0.44851051, 0.5388553 ,
        0.48992475, 0.7822408 , 0.61147766, 0.74086037, 0.57496588,
        0.14292354, 0.49343722, 0.14274424, 0.84597549, 0.60159599])}

plt.scatter()

plt.scatter('a','d',data=data)
plt.xlabel('a 数据')
plt.ylabel('d 数据')
Text(0,0.5,'d 数据')

在这里插入图片描述

data['b']=np.abs(data['d'])
#散点图的样式
plt.scatter('a','b',data=data, c='r', marker='>')
<matplotlib.collections.PathCollection at 0x117cba1d0>

在这里插入图片描述

# c颜色 color
plt.scatter('c','d',data=data,c='c',marker='>')
<matplotlib.collections.PathCollection at 0x117e44c18>

在这里插入图片描述

柱状图绘制

names=['A类型','B类型','C类型']
value=[1,10,100]
plt.bar(range(len(names)),value)
plt.xticks(range(len(names)),names) #刻度
([<matplotlib.axis.XTick at 0x1180c30b8>,
  <matplotlib.axis.XTick at 0x1180b5a90>,
  <matplotlib.axis.XTick at 0x118139e10>],
 <a list of 3 Text xticklabel objects>)

在这里插入图片描述

plt.scatter(names,value)
<matplotlib.collections.PathCollection at 0x1171826d8>

在这里插入图片描述

plt.scatter(range(len(names)),value)
plt.xticks(range(len(names)),names)
plt.title('离散数据散点图')
Text(0.5,1,'离散数据散点图')

在这里插入图片描述

子图-SubPlot

1.将一个画布进行切分(Figure)

2.将切分后的图分配到固定的位置

3.将图可以设置成固定的大小

栅格系统,数字确定它的位置

plt.figure(1)
plt.subplot(131)
plt.bar(names,value,color='r')
plt.subplot(236)
plt.scatter(names,value,color='y')
plt.subplot(333)
plt.plot(names,value,color='g')
plt.title('离散数据的柱状图、散点图、折线图')
Text(0.5,1,'离散数据的柱状图、散点图、折线图')

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值