【數據分析與可視化】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)]

# 兩組值必須使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)]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章