示例:绘制正弦曲线、余弦曲线(及同一区域绘制多图)

思路:首先需要一系列的x轴坐标,可通过numpy中的arange()函数生成,例如从0到4,步长为0.02;然后借助numpy中的正弦、余弦函数对每个x坐标分别求值;最后根据x坐标和对应的y坐标画图。

In [42]:

import matplotlib.pyplot as plt 
import numpy as np

plt.rcParams['font.family'] ='kaiti'

x = np.arange(0,4,0.02) #从0-4中以0.02为等差的等差序列,生成x轴序列 
y_1 = np.sin(np.pi*x)+2 
y_2 = np.cos(np.pi*x) 
plt.plot(x,y_1,'mx') 
plt.plot(x,y_2,'r')
         
plt.legend(['正弦曲线','余弦曲线'])
plt.title('正余弦曲线')
plt.savefig('../R&Q_pic/test3',dpi=600)
plt.show()
C:\Users\Administrator\AppData\Local\Temp\ipykernel_4764\3258683806.py:14: UserWarning: Glyph 8722 (\N{MINUS SIGN}) missing from current font.
  plt.savefig('../R&Q_pic/test3',dpi=600)
C:\Users\Administrator\anaconda3\lib\site-packages\IPython\core\pylabtools.py:151: UserWarning: Glyph 8722 (\N{MINUS SIGN}) missing from current font.
  fig.canvas.print_figure(bytes_io, **kw)

示例:绘制正弦曲线、余弦曲线(及同一区域绘制多图)_第1张图片

In [49]:

#多区域绘图
import numpy as np
import matplotlib.pyplot as plt 

def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t) 
    
a=np.arange (0,5,0.02)
plt. subplot(322)    #创建多个子区域,3行2列,并将此图表放在第2区域 
plt.plot(a,f (a))

plt.subplot(323)
plt.plot(a,np.cos(2*np.pi*a),'-',color='r') 

plt.subplot(324)
plt.plot(a,np.sin(2*np.pi*a),'-',color='g') 

plt.subplot(325)
plt.plot(a,np.tan(2*np.pi*a),':',color='m') 

plt.savefig ('../R&Q_pic/test4',dpi=600) 
plt.show()
C:\Users\Administrator\AppData\Local\Temp\ipykernel_4764\731795327.py:21: UserWarning: Glyph 8722 (\N{MINUS SIGN}) missing from current font.
  plt.savefig ('../R&Q_pic/test4',dpi=600)

示例:绘制正弦曲线、余弦曲线(及同一区域绘制多图)_第2张图片

#同一绘图区域绘制多图
import matplotlib.pyplot as plt
import numpy as np 
a = np.arange(10)#创建一个0-9的整数序列,作为x坐标
plt.plot(a,a*1.5,'ro-',label = 'a') 
plt.plot(a,a*2.5, 'gx-',label = 'b') 
plt.plot(a,a*3.5, 'y*',label = 'c')
plt.plot(a,a*4.5,'bs-',label = 'd') 
plt.plot(a,a**2,'mH--',label = 'e')
plt.savefig('../R&Q_pic/test2',dpi=600)   #保存在指定的路径里面
plt.legend () 
plt.show ()

示例:绘制正弦曲线、余弦曲线(及同一区域绘制多图)_第3张图片

In [32]:

np.arange(10)

Out[32]:

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [33]:

np.arange(0,5,0.02)  #创建从0-5中以0.02为等差的等差序列

Out[33]:

