Plot confidence interval of a duration series - python

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?

From what I understood this code should answer your question
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
from statistics import NormalDist
X = np.random.sample(100)
data = ((X - min(X)) / (max(X) - min(X))) * 3 + 3
confidence_interval = 0.95
def getCI(data, ci):
normalDist = NormalDist.from_samples(data)
z = NormalDist().inv_cdf((1 + ci) / 2.)
p = normalDist.stdev * z / ((len(data) - 1) ** .5)
return normalDist.mean, normalDist.mean - p, normalDist.mean + p
avg, lower, upper = getCI(data, confidence_interval)
sns.set_style("whitegrid")
plt.figure(figsize=(8, 4))
sns.histplot(data, bins = 10)
plt.axvspan(lower, upper, facecolor='r', alpha=0.2)
plt.axvline(avg, color = 'b', label = 'Average')
plt.ylabel("Operations")
plt.xlabel("Connection Request Duration (ms)")
plt.show()
For boxplot:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
from statistics import NormalDist
X = np.random.sample(100)
data = ((X - min(X)) / (max(X) - min(X))) * 3 + 3
confidence_interval = 0.95
def getCI(data, ci):
normalDist = NormalDist.from_samples(data)
z = NormalDist().inv_cdf((1 + ci) / 2.)
p = normalDist.stdev * z / ((len(data) - 1) ** .5)
return normalDist.mean, normalDist.mean - p, normalDist.mean + p
avg, lower, upper = getCI(data, confidence_interval)
sns.set_style("whitegrid")
plt.figure(figsize=(8, 4))
sns.boxplot(data = data, orient = "h")
plt.axvspan(lower, upper, facecolor='r', alpha=0.4)
plt.axvline(avg, color = 'b', label = 'Average')
plt.ylabel("Operations")
plt.xlabel("Connection Request Duration (ms)")
plt.yticks([0],["Server Retry Request Delay"])
plt.savefig("fig.png")
plt.show()
For Multiple Plots:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
from statistics import NormalDist
X1, X2 = np.random.sample(100), np.random.sample(100)
data1, data2 = ((X1 - min(X1)) / (max(X1) - min(X1))) * 3 + 3, ((X2 - min(X2)) / (max(X2) - min(X2))) * 2 + 3
confidence_interval = 0.95
def getCI(data, ci):
normalDist = NormalDist.from_samples(data)
z = NormalDist().inv_cdf((1 + ci) / 2.)
p = normalDist.stdev * z / ((len(data) - 1) ** .5)
return normalDist.mean, normalDist.mean - p, normalDist.mean + p
sns.set_style("whitegrid")
avg1, lower1, upper1 = getCI(data1, confidence_interval)
avg2, lower2, upper2 = getCI(data2, confidence_interval)
fig = plt.figure(figsize=(12, 6))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212, sharex = ax1, sharey = ax1)
sns.boxplot(data = data1, orient = "h", ax = ax1)
ax1.axvspan(lower1, upper1, facecolor='r', alpha=0.4)
ax1.axvline(avg1, color = 'b', label = 'Average')
sns.boxplot(data = data2, orient = "h", ax = ax2)
ax2.axvspan(lower2, upper2, facecolor='r', alpha=0.4)
ax2.axvline(avg2, color = 'b', label = 'Average')
ax2.set_xlabel("Connection Request Duration (ms)")
plt.setp(ax1.get_xticklabels(), visible=False)
plt.setp(ax1.get_yticklabels(), visible=False)
plt.setp(ax2.get_yticklabels(), visible=False)
fig.text(0.08, 0.5, "Operations", va='center', rotation='vertical')
plt.show()

Related

Matplotlib cmap - custom color definition

I am plotting a graph using matplotlib.
Here is the code:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_title("Grid search results - " + model_name)
ax.set_xlabel("Log10(Wight decay)")
ax.set_ylabel("Log10(Learning rate)")
ax.set_zlabel("Batch size")
ax.set_xticks(weightdecay)
ax.set_yticks(learningrate)
ax.set_zticks(trainbatchsize)
scat_plot = ax.scatter(xs=weightdecay, ys=learningrate, zs=trainbatchsize, c=f1, cmap="bwr")
ax.text(top_score[0], top_score[1], top_score[2], top_score[3], color="black")
cb = plt.colorbar(scat_plot, pad=0.2)
cb.ax.set_xlabel('F1 score')
plt.plot(top_score[0], top_score[1], top_score[2], marker="o", markersize=15, markerfacecolor="yellow")
path = Path(output_dir)
plt.savefig(str(path.absolute()) + '/grid_search_plot_' + model_name + ".pdf")
plt.show()
The graph I am getting looks like:
What I would like to do is to use a more granular color-bar. For example for my F1-score (colour-bar), show in:
color1 scores < 0.5
color2 scores 0.5 - 0.75
color3 scores 0.75 - 0.80
color4 scores 0.8 - 0.85
color5 scores 0.85-1
I was trying to re-use some code to create a custom cmap but nothing was working as expected.
One cheap/quick solution might be to create a "categorical color value", like this:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import ListedColormap
import numpy as np
N = 40
x = np.random.uniform(0, 1, N)
y = np.random.uniform(0, 1, N)
z = np.random.uniform(0, 1, N)
# color values
c = np.random.uniform(0, 1, N)
# new color values
new_col = c.copy()
new_col[c < 0.5] = 0
new_col[(c >= 0.5) & (c < 0.75)] = 1
new_col[(c >= 0.75) & (c < 0.8)] = 2
new_col[(c >= 0.8) & (c < 0.85)] = 3
new_col[c >= 0.85] = 4
new_col = new_col / new_col.max()
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
scatter = ax.scatter(x, y, z, c=new_col, cmap=cm.get_cmap("tab10", 5))
cb = fig.colorbar(scatter)
cb.ax.set_yticklabels([0, 0.5, 0.75, 0.80, 0.85, 1])
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)
EDIT to accommodate comments. The following should be able to deal with cases in which a category doesn't have any element:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import ListedColormap
import numpy as np
N = 40
x = np.random.uniform(0, 1, N)
y = np.random.uniform(0, 1, N)
z = np.random.uniform(0, 1, N)
# color values
c = np.random.uniform(0, 1, N)
# number of categories
NC = 5
# new color values
new_col = c.copy()
new_col[c < 0.5] = 0
new_col[(c >= 0.5) & (c < 0.75)] = 1
new_col[(c >= 0.75) & (c < 0.8)] = 2
new_col[(c >= 0.8) & (c < 0.85)] = 3
new_col[c >= 0.85] = 4
new_col = new_col / NC
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
cmap = ListedColormap(["red", "green", "blue", "magenta", "cyan"])
scatter = ax.scatter(x, y, z, c=cmap(new_col))
cb = fig.colorbar(cm.ScalarMappable(cmap=cmap))
cb.ax.set_yticks(np.linspace(0, 1, NC+1), [0, 0.5, 0.75, 0.80, 0.85, 1])
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("z")
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_zlim(0, 1)

Color map to shades of blue - python

I have the following (example) code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
n_lines = 5
x = np.linspace(0, 10, 100)
y = np.sin(x[:, None] + np.pi * np.linspace(0, 1, n_lines))
c = np.arange(1, n_lines + 1)
norm = mpl.colors.Normalize(vmin=c.min(), vmax=c.max())
cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.jet)
cmap.set_array([])
fig, ax = plt.subplots(dpi=100)
for i, yi in enumerate(y.T):
ax.plot(x, yi, c=cmap.to_rgba(i + 1))
fig.colorbar(cmap, ticks=c)
plt.show();
I would like to find a substirute for cmap.to_rgba that makes the colour of each line come out as a differnet shade of blue. Basically I want to keep the same layout as the result of this code, but using the colour map Blues.
How can I do it?
You need to change your colormap that you are using from jet to Blues.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
n_lines = 5
x = np.linspace(0, 10, 100)
y = np.sin(x[:, None] + np.pi * np.linspace(0, 1, n_lines))
c = np.arange(1, n_lines + 1)
norm = mpl.colors.Normalize(vmin=c.min(), vmax=c.max())
cmap = mpl.cm.ScalarMappable(norm=norm, cmap=mpl.cm.Blues)
cmap.set_array([])
fig, ax = plt.subplots(dpi=100)
for i, yi in enumerate(y.T):
ax.plot(x, yi, c=cmap.to_rgba(i + 1))
fig.colorbar(cmap, ticks=c)
plt.show()
This produces:

Rounding the edges of a cylinder in matplotlib poly3D

I have the following code which produces a cylinder-like object using matplotlib:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
nphi,nz=7,20
r=1 # radius of cylinder
phi = np.linspace(0,360, nphi)/180.0*np.pi
z= np.linspace(0,1.0,nz)
print z
cols=[]
verts2 = []
for i in range(len(phi)-1):
cp0= r*np.cos(phi[i])
cp1= r*np.cos(phi[i+1])
sp0= r*np.sin(phi[i])
sp1= r*np.sin(phi[i+1])
for j in range(len(z)-1):
z0=z[j]
z1=z[j+1]
verts=[]
verts.append((cp0, sp0, z0))
verts.append((cp1, sp1, z0))
verts.append((cp1, sp1, z1))
verts.append((cp0, sp0, z1))
verts2.append(verts)
value=np.random.rand()
#print value
col=plt.cm.rainbow(0.9)
#print col
cols.append(col)
poly3= Poly3DCollection(verts2, facecolor=cols,edgecolor = "none" )
poly3.set_alpha(0.8)
ax.add_collection3d(poly3)
ax.set_xlabel('X')
ax.set_xlim3d(-1, 1)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 1)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
plt.show()
This code produces the following image:
However as you can see the are sharp corners in the figure. Is there anyway to make these edges rounder so that the figure looks like a proper cylinder with a circular cross-section as opposed to a hexagonal cross-section?
The third argument to
np.linspace
controls how many values you want it to generate. Thus, nphi controls the
number of values in phi, and nz controls the number of values in z:
phi = np.linspace(0,360, nphi)/180.0*np.pi
z = np.linspace(0,1.0,nz)
So if you increase nphi, then you'll get more points along the circle:
cp0 = r*np.cos(phi[i])
sp0 = r*np.sin(phi[i])
For example, try changing nphi, nz = 7,20 to nphi, nz = 70, 2.
Note that there is no need for nz to be greater than 2 since the sides of the
cylinder are flat in the z direction.
By the way, the double for-loop can be replaced by:
PHI, Z = np.meshgrid(phi, z)
CP = r * np.cos(PHI)
SP = r * np.sin(PHI)
XYZ = np.dstack([CP, SP, Z])
verts = np.stack(
[XYZ[:-1, :-1], XYZ[:-1, 1:], XYZ[1:, 1:], XYZ[1:, :-1]], axis=-2).reshape(-1, 4, 3)
So, for example,
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
nphi, nz = 70, 2
r = 1 # radius of cylinder
phi = np.linspace(0, 360, nphi) / 180.0 * np.pi
z = np.linspace(0, 1.0, nz)
PHI, Z = np.meshgrid(phi, z)
CP = r * np.cos(PHI)
SP = r * np.sin(PHI)
XYZ = np.dstack([CP, SP, Z])
verts = np.stack(
[XYZ[:-1, :-1], XYZ[:-1, 1:], XYZ[1:, 1:], XYZ[1:, :-1]], axis=-2).reshape(-1, 4, 3)
cmap = plt.cm.rainbow
cols = cmap(np.random.random())
poly3 = Poly3DCollection(verts, facecolor=cols, edgecolor="none")
poly3.set_alpha(0.8)
ax.add_collection3d(poly3)
ax.set_xlabel('X')
ax.set_xlim3d(-1, 1)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 1)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
plt.show()
yields

title and axis gets over each other

