Less than a year ago, being a python novice it was hard to look over utilities like excel and more recently gnuplot to plot graphs from stats obtained in files. Both have their pros and cons. But once the python knot got untied it is harder to look over the ease it provides for anything and everything. One such module is matplotlib.
Matplotlib is a python 2D plotting library module. As with any other python module one import statement should do the trick. In this post I am just glossing the very basic plotting I required for my lab report. Future posts I will try and cover the other features I have tried out. The situation where matplotlib nicely fits in is the classic stats dump and analysis that we are very often used to. After learning python I realised that collecting stats is so much simpler in python and once we dump the stats into the stats file in our format, the matplotlib code can give you the plots in no time.
Enough of the boring text, we need code. So the following is a small code I wrote for my Networks assignment where I analysed the response time of the standard UDP connection with various packet error rates(simualted unreliable socket) and timeout values at the client.
#!/usr/bin/env python
import matplotlib.pyplot as plt
def get_plot_values(file_name):
fp = open(file_name)
data = []
fp.readline()
while True:
line = fp.readline()
if not line: break
x = int(line.split()[1])
y = float(line.split()[2])
data.append((x,y))
return data
pkt_error_rate = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
i = 0
arr_x = []
arr_y = []
for p in pkt_error_rate:
file_name = "stats/plot_data"+str(p)+".txt"
plot_val = get_plot_values(file_name)
x_val,y_val = zip(*plot_val)
arr_x.append(x_val)
arr_y.append(y_val)
i = i + 1
plt.title('Timeout time vs Response time for various Pkt error rates')
plt.xlabel('Timeout value')
plt.ylabel('Response time(in ms)')
plt.plot(arr_x[0], arr_y[0], arr_x[1], arr_y[1], arr_x[2], arr_y[2], arr_x[3], arr_y[3],
arr_x[4], arr_y[4], arr_x[5], arr_y[5])plt.show() |
A small extension to the above code I would like to add for people who like to get multiple subplots on the same plot, matplotlib saves your time. It produces research paper quality plots.
plt.figure(1)
plt.axes([0.1,0.1,0.3,0.3])
plt.title('packet error rate vs Response time for various time out value = 1s')
plt.xlabel('Packet error rate')
plt.ylabel('Response time(in ms)')
plt.plot(arr[0][0], arr[0][0])
plt.axes([0.6,0.1,0.3,0.3])
plt.title('packet error rate vs Response time for various time out value = 2s')
plt.xlabel('Packet error rate')
plt.ylabel('Response time(in ms)')
plt.plot(arr[1][0], arr[1][1])
plt.axes([0.1,0.5,0.5,0.4])
plt.title('packet error rate vs Response time for various time out value = 3s')
plt.xlabel('Packet error rate')
plt.ylabel('Response time(in ms)')
plt.plot(arr[2][0], arr[2][1])
plt.show() |
For a sample demonstration of the plot, the following was my packet error rate vs response time for various timeout values(the second example).

No comments:
Post a Comment