python 实现将多条曲线画在一幅图上的方法

yipeiwu_com6年前Python基础

如下所示:

# -*- coding: utf-8 -*-
"""
Created on Thu Jun 07 09:17:40 2018

@author: yjp
"""
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator, FormatStrFormatter


y0 = []
y1 = []
y2 = []
y3 = []
y4 = []

f = open("y0.txt") 
lines = f.readlines() 
for li in lines: 
  y0.append(li)

f = open("y1.txt") 
lines = f.readlines() 
for li in lines:  
  y1.append(li)

f = open("y2.txt") 
lines = f.readlines() 
for li in lines:  
  y2.append(li)

f = open("y3.txt") 
lines = f.readlines() 
for li in lines:  
  y3.append(li)

f = open("y4.txt") 
lines = f.readlines() 
for li in lines: 
  y4.append(li)

font1 = {'family' : 'Times New Roman', 
'weight' : 'normal', 
'size'  : 9, 
} 

font2 = {'family' : 'Times New Roman', 
'weight' : 'normal', 
'size'  : 14, 
} 

figsize = 8, 9
plt.subplots(figsize=figsize)                # 设定整张图片大小

ax1 = plt.subplot(4, 1, 1)
ax1.yaxis.set_major_locator(MultipleLocator(15))       # 设定y轴刻度间距
#第一条线
x = range(0, len(y0))
plt.plot(x, y0, color='black', label='$DT$', linewidth=0.8) # 绘制,指定颜色、标签、线宽,标签采用latex格式
plt.ylim(-90, -20)                      # 设定y轴范围
hl=plt.legend(loc='upper right', prop=font1, frameon=False)                # 绘制图例,指定图例位置
#set(hl,'Box','off');
#第二条曲线
x = range(0, len(y1))
plt.plot(x, y1, color='red', label='$M_1$', linewidth=0.8)
plt.legend(loc='upper right', prop=font1, frameon=False)                # 绘制图例,指定图例位置
plt.xticks([])                        # 去掉x坐标轴刻度
plt.xlim(0, 580)                       # 设定x轴范围

ax2 = plt.subplot(4, 1, 2)
ax2.yaxis.set_major_locator(MultipleLocator(15))
x = range(0, len(y0))
plt.plot(x, y0, color='black', label='$DT$', linewidth=0.8)
plt.ylim(-90, -20)
hl=plt.legend(loc='upper right', prop=font1, frameon=False)
#set(hl,'Box','off');
x = range(0, len(y2))
plt.plot(x, y2, color='red', label='$M_2$', linewidth=0.8)
plt.legend(loc='upper right', prop=font1, frameon=False)
plt.ylabel("strength/dBm", font2)
plt.xticks([])
plt.xlim(0, 580)

ax3 = plt.subplot(4, 1, 3)
ax3.yaxis.set_major_locator(MultipleLocator(15))
x = range(0, len(y0))
plt.plot(x, y0, color='black', label='$DT$', linewidth=0.8)
hl=plt.legend(loc='upper right', prop=font1, frameon=False)
#set(hl,'Box','off');
plt.ylim(-90, -20)
x = range(0, len(y3))
plt.plot(x, y3, color='red', label='$M_3$', linewidth=0.8)
plt.legend(loc='upper right', prop=font1, frameon=False)
plt.xticks([])
plt.xlim(0, 580)

ax4 = plt.subplot(4, 1, 4)
ax4.yaxis.set_major_locator(MultipleLocator(15))
ax4.xaxis.set_major_locator(MultipleLocator(50))
x = range(0, len(y0))
plt.plot(x, y0, color='black', label='$DT$', linewidth=0.8)
plt.ylim(-90, -20)
hl=plt.legend(loc='upper right', prop=font1, frameon=False)
#set(hl,'Box','off');
x = range(0, len(y4))
plt.plot(x, y4, color='red', label='$M_4$', linewidth=0.8)
plt.legend(loc='upper right', prop=font1, frameon=False)
plt.xlabel("index of grids in path", font2)
plt.xlim(0, 580)

plt.savefig("1.png", dpi=600))

plt.show()

以上这篇python 实现将多条曲线画在一幅图上的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python将图片批量从png格式转换至WebP格式

Python将图片批量从png格式转换至WebP格式

实现效果 将位于/img目录下的1000张.png图片,转换成.webp格式,并存放于img_webp文件夹内。 源图片目录 目标图片目录 关于批量生成1000张图片,可以参考这篇文...

python 中的int()函数怎么用

int(x, [base]) 功能: 函数的作用是将一个数字或base类型的字符串转换成整数。 函数原型: int(x=0) int(x, base=10),base缺省值为10,也就是...

python 与GO中操作slice,list的方式实例代码

python 与GO中操作slice,list的方式实例代码 GO代码中遍历slice,寻找某个slice,统计个数。 type Element interface{} func...

Python模块future用法原理详解

这篇文章主要介绍了Python模块future用法原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 计算机的知识太多了,很多东西...

Python Web框架Flask下网站开发入门实例

Python Web框架Flask下网站开发入门实例

一、Flask简介 Flask 是一个 Python 实现的 Web 开发微框架。官网:http://flask.pocoo.org/ 二、Demo 1、代码结构 复制代码 代码如下:...