array([0.  , 0.02, 0.04, 0.06, 0.08, 0.1 , 0.12, 0.14, 0.16, 0.18, 0.2 ,
       0.22, 0.24, 0.26, 0.28, 0.3 , 0.32, 0.34, 0.36, 0.38, 0.4 , 0.42,
       0.44, 0.46, 0.48, 0.5 , 0.52, 0.54, 0.56, 0.58, 0.6 , 0.62, 0.64,
       0.66, 0.68, 0.7 , 0.72, 0.74, 0.76, 0.78, 0.8 , 0.82, 0.84, 0.86,
       0.88, 0.9 , 0.92, 0.94, 0.96, 0.98, 1.  , 1.02, 1.04, 1.06, 1.08,
       1.1 , 1.12, 1.14, 1.16, 1.18, 1.2 , 1.22, 1.24, 1.26, 1.28, 1.3 ,
       1.32, 1.34, 1.36, 1.38, 1.4 , 1.42, 1.44, 1.46, 1.48, 1.5 , 1.52,
       1.54, 1.56, 1.58, 1.6 , 1.62, 1.64, 1.66, 1.68, 1.7 , 1.72, 1.74,
       1.76, 1.78, 1.8 , 1.82, 1.84, 1.86, 1.88, 1.9 , 1.92, 1.94, 1.96,
       1.98, 2.  , 2.02, 2.04, 2.06, 2.08, 2.1 , 2.12, 2.14, 2.16, 2.18,
       2.2 , 2.22, 2.24, 2.26, 2.28, 2.3 , 2.32, 2.34, 2.36, 2.38, 2.4 ,
       2.42, 2.44, 2.46, 2.48, 2.5 , 2.52, 2.54, 2.56, 2.58, 2.6 , 2.62,
       2.64, 2.66, 2.68, 2.7 , 2.72, 2.74, 2.76, 2.78, 2.8 , 2.82, 2.84,
       2.86, 2.88, 2.9 , 2.92, 2.94, 2.96, 2.98, 3.  , 3.02, 3.04, 3.06,
       3.08, 3.1 , 3.12, 3.14, 3.16, 3.18, 3.2 , 3.22, 3.24, 3.26, 3.28,
       3.3 , 3.32, 3.34, 3.36, 3.38, 3.4 , 3.42, 3.44, 3.46, 3.48, 3.5 ,
       3.52, 3.54, 3.56, 3.58, 3.6 , 3.62, 3.64, 3.66, 3.68, 3.7 , 3.72,
       3.74, 3.76, 3.78, 3.8 , 3.82, 3.84, 3.86, 3.88, 3.9 , 3.92, 3.94,
       3.96, 3.98, 4.  , 4.02, 4.04, 4.06, 4.08, 4.1 , 4.12, 4.14, 4.16,
       4.18, 4.2 , 4.22, 4.24, 4.26, 4.28, 4.3 , 4.32, 4.34, 4.36, 4.38,
       4.4 , 4.42, 4.44, 4.46, 4.48, 4.5 , 4.52, 4.54, 4.56, 4.58, 4.6 ,
       4.62, 4.64, 4.66, 4.68, 4.7 , 4.72, 4.74, 4.76, 4.78, 4.8 , 4.82,
       4.84, 4.86, 4.88, 4.9 , 4.92, 4.94, 4.96, 4.98])

In [36]:

np.pi

Out[36]:

3.141592653589793

In [39]:

np.random.rand(100)   #从0-1随机取100个值作为一位数组

Out[39]:

array([6.90385391e-01, 8.22879238e-01, 6.05341754e-01, 4.50783910e-01,
       8.36738379e-01, 1.53488645e-01, 9.98286782e-01, 4.39310211e-01,
       1.88044599e-01, 2.80110268e-01, 4.39217862e-04, 8.52119594e-01,
       4.36583005e-01, 5.08580780e-01, 7.41929307e-01, 7.79620382e-01,
       8.93598865e-01, 3.52869491e-01, 6.98635953e-01, 5.11315371e-01,
       2.57477752e-01, 1.04631042e-01, 9.45882320e-01, 1.00397418e-01,
       3.98366180e-02, 9.34589026e-03, 1.59603568e-01, 6.46269064e-01,
       4.93580997e-01, 6.02307171e-01, 6.12415328e-01, 9.61631798e-01,
       5.31597799e-02, 6.39875978e-01, 3.20423216e-01, 2.42010958e-01,
       9.16476772e-01, 1.96342108e-01, 1.32353216e-01, 5.80374832e-01,
       7.71115465e-01, 7.22203431e-01, 4.28297287e-01, 1.96589694e-01,
       9.44804563e-01, 8.14561165e-02, 7.80596905e-01, 1.91930814e-01,
       8.21369354e-01, 9.81945827e-01, 8.38280271e-01, 2.02715701e-01,
       6.30244232e-01, 8.47678521e-01, 9.78319311e-01, 7.11069019e-01,
       1.07173532e-02, 9.62737352e-01, 4.44465943e-01, 4.60223423e-01,
       1.83925374e-01, 9.81606244e-01, 5.92186913e-01, 1.18232854e-01,
       9.99574550e-01, 3.15071581e-01, 7.58756629e-01, 4.80356610e-01,
       6.36292303e-01, 7.84105625e-02, 2.89735305e-01, 5.67344317e-01,
       1.09122800e-01, 1.39315583e-02, 3.71536337e-01, 6.49739057e-01,
       8.71014245e-01, 4.54287828e-01, 9.10529043e-01, 3.41079879e-01,
       2.17658907e-01, 4.35144136e-01, 5.82262022e-01, 8.34418064e-01,
       9.09220165e-01, 7.68007562e-02, 9.24574776e-01, 9.09122952e-01,
       4.90731702e-01, 4.06648172e-01, 7.93381348e-01, 6.33977887e-02,
       9.69709332e-01, 7.94222110e-01, 3.09519754e-01, 5.37896370e-02,
       8.62316786e-01, 5.51177436e-02, 2.58748335e-01, 2.97458578e-01])

In [ ]:

np.random.randint(10,20,(5,6))    #从选定区域(10,20)随机生成

你可能感兴趣的:(笔记,python,pandas,数据分析,numpy)