Python Matplotlib.plot Update image Questions

1. 最近在测试一款设备,采集了一些设备后需要一帧一帧显示图像,经常使用Python,所以选用了Matplotlib进行图像操作

数据结构: timesatamp polar_distance horizontal_angle refelectivity_intensity,所有数据类型都是 float,储存在文件内并且以空格分隔

import math
import matplotlib.pyplot as plt

#read data from file
def LoadData(filename):
    inFile = open(filename, 'r')
    
    #declare two empty list,to save data
    inten = []
    polar_dist = []
    hori_angle = []
    
    for line in inFile:
        splitline = line.split(" ")
        polar_dist.append(splitline[1])
        hori_angle.append(splitline[2])
        inten.append(splitline[3])
    inFile.close()   
   
    #mapping to float
    polar_dist = list(map(float,polar_dist))
    hori_angle = list(map(float,hori_angle))
    inten = list(map(float,inten))
    
    return(polar_dist, hori_angle,inten)

2. 显示极坐标图像

def GenerateImageFromData(polar,angle,inten):
    d2r = math.pi/180
    for i in range(len(polar)):
        angle[i] = d2r*(angle[i] +180)
    plt.ion()
  #inreactive code ax
= plt.subplot(111, projection='polar') for i in range(100): c = ax.scatter(angle[i*167 :(i+1)*167], polar[i*167 :(i+1)*167], s = 10) plt.show() plt.pause(1)

3. 调用函数

if __name__=="__main__":
    (polar,angle,inten) = LoadData("CloudData.txt")
    GenerateImageFromData(polar,angle,inten)

当绘图语句中加入pl.ion()时,表示打开了交互模式。此时python解释器解释完所有命令后,给你出张图,但不会结束会话,而是等着你跟他交流交流。如果你继续往代码中加入语句,run之后,你会实时看到图形的改变。当绘图语句中加入pl.ioff()时或不添加pl.ion()时,表示打关了交互模式。

4. 另外一个example 

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 16 21:26:17 2017

@author: XX
"""

import matplotlib.pylab as plt 
import numpy as np 
import math 

#clear images 
plt.close()

fig = plt.figure()
ax = fig.add_subplot(111)

#ratio of x and y axis
ax.axis("equal")

#enable grid of image
ax.grid(True)

#activate inreactivate mode
plt.ion()

init_x = 0.0
init_y = 3.5
init_angle = 45
init_wind_speed = 3

print("inreactive mode beginning")
try: 
    for t in range(50):
        x = init_x + init_wind_speed*math.sin(math.pi/180*init_angle)*t
        y = init_y + init_wind_speed*math.cos(init_angle*math.pi/180)*t
        ax.scatter(x,y,c='r', s =5, marker ='^')
        plt.pause(0.2)
except Exception as err:
    print(err)

 

posted @ 2017-11-16 22:04  采男孩的小蘑菇  阅读(677)  评论(0编辑  收藏  举报