I am trying to plot several plots. I want to add to each a title. However, in my code the title and the axis gets over each other. Is there a workaround this?
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
randn = np.random.randn
fig = plt.figure(figsize=(15, 12))
train= df = pd.DataFrame(randn(10, 34))
for i in range(1, train.shape[1]):
plt.subplot(6, 6, i)
f = plt.gca()
f.axes.get_yaxis().set_visible(False)
f.set_title(train.columns.values[i])
vals = np.size(train.iloc[:, i].unique())
if vals < 10:
bins = vals
else:
vals = 10
plt.hist(train.iloc[:, i], bins=30, color='#3F5D7D')
plt.show()
you could try:
plt.tight_layout()
The solution is:
plt.tight_layout()
Here is some good documentation, it has an example that looks like your problem.
http://matplotlib.org/users/tight_layout_guide.html
An alternative solution would be to place your subplots manually in your figure to allow a maximum of flexibility in your layout design. I've put together some code that shows how this can be done. Note that there is a big part of the code that is only to make the xticks format visually appealing.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.close('all')
#------------------------------------------------------------- prepare data ----
randn = np.random.randn
train= df = pd.DataFrame(randn(10, 34))
ngraphs = train.shape[1]
#------------------------------------------------------------ create figure ----
fwidth = 15
fheight = 12
fig = plt.figure(figsize=(fwidth, fheight))
fig.patch.set_facecolor('white')
left_margin = 0.5 / fwidth
right_margin = 0.5 / fwidth
bottom_margin = 0.5 / fheight
top_margin = 0.75 / fheight
vinter_margin = 0.75 / fheight
hinter_margin = 0.5 / fwidth
#-------------------------------------------------------------- create axes ----
ncol = 6
nrow = int(np.ceil(ngraphs/float(ncol)))
w0 = (1 - (left_margin + right_margin + (ncol-1) * hinter_margin)) / ncol
h0 = (1 - (bottom_margin + top_margin + (nrow-1) * vinter_margin)) / nrow
AX0 = [0] * ngraphs
itot = 0
y0 = 1 - top_margin - h0
for row in range(nrow):
x0 = left_margin
for col in range(ncol):
AX0[itot] = fig.add_axes([x0, y0, w0, h0], frameon=True)
#-------------------------------------------------------- plot data ----
vals = np.size(train.iloc[:, itot].unique())
if vals < 10:
bins = vals
else:
vals = 10
AX0[itot].hist(train.iloc[:, itot], bins=30, color='#3F5D7D')
#--------------------------------------------------------- set axis ----
AX0[itot].axes.get_yaxis().set_visible(False)
AX0[itot].set_title(train.columns.values[itot])
#---- major ticks ----
AX0[itot].tick_params(top='off', labeltop='off')
AX0[itot].tick_params(axis='x', direction='out', labelsize=8)
trainmax = np.ceil(np.max(train.iloc[:, itot])/0.5)*0.5
trainmin = np.floor(np.min(train.iloc[:, itot])/0.5)*0.5
AX0[itot].set_xticks([trainmin,0, trainmax])
#---- minor ticks ----
AX0[itot].set_xticks(np.arange(trainmin, trainmax, 0.5), minor=True)
AX0[itot].tick_params(axis='x', which='minor', direction='out',
top='off', length=3)
#---- axis limits ----
AX0[itot].axis(xmin=trainmin, xmax=trainmax)
#---------------------------------------------------------- iterate ----
x0 = x0 + w0 + hinter_margin
itot += 1
if itot == ngraphs:
break
y0 = y0 - h0 - vinter_margin
plt.show(block=False)
fig.savefig('subplot_layout.png')
Which results in:

matplotlib colored segment of a function plot

I wonder if there is a more elegant way to draw the polygon in below code, or with a special plot function or parameter ?
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
x = np.linspace(-4,4,150)
# plot density with shaded area showing Pr(-2 <= x <= 1)
lb = -2
ub = 1
d=norm.pdf(x)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, d)
### can this be done more elegantly ###
sx = np.linespace(lb,ub,100)
sd = norm.pdf(sx)
sx = [lb] + sx + [ub]
sd = [0] + list(sd) + [0]
xy = np.transpose(np.array([sx, sd]))
pgon = plt.Polygon(xy, color='b')
#######################################
ax.add_patch(pgon)
plt.show()
Perhaps you are looking for plt.fill_between:
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
x = np.linspace(-4,4,150)
# plot density with shaded area showing Pr(-2 <= x <= 1)
lb = -2
ub = 1
d = norm.pdf(x)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(x, d)
idx = np.searchsorted(x,[lb,ub])
sx = x[idx[0]:idx[1]]
sd = d[idx[0]:idx[1]]
plt.fill_between(sx, sd, 0, color = 'b')
plt.show()

Categories

Resources