python数据可视化,matplotlib可视化绘图,自定义子图布局,多区域

教程简介

最近需要为某个项目的matlab数据提供一套可视化的图表渲染结果

利用Python + Matplotlib,很方便的处理了数据,也很优质的呈现了数据结果,效果非常好,其中主要遇到了如下几个难点

  • 利用Matplotlib 实现多个图表的排版
  • 加入数学公式图例

效果图为:

出图代码为: 

def decode_bin(self,bin_folder,target_folder):
		filepath = "{0}/{1}/{2}.bin".format(bin_folder,self._name,self._name)
		imgfolder = "{0}/{1}".format(target_folder,self._name)
		self.open_file(filepath)
		#读取支路数(4)
		branchs = self.read_int()
		print("支路数为:",branchs)
		#读取画图变量长度(4)
		times = self.read_int()
		print("画图变量为:",times)
		imgindex = 1
		#逐个支路读取
		for temp in range(branchs):
				plt.figure(figsize=(16,12),facecolor='snow')
				plt.subplots_adjust(hspace=0.5)
				# 设置title,x,y
				plt.title(self.coord_infos[self._name][imgindex - 1]['title'], fontproperties="SimHei")
				#设置完毕
				#IP
				x234567 = self.read_double_array(times)
				x1y2 = self.read_double_array(times)
				y1 = self.read_double_array(times)
				y3 = self.read_double_array(times)
				y5 = self.read_double_array(times)
				y6 = self.read_double_array(times)
				y7 = self.read_double_array(times)
				y4_1 = self.read_double_array(times)
				y4_2 = self.read_double_array(times)
				y4_3 = self.read_double_array(times)
				#左上

				gs = GridSpec(3,3)
				plt.subplot(gs[0,0])
				plt.title("解调信号向量图")
				plt.xlabel("I支路")
				plt.ylabel("Q支路")
				plt.scatter(x1y2,y1)
				#右上
				plt.subplot(gs[0, 1:])
				plt.title("即时I支路输出")
				plt.xlabel("时间/秒")
				plt.plot(x234567,x1y2)
				#中1
				plt.subplot(gs[1, 0])
				plt.title("载波环鉴相器输出")
				plt.xlabel("时间/秒")
				plt.ylabel("幅度")
				plt.plot(x234567,y3)
				#中2
				cfg = plt.subplot(gs[1, 1:])
				plt.title("相关结果")
				plt.xlabel("时间/秒")
				plt.plot(x234567,y4_1,label="$\sqrt{I_{E}^2 + Q_{E}^2}$")
				plt.plot(x234567,y4_2,label="$\sqrt{I_{P}^2 + Q_{P}^2}$")
				plt.plot(x234567,y4_3,label="$\sqrt{I_{L}^2 + Q_{L}^2}$")
				plt.legend(loc="upper right",ncol=1)
				#下1
				plt.subplot(gs[2, 0])
				plt.title("载波环环路滤波器输出")
				plt.xlabel("时间/秒")
				plt.ylabel("载波中频调整量/Hz")
				plt.plot(x234567,y5)
				#下2
				plt.subplot(gs[2, 1])
				plt.title("码环鉴相器输出")
				plt.xlabel("时间/秒")
				plt.ylabel("幅度")
				plt.plot(x234567,y6)
				#下3
				plt.subplot(gs[2, 2])
				plt.title("码环环路滤波器输出")
				plt.xlabel("时间/秒")
				plt.ylabel("码速率调整量/Hz")
				plt.plot(x234567,y7)

				plt.savefig("{0}/{1}.jpg".format(imgfolder,self.coord_infos[self._name][imgindex - 1]['title']))
				imgindex+=1
		self.close()
		plt.cla()
		plt.close("all")
		print("解析完毕")

技术点归纳

  • 如何利用Matplotlib进行多个图表排版
    • GridSpec代表着一个布局对象,创建了一个3x3的布局对象,我们可以想象成一个九宫格,
    • gs = GridSpec(3,3)

      我们可以通过类似操纵列表对象的方式,来指定每个子图的布局范围

    • plt.subplot(gs[0, 1:])

      然后就可以安心绘图啦。

  • 如何增加图例公式
    • matplotlib增加图例公式的方法和matlab一致,直接用matlab的公式字符串即可
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碳纤维石头君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值