您的位置:首页 > 编程语言 > Python开发

Python数据分析与展示——Matplotlib

2020-07-06 23:49 363 查看

Matplotlib:数据可视化库
import matplotlib.pyplot as plt
内容:
代码托管:https://github.com/ve-loo/python/blob/master/matplotlib.ipynb
电脑文件夹C:\Users\asus-1\Desktop\2020learn\Numpy1中的matplotlib.ipynb

一、pyplot的plot函数、中文显示、文本显示、子绘图区域

1.pyplot的plot()函数


2.pyplot的中文显示


3.pyplot的文本显示


5.pyplot的子绘图区域

二、基础绘图函数
1、饼图
plt.pie()

2.直方图
plt.hist()
3.极坐标图

4.散点图

三、引力波的绘制

实例2:引力波的绘制

  1. 什么是引力波?
    物理学中,引力波是因为时空弯曲对外以辐射形式传播的能量.
    爱因斯坦基于广义相对论预言了引力波的存在.

数据源:http://python123.io/dv/grawave.html

import numpy as np #科学计算库numpy
import matplotlib.pyplot as plt #绘图所用的pyplot
from scipy.io import wavfile #读取波形文件的库

rate_h, hstrain = wavfile.read(r"C:/Users/asus-1/Desktop/2020learn/Numpy1/H1_Strain.wav",“rb”) #wavfile可用来读取音频文件,在字符串前添加r表示原始的字符串
rate_l, lstrain = wavfile.read(r"C:/Users/asus-1/Desktop/2020learn/Numpy1/L1_Strain.wav",“rb”) # 速率rate,数值strain

#reftime, ref_H1 = np.genfromtxt(‘GW150914_4_NR_waveform_template.txt’).transpace() #reftime是时间序列,ref_H1是信号的数据
reftime, ref_H1 = np.genfromtxt(‘C:/Users/asus-1/Desktop/2020learn/Numpy1/wf_template.txt’).transpose() #使用python123.io下载文件

htime_interval = 1/rate_h
ltime_interval = 1/rate_l

fig = plt.figure(figsize = (12, 6)) # 丢失信号起始点 创建12*6的绘图空间
htime_len = hstrain.shape[0] / rate_h
htime = np.arange(-htime_len/2, htime_len/2, htime_interval)

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

ltime_len = lstrain.shape[0] / rate_l
ltime = np.arange(-ltime_len/2, ltime_len/2, ltime_interval)
pltl = fig.add_subplot(222)
pltl.plot(ltime, lstrain, ‘g’)
pltl.set_xlabel(‘Time(seconds)’)
pltl.set_ylabel(‘L1 Strain’)
pltl.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()
plt.savefig(“Gravitational_Waves_Original.png”)
plt.show()
plt.close(fig)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: