I measured the duration of 6000 requests.
I got now an Array of 6000 elements. Each element represents the duration of a connection request in milliseconds.
[3,2,2,3,4,2,2,4,2,3,3,4,2,4,4,3,3,3,4,3,2,3,5,5,2,4,4,2,2,2,3,5,3,2,2,3,3,3,5,4........]
I want to plot the confidence interval in Python and in a clearly arranged manner.
Do you have any Idea how I should plot them?
Advertisement
Answer
From what I understood this code should answer your question
JavaScript
x
27
27
1
import numpy as np
2
import matplotlib.pyplot as plt
3
%matplotlib inline
4
import seaborn as sns
5
from statistics import NormalDist
6
7
X = np.random.sample(100)
8
data = ((X - min(X)) / (max(X) - min(X))) * 3 + 3
9
confidence_interval = 0.95
10
11
def getCI(data, ci):
12
normalDist = NormalDist.from_samples(data)
13
z = NormalDist().inv_cdf((1 + ci) / 2.)
14
p = normalDist.stdev * z / ((len(data) - 1) ** .5)
15
return normalDist.mean, normalDist.mean - p, normalDist.mean + p
16
17
avg, lower, upper = getCI(data, confidence_interval)
18
sns.set_style("whitegrid")
19
plt.figure(figsize=(8, 4))
20
sns.histplot(data, bins = 10)
21
plt.axvspan(lower, upper, facecolor='r', alpha=0.2)
22
plt.axvline(avg, color = 'b', label = 'Average')
23
plt.ylabel("Operations")
24
plt.xlabel("Connection Request Duration (ms)")
25
26
plt.show()
27
For boxplot:
JavaScript
1
29
29
1
import numpy as np
2
import matplotlib.pyplot as plt
3
%matplotlib inline
4
import seaborn as sns
5
from statistics import NormalDist
6
7
X = np.random.sample(100)
8
data = ((X - min(X)) / (max(X) - min(X))) * 3 + 3
9
confidence_interval = 0.95
10
11
def getCI(data, ci):
12
normalDist = NormalDist.from_samples(data)
13
z = NormalDist().inv_cdf((1 + ci) / 2.)
14
p = normalDist.stdev * z / ((len(data) - 1) ** .5)
15
return normalDist.mean, normalDist.mean - p, normalDist.mean + p
16
17
avg, lower, upper = getCI(data, confidence_interval)
18
sns.set_style("whitegrid")
19
plt.figure(figsize=(8, 4))
20
sns.boxplot(data = data, orient = "h")
21
plt.axvspan(lower, upper, facecolor='r', alpha=0.4)
22
plt.axvline(avg, color = 'b', label = 'Average')
23
plt.ylabel("Operations")
24
plt.xlabel("Connection Request Duration (ms)")
25
plt.yticks([0],["Server Retry Request Delay"])
26
27
plt.savefig("fig.png")
28
plt.show()
29
For Multiple Plots:
JavaScript
1
40
40
1
import numpy as np
2
import matplotlib.pyplot as plt
3
%matplotlib inline
4
import seaborn as sns
5
from statistics import NormalDist
6
7
X1, X2 = np.random.sample(100), np.random.sample(100)
8
data1, data2 = ((X1 - min(X1)) / (max(X1) - min(X1))) * 3 + 3, ((X2 - min(X2)) / (max(X2) - min(X2))) * 2 + 3
9
confidence_interval = 0.95
10
11
def getCI(data, ci):
12
normalDist = NormalDist.from_samples(data)
13
z = NormalDist().inv_cdf((1 + ci) / 2.)
14
p = normalDist.stdev * z / ((len(data) - 1) ** .5)
15
return normalDist.mean, normalDist.mean - p, normalDist.mean + p
16
17
sns.set_style("whitegrid")
18
avg1, lower1, upper1 = getCI(data1, confidence_interval)
19
avg2, lower2, upper2 = getCI(data2, confidence_interval)
20
21
fig = plt.figure(figsize=(12, 6))
22
ax1 = fig.add_subplot(211)
23
ax2 = fig.add_subplot(212, sharex = ax1, sharey = ax1)
24
25
sns.boxplot(data = data1, orient = "h", ax = ax1)
26
ax1.axvspan(lower1, upper1, facecolor='r', alpha=0.4)
27
ax1.axvline(avg1, color = 'b', label = 'Average')
28
29
sns.boxplot(data = data2, orient = "h", ax = ax2)
30
ax2.axvspan(lower2, upper2, facecolor='r', alpha=0.4)
31
ax2.axvline(avg2, color = 'b', label = 'Average')
32
ax2.set_xlabel("Connection Request Duration (ms)")
33
34
plt.setp(ax1.get_xticklabels(), visible=False)
35
plt.setp(ax1.get_yticklabels(), visible=False)
36
plt.setp(ax2.get_yticklabels(), visible=False)
37
fig.text(0.08, 0.5, "Operations", va='center', rotation='vertical')
38
39
plt.show()
40