This repository has been archived by the owner on Nov 18, 2022. It is now read-only.
/
livePlot.py
91 lines (79 loc) · 2.67 KB
/
livePlot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Copyright (C) 2014 SensorLab, Jozef Stefan Institute
# http://sensorlab.ijs.si
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. */
# Authors: Mihai Suciu
from matplotlib import pyplot as plot
import time
class GameLivePlot():
"""
Used to plot player strategies during the game.
"""
title = 'plot'
nr_players = 0
# dictionary holding players best response
gains = dict()
markers = ['o', 'v', 's', 'D']
colors = ['red', 'green', 'blue', 'magenta']
def __init__(self, plot_title):
"""
:param plot_title:
:return:
"""
self.title = plot_title
# start an interactive plot
plot.ion()
# create a figure
self.fig1 = plot.figure(self.title)
def init_plot(self, nr_players):
"""
init plot params
:param nr_players:
:return:
"""
self.nr_players = nr_players
for x in range(0, self.nr_players):
self.gains[x] = list()
def plot_tx_powers(self, pl_tx_powers):
"""
initialize empty plot
:param pl_tx_powers: list containing players initial tx power
:return:
"""
for x in range(0, self.nr_players):
self.gains[x].append(pl_tx_powers[x])
plot.clf()
plot.grid()
plot.xlabel('game iterations')
plot.ylabel('player strategies [dBm]')
for i in range(0, self.nr_players):
plot.plot(range(0,len(self.gains[i])), self.gains[i], color=self.colors[i], marker=self.markers[i], label="Player %d" % (i+1))
plot.legend(loc=0, bbox_to_anchor=(1.1, 1.1))
plot.axis([0, 70, -90, 0])
plot.draw()
time.sleep(2)
def test_plot(self):
"""
test plot
:return:
"""
plot.clf()
plot.grid()
plot.xlabel('game iterations')
plot.ylabel('player strategies')
plot.axis([0, 5, 0, 3])
plot.plot([1, 2, 3], [3, 2, 3], color='red', marker=self.markers[0], label='Player1')
plot.legend(loc=0, bbox_to_anchor=(1.1, 1.1))
plot.draw()
time.sleep(2)