绘制能量衰减图
import matplotlib.pyplot as plt
import numpy as np
def f(t):
return np.exp(-t) * np.cos(2*np.pi*t)
a = np.arange(0.0, 5.0, 0.02)
plt.subplot(211)
plt.plot(a, f(a))
plt.subplot(212)
plt.plot(a, np.cos(2*np.pi*a), 'r--')
plt.show()
使用中文标签
python">import matplotlib.pyplot as plt
import numpy as np
import matplotlib
# 第一种方法:全局改变
matplotlib.rcParams['font.family'] = 'SimHei'
plt.plot([3,1,3,2,6])
plt.ylabel("测试哈哈哈")
plt.show()
# 第二种方法:属性设置
plt.ylabel("测试哈哈哈",fontproperties = 'SimHei',fontsize = 20)
plt.show()
文本显示
python">import matplotlib.pyplot as plt
import numpy as np
a = np.arange(0.0,5.0,0.02)
plt.plot(a,np.cos(2 * np.pi * a), 'r--')
plt.xlabel("横轴:时间", fontproperties = 'SimHei',fontsize=15,color = 'green')
plt.ylabel("纵轴:振幅",fontproperties = 'SimHei',fontsize=15)
plt.title(r'正弦波实例 $y=cos(2\pi x)$',fontproperties='SimHei',fontsize=25)
plt.text(2,1.5, r'$\mu=100$',fontsize=15)
# 使用箭头
# plt.annotate(r'$\mu=100$',xy=(2,1),xytext=(3,1.5),arrowprops=dict(facecolor='black',shrink=0.1,width=2))
plt.axis([-1,6,-2,2])
plt.grid(True)
plt.show()
饼图绘制
python">import matplotlib.pyplot as plt
import numpy as np
labels = 'Frogs','Hogs','Dogs','Logs'
sizes = [15,30,45,10]
explode = [0,0.1,0,0]
plt.pie(sizes,explode = explode,labels = labels,autopct='%1.1f%%',shadow=False,startangle=90)
plt.axis('equal')
plt.show()
直方图绘制
报错:AttributeError: 'Polygon' object has no property 'normed'
原因:库更新后不支持normed
了,改为density=True
python">import matplotlib.pyplot as plt
import numpy as np
np.random.seed(0)
mu, sigma = 100,20
a = np.random.normal(mu,sigma,size = 100)
plt.hist(a,30,density=True,histtype='stepfilled', facecolor='b',alpha = 0.75,stacked=True)
plt.title('Histogram')
plt.show()
极坐标图
python">
import matplotlib.pyplot as plt
import numpy as np
N = 10
theta = np.linspace(0.0, 2 * np.pi,N,endpoint=False)
radii = 10* np.random.rand(N)
width = np.pi / 2 * np.random.rand(N)
ax = plt.subplot(111,projection = 'polar')
bars = ax.bar(theta, radii, width = width, bottom =0.0)
for r, bar in zip(radii, bars):
bar.set_facecolor(plt.cm.viridis(r / 10))
bar.set_alpha(0.5)
plt.show()
散点图
python">import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
ax.set_title('simple Scatter')
plt.show()
引力波绘制
数据在这个网站获取https://python123.io/dv/grawave.html
python">import matplotlib.pyplot as plt
import numpy as np
from scipy.io import wavfile
# 使用wavfile来读取音频文件.wav
rate_h, hstrain = wavfile.read(r"H1_Strain.wav","rb")
rate_l, lstrain = wavfile.read(r"L1_Strain.wav","rb")
# 读取引力波模型
# 其中reftime是时间序列,ref_H1是信号数据
reftime, ref_H1 = np.genfromtxt('wf_template.txt').transpose()
# 对波形求倒数,得到时间间隔
htime_interval = 1/rate_h
ltime_interval = 1/rate_l
# 求坐标轴的长度以及每个坐标
htime_len = hstrain.shape[0]/rate_h
htime = np.arange(-htime_len/2, htime_len/2, htime_interval)
ltime_len = lstrain.shape[0]/rate_h
ltime = np.arange(-ltime_len/2, ltime_len/2, ltime_interval)
# 创造绘图空间
fig = plt.figure(figsize = (12,6))
plth = fig.add_subplot(221)
plth.plot(htime,hstrain,'y')
plth.set_xlabel('Time (seconds)')
plth.set_ylabel('H1 Strain')
plth.set_title('H1 Strain')
# 以完全相同的方式绘制另外两幅图像
plth = fig.add_subplot(222)
plth.plot(ltime,lstrain,'g')
plth.set_xlabel('Time (seconds)')
plth.set_ylabel('L1 Strain')
plth.set_title('L1 Strain')
# 以完全相同的方式绘制另外两幅图像
plth = fig.add_subplot(212)
plth.plot(reftime,ref_H1)
plth.set_xlabel('Time (seconds)')
plth.set_ylabel('Template Strain')
plth.set_title('Template')
fig.tight_layout()
plt.savefig("xxx.png")
plt.show()
plt.close(fig)
效果: