【数据分析与可视化】Matplotlib简单绘图之plot

了解

matlab

常用指令

在这里插入图片描述

常用函数

绘图在一个figure里面

在这里插入图片描述

在这里插入图片描述

上手操作

数据可以只有一组数据
两组数据必须长度一致

plt.show()

以前直接plot()无法显示图像
必须调用 plt.show(),因此需引入%matplotlib inline,使plt.plot(a)直接显示图像
现在不需要这么麻烦

import numpy as np
import matplotlib.pyplot as plt
# jupyter的魔法函数
%matplotlib inline
# 其他魔法函数-代码执行时间
%timeit np.arange(10)
589 ns ± 17.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
# 默认一条数据的充当 y坐标(x默认从0开始填充横坐标)
a = [1, 2, 3]
plt.plot(a)
[<matplotlib.lines.Line2D at 0x11cfe3ad0>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-60DhwDDV-1587630665365)(output_4_1.png)]

# 传入第二组数值为 y坐标 
b = [4, 5, 6]
plt.plot(a, b)
[<matplotlib.lines.Line2D at 0x11e87bed0>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H0LlUqO1-1587630665370)(output_5_1.png)]

扫描二维码关注公众号,回复: 11160984 查看本文章
# 两组值必须使xy对应
b = [4, 5, 6, 7]
plt.plot(a,b)
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-9-394387af9449> in <module>
      1 b = [4, 5, 6, 7]
----> 2 plt.plot(a,b)


~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
   2793     return gca().plot(
   2794         *args, scalex=scalex, scaley=scaley, **({"data": data} if data
-> 2795         is not None else {}), **kwargs)
   2796 
   2797 


~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1664         """
   1665         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D._alias_map)
-> 1666         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1667         for line in lines:
   1668             self.add_line(line)


~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py in __call__(self, *args, **kwargs)
    223                 this += args[0],
    224                 args = args[1:]
--> 225             yield from self._plot_args(this, kwargs)
    226 
    227     def get_next_color(self):


~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    389             x, y = index_of(tup[-1])
    390 
--> 391         x, y = self._xy_from_xy(x, y)
    392 
    393         if self.command == 'plot':


~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/axes/_base.py in _xy_from_xy(self, x, y)
    268         if x.shape[0] != y.shape[0]:
    269             raise ValueError("x and y must have same first dimension, but "
--> 270                              "have shapes {} and {}".format(x.shape, y.shape))
    271         if x.ndim > 2 or y.ndim > 2:
    272             raise ValueError("x and y can be no greater than 2-D, but have "


ValueError: x and y must have same first dimension, but have shapes (3,) and (4,)

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qRuhRHck-1587630665373)(output_6_1.png)]

# 看参数-定制风格(r红色*状)
b = [4, 5, 6]
plt.plot(a, b, 'r*')
[<matplotlib.lines.Line2D at 0x11f1e9650>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7wp57UMI-1587630665379)(output_7_1.png)]

# 两组数据
c = [2,5,1]
d = [3, 4, 6]
plt.plot(a, b, 'r--', c, d, 'b-*')
[<matplotlib.lines.Line2D at 0x11ed65b50>,
 <matplotlib.lines.Line2D at 0x11ed65390>]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H00q27TM-1587630665385)(output_8_1.png)]

# np生成连续数据,方便查看
t = np.arange(0.0, 2.0, 0.1)
t
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
       1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])
t.size
20
s = np.sin(t*np.pi)
s
array([ 0.00000000e+00,  3.09016994e-01,  5.87785252e-01,  8.09016994e-01,
        9.51056516e-01,  1.00000000e+00,  9.51056516e-01,  8.09016994e-01,
        5.87785252e-01,  3.09016994e-01,  1.22464680e-16, -3.09016994e-01,
       -5.87785252e-01, -8.09016994e-01, -9.51056516e-01, -1.00000000e+00,
       -9.51056516e-01, -8.09016994e-01, -5.87785252e-01, -3.09016994e-01])
s.size
20
plt.plot(t, s, 'r--', label='aaa')
plt.plot(t*2, s, 'b--',label='bb')
plt.xlabel('this is x')
plt.ylabel('this is y')
plt.title('this is a demo')
# 显示设置的label样例
plt.legend()
<matplotlib.legend.Legend at 0x121e0e510>

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8gNU4owm-1587630665386)(output_13_1.png)]

原创文章 257 获赞 187 访问量 16万+

猜你喜欢

转载自blog.csdn.net/weixin_43469680/article/details/105708139