Python数据分析与展示之Matplotlib基础绘图函数示例和引力波的绘制学习笔记手札及代码实战

学习笔记手札及单元小结

01
02
03
04

pyplot饼图的绘制

代码请在Anaconda的Spyder平台运行

import matplotlib.pyplot as plt

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.show()

01

import matplotlib.pyplot as plt

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()

在这里插入图片描述

pyplot直方图的绘制

bin:直方图的个数发生改变,
小伙伴快找一找彩蛋的地方在哪里

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
mu,sigma = 100,20  #均值和标准差
a = np.random.normal(mu,sigma,size=100)

plt.hist(a,20,normed=1,histtype='stepfilled',facecolor='b',alpha=0.75)
plt.title('Histogram')

plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
mu,sigma = 100,20  #均值和标准差
a = np.random.normal(mu,sigma,size=100)

plt.hist(a,10,normed=1,histtype='stepfilled',facecolor='b',alpha=0.75)
plt.title('Histogram')

plt.show()

在这里插入图片描述

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
mu,sigma = 100,20  #均值和标准差
a = np.random.normal(mu,sigma,size=100)

plt.hist(a,40,normed=1,histtype='stepfilled',facecolor='b',alpha=0.75)
plt.title('Histogram')

plt.show()

在这里插入图片描述

pyplot极坐标图的绘制

面向对象绘制极坐标

import numpy as np
import matplotlib.pyplot as plt

N = 20
theta = np.linspace(0.0,2*np.pi,N,endpoint=False)
radii = 10*np.random.rand(N)
width = np.pi/4 * np.random.rand(N)

ax = plt.subplot(111,projection='polar')
bars = ax.bar(theta,radii,width=width,bottom=0.0)
#theta,radii,width分别对应left,height,width

for r,bar in zip(radii,bars):
    bar.set_facecolor(plt.cm.viridis(r / 10.))
    bar.set_alpha(0.5)
    
plt.show()

在这里插入图片描述
面向对象绘制方式

import numpy as np
import matplotlib.pyplot as plt

N = 10  #此处缩减为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()

在这里插入图片描述

pyplot散点图的绘制

import numpy as np
import matplotlib.pyplot as plt

fig,ax = plt.subplots()
ax.plot(10*np.random.randn(100),10*np.random.randn(100),'o')
ax.set_title('Simple Scatter')

plt.show()

在这里插入图片描述

“引力波的绘制”实例介绍与分析展示

#从配置文档中读取事件相关数据
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile  #此处要调用scipy库

rate_h,hstrain=wavfile.read(r"D:/H1_Strain.wav","rb")
rate_l,lstrain=wavfile.read(r"D:/L1_Strain.wav","rb")
reftime,ref_H1 = np.genfromtxt('D:/GW150914_4_NR_waveform_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_l
ltime = np.arange(-ltime_len/2,ltime_len/2,ltime_interval)
#使用来自"H1"探测器的数据做图
fig = plt.figure(figsize=(12,6)) #创建一个大小为12*6的绘图空间

plth = fig.add_subplot(211)
plth.plot(htime,hstrain,'y')#画出以时间为X轴,应变数据为Y轴的图像并设置标题和坐标轴的标签
plth.set_xlabel('Time(seconds)')
plth.set_ylabel('H1 Strain')
plth.set_title('H1 Strain')

pltl = fig.add_subplot(222)
pltl.plot(ltime,lstrain,'g')
pltl.set_xlabel('Time(seconds)')
pltl.set_ylabel('L1 Strain')
plt.set_title('L1 Strain')
#以完全相同的方法绘制另外两幅图像。分别放在绘图区域的第一列右边和第二列
pltref = fig.add_subplot(212) 
pltref.plot(reftime,ref_H1)
pltref.set_xlabel('Time(seconds)')
pltref.set_ylabel('Template Strain')
pltref.set_title('Template')
fig.tight_layout()

fig.tight_layout() #自动调整图像外部边缘

plt.savefig("Gravitational_Waves_Original.png") #保存图像为PNG格式
plt.show()
plt.close(fig)

上述引入文件可从“http://python123.io/dv/grawave.html”种下载

猜你喜欢

转载自blog.csdn.net/zzw1208/article/details/107023943