因为工作中需要进行数据的可视化,首先想到了Matplotlib,因为将其中常用的图形绘制方法进行了如下的总结。
  • 饼状图
  • 直方图
  • 坐标图
  • 散点图
  • 引力波图
1. 饼状图
1import
 matplotlib.pyplot 
as
 plt

2
labels = 
'Frogs'
,
'Hogs'
,
'Dogs'
,
'Logs'#自定义标签
3
sizes = [
15
,
30
,
45
,
10
#每个标签占多大
4
explode = (
0
,
0.1
,
0
,
0
#将某部分爆炸出来
5
plt.pie(sizes,explode=explode,labels=labels,autopct=
'%1.1f%%'
,shadow=
False
,startangle=
90
)

6# autopct,圆里面的文本格式,%1.1f%%表示小数有1位,整数有一位的浮点数
7# shadow,饼是否有阴影
8# startangle,起始角度,0,表示从0开始逆时针转,为第一块。一般选择从90度开始比较好看
9
plt.axis(
'equal'
# 设置x,y轴刻度一致,这样饼图才能是圆的
10
plt.show()
2. 直方图
1import
 numpy 
as
 np

2import
 matplotlib.pyplot 
as
 plt

3
np.random.seed(
0
#每次生成的随即数都相同
4
mu , sigma = 
100
,
20#均值和标准差
5
a = np.random.normal(mu,sigma,size=
100
#给出均值为mean,标准差为stdev的高斯随机数(场),当size赋值时,例如:size=100,表示返回100个高斯随机数。
6
plt.hist(a,
10
,histtype=
'stepfilled'
,facecolor=
'b'
,alpha=
0.75
#10是直方图的个数
7
plt.title(
'Histogram'
#标题
8
plt.show()

3. 坐标图
1import
 numpy 
as
 np

2import
 matplotlib.pyplot 
as
 plt

3
N = 
20
4
theta = np.linspace(
0.0
2
 * np.pi, N , endpoint=
False
)

5
radii = 
10
 * np.random.rand(N)

6
width = np.pi / 
4
 * np.random.rand(N)

7
ax = plt.subplot(
111
,projection=
'polar'
)

8
bars = ax.bar(theta,radii,width=width,bottom=
0.0
)

9for
 r,bar 
in
 zip(radii,bars):

10
    bar.set_facecolor(plt.cm.viridis(r / 
10.
))

11
    bar.set_alpha(
0.5
)

12
plt.show()

4. 散点图
1import
 numpy 
as
 np

2import
 matplotlib.pyplot 
as
 plt

3
fig , ax = plt.subplots()

4
ax.plot(
10
*np.random.rand(
100
),
10
*np.random.rand(
100
),
'o'
)

5
ax.set_title(
'Simple Scatter'
)

6
plt.show()

整体来看,Python的Matplotlib画图还是挺不错的,看看用BDP画出来的图是哪样的。。
感觉还是有一些差距啊~最后教大家一招用Matplotlib绘制引力波,效果如下
1import
 numpy 
as
 np

2import
 matplotlib.pyplot 
as
 plt

3from
 scipy.io 
import
 wavfile

4
rate_h, hstrain = wavfile.read(
r"H1_Strain.wav"
"rb"
)

5
rate_l, lstrain = wavfile.read(
r"L1_Strain.wav"
"rb"
)

6
reftime, ref_H1 = np.genfromtxt(
'wf_template.txt'
).transpose() 
# 使用python123.io下载文件
7
htime_interval = 
1
 / rate_h

8
ltime_interval = 
1
 / rate_l

9
fig = plt.figure(figsize=(
12
6
))

10# 丢失信号起始点
11
htime_len = hstrain.shape[
0
] / rate_h

12
htime = np.arange(-htime_len / 
2
, htime_len / 
2
, htime_interval)

13
plth = fig.add_subplot(
221
)

14
plth.plot(htime, hstrain, 
'y'
)

15
plth.set_xlabel(
'Time (seconds)'
)

16
plth.set_ylabel(
'H1 Strain'
)

17
plth.set_title(
'H1 Strain'
)

18
ltime_len = lstrain.shape[
0
] / rate_l

19
ltime = np.arange(-ltime_len / 
2
, ltime_len / 
2
, ltime_interval)

20
pltl = fig.add_subplot(
222
)

21
pltl.plot(ltime, lstrain, 
'g'
)

22
pltl.set_xlabel(
'Time (seconds)'
)

23
pltl.set_ylabel(
'L1 Strain'
)

24
pltl.set_title(
'L1 Strain'
)

25
pltref = fig.add_subplot(
212
)

26
pltref.plot(reftime, ref_H1)

27
pltref.set_xlabel(
'Time (seconds)'
)

28
pltref.set_ylabel(
'Template Strain'
)

29
pltref.set_title(
'Template'
)

30
fig.tight_layout()

31
plt.savefig(
"Gravitational_Waves_Original.png"
)

32
plt.show()

33
plt.close(fig)

推荐阅读
喜欢就点击“在看”吧!
继续阅读
阅读原文