瓜瓜笔记 | Python数据可视化,从萌新迈向大佬的第一步
因为工作中需要进行数据的可视化,首先想到了Matplotlib,因为将其中常用的图形绘制方法进行了如下的总结。
- 饼状图
- 直方图
- 坐标图
- 散点图
- 引力波图
1. 饼状图
1import matplotlib.pyplot as plt
2labels = 'Frogs','Hogs','Dogs','Logs'#自定义标签
3sizes = [15,30,45,10] #每个标签占多大
4explode = (0,0.1,0,0) #将某部分爆炸出来
5plt.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度开始比较好看
9plt.axis('equal') # 设置x,y轴刻度一致,这样饼图才能是圆的
10plt.show()
2. 直方图
1import numpy as np
2import matplotlib.pyplot as plt
3np.random.seed(0) #每次生成的随即数都相同
4mu , sigma = 100,20#均值和标准差
5a = np.random.normal(mu,sigma,size=100) #给出均值为mean,标准差为stdev的高斯随机数(场),当size赋值时,例如:size=100,表示返回100个高斯随机数。
6plt.hist(a,10,histtype='stepfilled',facecolor='b',alpha=0.75) #10是直方图的个数
7plt.title('Histogram') #标题
8plt.show()
3. 坐标图
1import numpy as np
2import matplotlib.pyplot as plt
3N = 20
4theta = np.linspace(0.0, 2 * np.pi, N , endpoint=False)
5radii = 10 * np.random.rand(N)
6width = np.pi / 4 * np.random.rand(N)
7ax = plt.subplot(111,projection='polar')
8bars = 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)
12plt.show()
1import numpy as np
2import matplotlib.pyplot as plt
3fig , ax = plt.subplots()
4ax.plot(10*np.random.rand(100),10*np.random.rand(100),'o')
5ax.set_title('Simple Scatter')
6plt.show()
整体来看,Python的Matplotlib画图还是挺不错的,看看用BDP画出来的图是哪样的。。
感觉还是有一些差距啊~最后教大家一招用Matplotlib绘制引力波,效果如下
1import numpy as np
2import matplotlib.pyplot as plt
3from scipy.io import wavfile
4rate_h, hstrain = wavfile.read(r"H1_Strain.wav", "rb")
5rate_l, lstrain = wavfile.read(r"L1_Strain.wav", "rb")
6reftime, ref_H1 = np.genfromtxt('wf_template.txt').transpose() # 使用python123.io下载文件
7htime_interval = 1 / rate_h
8ltime_interval = 1 / rate_l
9fig = plt.figure(figsize=(12, 6))
10# 丢失信号起始点
11htime_len = hstrain.shape[0] / rate_h
12htime = np.arange(-htime_len / 2, htime_len / 2, htime_interval)
13plth = fig.add_subplot(221)
14plth.plot(htime, hstrain, 'y')
15plth.set_xlabel('Time (seconds)')
16plth.set_ylabel('H1 Strain')
17plth.set_title('H1 Strain')
18ltime_len = lstrain.shape[0] / rate_l
19ltime = np.arange(-ltime_len / 2, ltime_len / 2, ltime_interval)
20pltl = fig.add_subplot(222)
21pltl.plot(ltime, lstrain, 'g')
22pltl.set_xlabel('Time (seconds)')
23pltl.set_ylabel('L1 Strain')
24pltl.set_title('L1 Strain')
25pltref = fig.add_subplot(212)
26pltref.plot(reftime, ref_H1)
27pltref.set_xlabel('Time (seconds)')
28pltref.set_ylabel('Template Strain')
29pltref.set_title('Template')
30fig.tight_layout()
31plt.savefig("Gravitational_Waves_Original.png")
32plt.show()
33plt.close(fig)
推荐阅读
喜欢就点击“在看”吧!
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。