使用matplotlib更新绘制的数据

时间:2016-09-19 09:05:42

标签: python matplotlib jsonpickle

我已经检查了很多与这个问题有关的问题,但我仍然无法解决我的问题。当我通过终端打印数据时,我看到数据已正确更新,但在图中未正确更新,因为数据似乎重叠。我的函数的输入数据是使用jsonpickle格式编码的多个字符串的列表。

def satellite_plot(messages_list):

    plots = {}

    ## Get the necessary data from the input message list

    for ii in range(len(messages_list)):

        if messages_list[ii]["satellite_info"]["gnss"] == "GPS":
            satellite_name = "G" + str(messages_list[ii]["satellite_info"]["satellite_id"])

        elif messages_list[ii]["satellite_info"]["gnss"] == "Galileo":
            satellite_name = "E" + str(messages_list[ii]["satellite_info"]["satellite_id"])

        # considering polar coordinates:
        # the angle of the satellite is the azimuth
        # the elevation is converted to the radius of the circle by:

        azimuths[satellite_name] = messages_list[ii]["satellite_info"]["azimuth"] * pi  / 180  # to radians
        elevations[satellite_name] = 90 - messages_list[ii]["satellite_info"]["elevation"]  # elevation radiuses from 90 to 0

        ## make new plot if satellite name is not already contained in dicts:
        # if name not found, add new key to all dictionaries

        if messages_list[ii]["satellite_info"]["gnss"]  == "GPS":
            plots[satellite_name], = skyplot.plot(azimuths[satellite_name], elevations[satellite_name], 'bo')

        elif messages_list[ii]["satellite_info"]["gnss"] == "Galileo":
            plots[satellite_name], = skyplot.plot(azimuths[satellite_name], elevations[satellite_name], 'ro')

        plots[satellite_name].set_xdata(azimuths[satellite_name])
        plots[satellite_name].set_ydata(elevations[satellite_name])

    fig.canvas.draw()

if __name__ == "__main__":

    azimuths = {}  # radiuses of the currently tracked satellites

    elevations = {}    # angles of the currently tracked satellites

    plots = {}  # dictionary that contains the current point for each satellite

    file_sample =[
{"hw_timestamp":1822581000,"id":"pvt_calculation","protocol":"OGRP1_1","pvt_calculation":[{"clock_bias":-0.004423996296702252,"clock_drift":1.361534381506130e-08,"coordinate_system":"WGS84","number_of_satellites":10,"position":{"x":4.516736645143737e-05,"y":-3.836113359056981e-05,"z":-32.57596752978861},"precision":"3D","velocity":{"x":-0.1246464472973443,"y":0.1429197664922133,"z":0.03724550663488135}}],"sw_version":32768,"time_status":"FREE_RUNNING","timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":297.8554532555478,"direction":{"x":0.7537536349533224,"y":-0.5810116318484969,"z":0.3070520175009441},"elevation":48.91663887708157,"expected_range":20094032.37830078,"expected_range_rate":-141.2923829757924,"gnss":"GPS","satellite_id":10},"time_status":2,"timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":116.1412474962802,"direction":{"x":0.7404655783632587,"y":0.6033463158720899,"z":-0.2961147588060097},"elevation":47.77103661185114,"expected_range":20163389.04642602,"expected_range_rate":-252.7339922035036,"gnss":"GPS","satellite_id":17},"time_status":2,"timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":193.3871914400808,"direction":{"x":0.6471975304938233,"y":-0.1765013460394418,"z":-0.7416081386884599},"elevation":40.33060464932528,"expected_range":20657639.01743281,"expected_range_rate":535.0708954459112,"gnss":"GPS","satellite_id":26},"time_status":2,"timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":152.313597763160,"direction":{"x":0.4135761992720348,"y":0.4230330995330789,"z":-0.8062243633723396},"elevation":24.42962737306918,"expected_range":21953944.30989864,"expected_range_rate":120.1291192156332,"gnss":"GPS","satellite_id":27},"time_status":2,"timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":352.0793600961534,"direction":{"x":0.3628180253996969,"y":-0.1284117694789551,"z":0.9229699333696351},"elevation":21.27341152683660,"expected_range":22246730.52767780,"expected_range_rate":147.1208501747284,"gnss":"GPS","satellite_id":2},"time_status":2,"timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":204.9934115317646,"direction":{"x":0.4007384537379515,"y":-0.3871045359427047,"z":-0.8303967545386355},"elevation":23.62432645019001,"expected_range":22027616.56503316,"expected_range_rate":626.4965195208777,"gnss":"GPS","satellite_id":15},"time_status":2,"timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":34.46057167201275,"direction":{"x":0.3695267587318597,"y":0.5257886416201152,"z":0.7661568239756139},"elevation":21.68644952052070,"expected_range":22207804.72333424,"expected_range_rate":68.28392241501729,"gnss":"GPS","satellite_id":4},"time_status":2,"timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":294.4757165064577,"direction":{"x":0.3211060585438145,"y":-0.8619389858612561,"z":0.3923672817895573},"elevation":18.72988130689558,"expected_range":22490349.45883135,"expected_range_rate":286.7157028996278,"gnss":"GPS","satellite_id":12},"time_status":2,"timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":157.0683337268788,"direction":{"x":0.2610357127845332,"y":0.3761240272382553,"z":-0.8890394101417253},"elevation":15.13147027901228,"expected_range":22845982.12935818,"expected_range_rate":-552.3891676642655,"gnss":"GPS","satellite_id":28},"time_status":2,"timestamp":1471219272.999571},
{"hw_timestamp":1822581000,"id":"satellite_info","protocol":"OGRP1_1","satellite_info":{"azimuth":124.0520436322381,"direction":{"x":0.2270340137092914,"y":0.8068938053839888,"z":-0.5453237051990266},"elevation":13.12245746683365,"expected_range":23049782.25215284,"expected_range_rate":-2.105826922317874,"gnss":"GPS","satellite_id":8},"time_status":2,"timestamp":1471219272.999571}]

    # skyplot figure initialisation

    fig = plt.figure()
    skyplot = fig.add_subplot(111,projection='polar')
    skyplot.set_ylim(90)  # sets radius of the circle to the maximum elevation
    skyplot.set_theta_zero_location("N")  # sets 0(deg) to North
    skyplot.set_theta_direction(-1)  # sets plot clockwise
    skyplot.set_yticks(range(0, 90, 30))  # sets 3 concentric circles 
    skyplot.set_yticklabels(map(str, range(90, 0, -30)))  # reverse labels
    plt.show(block=False)
    ii = 0

    while ii < len(file_sample):
        try:
            data = jsonpickle.decode(file_sample[ii])
        except:
            break

        # splitting the received messages to get the needed data

        if data["id"] == "pvt_calculation":
            number_of_satellites = data["pvt_calculation"][0]["number_of_satellites"]

        if data["id"] == "satellite_info":

            messages_list.append(data)

            if len(messages_list) == number_of_satellites:

                satellite_plot(messages_list)
                messages_list = []
        ii += 1

有人可以告诉我,我可以更改哪些内容,以便在图中看到数据更新?

提前致谢

0 个答案:

没有答案