def plot_ellipse(ax, mu, sigma, color="k"):
"""
Based on
http://stackoverflow.com/questions/17952171/not-sure-how-to-fit-data-with-a-gaussian-python.
"""
# Compute eigenvalues and associated eigenvectors
vals, vecs = np.linalg.eigh(sigma)
# Compute "tilt" of ellipse using first eigenvector
x, y = vecs[:, 0]
theta = np.degrees(np.arctan2(y, x))
# Eigenvalues give length of ellipse along each eigenvector
w, h = 2 * np.sqrt(vals)
ax.tick_params(axis='both', which='major', labelsize=20)
ellipse = Ellipse(mu, w, h, theta, color=color) # color="k")
ellipse.set_clip_box(ax.bbox)
ellipse.set_alpha(0.2)
ax.add_artist(ellipse)
python类Ellipse()的实例源码
def plot_ellipse(ax, mu, sigma, color="b"):
"""
Based on
http://stackoverflow.com/questions/17952171/not-sure-how-to-fit-data-with-a-gaussian-python.
"""
# Compute eigenvalues and associated eigenvectors
vals, vecs = np.linalg.eigh(sigma)
# Compute "tilt" of ellipse using first eigenvector
x, y = vecs[:, 0]
theta = np.degrees(np.arctan2(y, x))
# Eigenvalues give length of ellipse along each eigenvector
w, h = 2 * np.sqrt(vals)
ellipse = Ellipse(mu, w, h, theta, color=color) # color="k")
ellipse.set_clip_box(ax.bbox)
ellipse.set_alpha(0.2)
ax.add_artist(ellipse)
def plot_ellipse(ax, mu, sigma, color="b"):
"""
Based on
http://stackoverflow.com/questions/17952171/not-sure-how-to-fit-data-with-a-gaussian-python.
"""
# Compute eigenvalues and associated eigenvectors
vals, vecs = np.linalg.eigh(sigma)
# Compute "tilt" of ellipse using first eigenvector
x, y = vecs[:, 0]
theta = np.degrees(np.arctan2(y, x))
# Eigenvalues give length of ellipse along each eigenvector
w, h = 2 * np.sqrt(vals)
ellipse = Ellipse(mu, w, h, theta, color=color) # color="k")
ellipse.set_clip_box(ax.bbox)
ellipse.set_alpha(0.2)
ax.add_artist(ellipse)
def draw_group(data, panel_params, coord, ax, **params):
data = coord.transform(data, panel_params)
fill = to_rgba(data['fill'], data['alpha'])
color = to_rgba(data['color'], data['alpha'])
ranges = coord.range(panel_params)
# For perfect circles the width/height of the circle(ellipse)
# should factor in the dimensions of axes
bbox = ax.get_window_extent().transformed(
ax.figure.dpi_scale_trans.inverted())
ax_width, ax_height = bbox.width, bbox.height
factor = ((ax_width/ax_height) *
np.ptp(ranges.y)/np.ptp(ranges.x))
size = data.loc[0, 'binwidth'] * params['dotsize']
offsets = data['stackpos'] * params['stackratio']
if params['binaxis'] == 'x':
width, height = size, size*factor
xpos, ypos = data['x'], data['y'] + height*offsets
elif params['binaxis'] == 'y':
width, height = size/factor, size
xpos, ypos = data['x'] + width*offsets, data['y']
circles = []
for xy in zip(xpos, ypos):
patch = mpatches.Ellipse(xy, width=width, height=height)
circles.append(patch)
coll = mcoll.PatchCollection(circles,
edgecolors=color,
facecolors=fill)
ax.add_collection(coll)
def make_handler_map_to_scale_circles_as_in(ax, dont_resize_actively=False):
fig = ax.get_figure()
def axes2pt():
return np.diff(
ax.transData.transform([(0,0), (1,1)]), axis=0)[0] * (72./fig.dpi)
ellipses = []
if not dont_resize_actively:
def update_width_height(event):
dist = axes2pt()
for e, radius in ellipses: e.width, e.height = 2. * radius * dist
fig.canvas.mpl_connect('resize_event', update_width_height)
ax.callbacks.connect('xlim_changed', update_width_height)
ax.callbacks.connect('ylim_changed', update_width_height)
def legend_circle_handler(legend, orig_handle, xdescent, ydescent,
width, height, fontsize):
w, h = 2. * orig_handle.get_radius() * axes2pt()
e = Ellipse(xy=(0.5*width-0.5*xdescent, 0.5*height-0.5*ydescent),
width=w, height=w)
ellipses.append((e, orig_handle.get_radius()))
return e
return {Circle: HandlerPatch(patch_func=legend_circle_handler)}
def plot_scatter(ax, x, y, s=5, c='black', label='', plot_training_cov=False,
model=None):
assert len(x.shape) == 1
assert len(y.shape) == 1
if plot_training_cov is True:
assert model is not None
ax.scatter(x, y, c=c, s=s, label=label)
if plot_training_cov is True:
cov = model.data[label]['cov']
mean_x, mean_y = model.data[label]['mean']
w, h, deg = cov_ellipse(cov, nsig=2)
ell = Ellipse(xy=(mean_x, mean_y),
width=w, height=h,
angle=deg, linewidth=2)
ell.set_facecolor('none')
ell.set_edgecolor('black')
ax.add_patch(ell)
ax.set_aspect('equal')
return ax
def error_ellipse(mu, cov, ax=None, factor=1.0, **kwargs):
"""
Plot the error ellipse at a point given its covariance matrix.
"""
# some sane defaults
facecolor = kwargs.pop('facecolor', 'none')
edgecolor = kwargs.pop('edgecolor', 'k')
x, y = mu
U, S, V = np.linalg.svd(cov)
theta = np.degrees(np.arctan2(U[1, 0], U[0, 0]))
ellipsePlot = Ellipse(xy=[x, y],
width=2 * np.sqrt(S[0]) * factor,
height=2 * np.sqrt(S[1]) * factor,
angle=theta,
facecolor=facecolor, edgecolor=edgecolor, **kwargs)
if ax is None:
ax = plt.gca()
ax.add_patch(ellipsePlot)
return ellipsePlot
def error_ellipse(mu, cov, ax=None, factor=1.0, **kwargs):
"""
Plot the error ellipse at a point given its covariance matrix.
"""
# some sane defaults
facecolor = kwargs.pop('facecolor', 'none')
edgecolor = kwargs.pop('edgecolor', 'k')
x, y = mu
U, S, V = np.linalg.svd(cov)
theta = np.degrees(np.arctan2(U[1, 0], U[0, 0]))
ellipsePlot = Ellipse(xy=[x, y],
width=2 * np.sqrt(S[0]) * factor,
height=2 * np.sqrt(S[1]) * factor,
angle=theta,
facecolor=facecolor, edgecolor=edgecolor, **kwargs)
if ax is None:
ax = pl.gca()
ax.add_patch(ellipsePlot)
return ellipsePlot
def annotate_target(ra, dec, text, ha='left', size=120, color='black',
extended=False, globular=False, padding=0.22, zorder=999):
if extended:
padding = 1.82 * padding
el = Ellipse((ra, dec), width=0.5, height=0.18, zorder=zorder-1,
facecolor='#888888', edgecolor=color, lw=2.)
pl.axes().add_artist(el)
elif globular:
pl.scatter(ra, dec, zorder=zorder-1, marker='o', lw=1.7, s=size, edgecolor=color, color="None")
pl.scatter(ra, dec, zorder=zorder-1, marker='+', lw=1.7, s=size, c=color)
else:
pl.scatter(ra, dec, zorder=zorder-1, marker='o', lw=0, s=size, c=color)
if ha == 'left':
padding = -padding
text = pl.text(ra + padding, dec, text, zorder=zorder, fontsize=22, va='center', ha=ha, color=color)
text.set_path_effects([path_effects.Stroke(linewidth=4, foreground='white'),
path_effects.Normal()])
def plot_fitted_data(points, c_means, c_variances):
"""Plots the data and given Gaussian components"""
plt.plot(points[:, 0], points[:, 1], "b.", zorder=0)
plt.plot(c_means[:, 0], c_means[:, 1], "r.", zorder=1)
for i in range(c_means.shape[0]):
std = np.sqrt(c_variances[i])
plt.axes().add_artist(pat.Ellipse(
c_means[i], 2 * std[0], 2 * std[1],
fill=False, color="red", linewidth=2, zorder=1
))
plt.show()
# PREPARING DATA
# generating DATA_POINTS points from a GMM with COMPONENTS components
def _plot_gaussian(mean, covariance, color, zorder=0):
"""Plots the mean and 2-std ellipse of a given Gaussian"""
plt.plot(mean[0], mean[1], color[0] + ".", zorder=zorder)
if covariance.ndim == 1:
covariance = np.diag(covariance)
radius = np.sqrt(5.991)
eigvals, eigvecs = np.linalg.eig(covariance)
axis = np.sqrt(eigvals) * radius
slope = eigvecs[1][0] / eigvecs[1][1]
angle = 180.0 * np.arctan(slope) / np.pi
plt.axes().add_artist(pat.Ellipse(
mean, 2 * axis[0], 2 * axis[1], angle=angle,
fill=False, color=color, linewidth=1, zorder=zorder
))
def _plot_gaussian(mean, covariance, color, zorder=0):
"""Plots the mean and 2-std ellipse of a given Gaussian"""
plt.plot(mean[0], mean[1], color[0] + ".", zorder=zorder)
if covariance.ndim == 1:
covariance = np.diag(covariance)
radius = np.sqrt(5.991)
eigvals, eigvecs = np.linalg.eig(covariance)
axis = np.sqrt(eigvals) * radius
slope = eigvecs[1][0] / eigvecs[1][1]
angle = 180.0 * np.arctan(slope) / np.pi
plt.axes().add_artist(pat.Ellipse(
mean, 2 * axis[0], 2 * axis[1], angle=angle,
fill=False, color=color, linewidth=1, zorder=zorder
))
def get_plot_buf(x, clusters, mu, logstd, true_mu, true_logstd):
N = x.shape[0]
K = mu.shape[0]
fig = plt.figure()
# print(clusters.shape)
# print(x.shape)
ax = fig.add_subplot(111, aspect='auto')
plt.scatter(x[:, 0], x[:, 1], c=clusters, s=50)
# print(mu, logstd)
ells = [Ellipse(xy=mean_, width=6*np.exp(logstd_[0]), height=6*np.exp(logstd_[1]),
angle=0, facecolor='none', zorder=10, edgecolor='g', label='predict' if i==0 else None)
for i, (mean_, logstd_) in enumerate(zip(mu, logstd))]
true_ells = [Ellipse(xy=mean_, width=6*np.exp(logstd_[0]), height=6*np.exp(logstd_[1]),
angle=0, facecolor='none', zorder=10, edgecolor='r', label='true' if i==0 else None)
for i,(mean_, logstd_) in enumerate(zip(true_mu, true_logstd))]
# print(ells[0])
[ax.add_patch(ell) for ell in ells]
[ax.add_patch(true_ell) for true_ell in true_ells]
ax.legend(loc='best')
ax.set_title('N={},K={}'.format(N, K))
plt.autoscale(True)
buf = io.BytesIO()
fig.savefig(buf, format='png')
plt.close()
buf.seek(0)
return buf
def annotate_ellipse(params, ax=None, crop_radius=1.2, **kwargs):
"""Annotates an ellipse on an image
Parameters
----------
params : tuple or dict
either (yr, xr, yc, xc) tuple
or dict with names ['yr', 'xr', 'yc', 'xc']
"""
from matplotlib.patches import Ellipse
ellipse_style = dict(ec='yellow', fill=False)
ellipse_style.update(kwargs)
if isinstance(params, tuple):
yr, xr, yc, xc = params
else:
yr = params['yr']
xr = params['xr']
yc = params['yc']
xc = params['xc']
ax.add_artist(Ellipse(xy=(xc, yc), width=xr*2, height=yr*2,
**ellipse_style))
# crop image around ellipse
ax.set_xlim(xc - crop_radius * xr, xc + crop_radius * xr)
ax.set_ylim(yc + crop_radius * yr, yc - crop_radius * yr)
return ax
def annotate_ellipsoid(params, axs=None, crop_radius=1.2, **kwargs):
"""Annotates an ellipse on an image
Parameters
----------
params : tuple or dict
either (zr, yr, xr, zc, yc, xc) tuple
or dict with names ['zr', 'yr', 'xr', 'zc', 'yc', 'xc']
"""
from matplotlib.patches import Ellipse
ellipse_style = dict(ec='yellow', fill=False)
ellipse_style.update(kwargs)
ax_xy, ax_zy, ax_zx, ax_extra = axs
if isinstance(params, tuple):
zr, yr, xr, zc, yc, xc = params
else:
zr = params['zr']
yr = params['yr']
xr = params['xr']
zc = params['zc']
yc = params['yc']
xc = params['xc']
ax_xy.add_artist(Ellipse(xy=(xc, yc), width=xr*2, height=yr*2,
**ellipse_style))
ax_zy.add_artist(Ellipse(xy=(zc, yc), width=zr*2, height=yr*2,
**ellipse_style))
ax_zx.add_artist(Ellipse(xy=(xc, zc), width=xr*2, height=zr*2,
**ellipse_style))
# crop image around ellipse
ax_xy.set_xlim(xc - crop_radius * xr, xc + crop_radius * xr)
ax_xy.set_ylim(yc - crop_radius * yr, yc + crop_radius * yr)
ax_zy.set_xlim(zc - crop_radius * zr, zc + crop_radius * zr)
return axs
def make_legend_ellipse(legend, orig_handle, xdescent, ydescent,
width, height, fontsize):
return mpatches.Ellipse(xy=(0.5*width-0.5*xdescent, 0.5*height-0.5*ydescent),
width = width+xdescent, height=(height+ydescent))
def draw_nodes(self):
"""
Draw nodes to screen.
"""
node_r = 1
for i, node in enumerate(self.nodes):
x = self.node_coords['x'][i]
y = self.node_coords['y'][i]
color = self.node_colors[i]
node_patch = patches.Ellipse((x, y), node_r, node_r,
lw=0, color=color, zorder=2)
self.ax.add_patch(node_patch)
def create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize, trans):
center = 0.5 * width - 0.5 * xdescent, 0.5 * height - 0.5 * ydescent
p = mpatches.Ellipse(xy=center, width=width + xdescent,
height=height + ydescent)
self.update_prop(p, orig_handle, legend)
p.set_transform(trans)
return [p]
def plot_ellipse(cov, mean, color):
angle, width, height = equiprobable_ellipse(cov)
e = Ellipse(xy=mean, width=width, height=height, angle=np.degrees(angle),
ec=color, fc="none", lw=3, ls="dashed")
plt.gca().add_artist(e)
def ellipse(ra, rb, ang, x0, y0):
theta = np.arange(0.0, 360.0, 1.0) * np.pi / 180.0
xcenter, ycenter = x0, y0
width = 2*ra
height = 2*rb
x = width * np.cos(theta)
y = height * np.sin(theta)
rtheta = np.radians(ang)
rotation_matrix = np.array([
[np.cos(rtheta), -np.sin(rtheta)],
[np.sin(rtheta), np.cos(rtheta)],
])
x, y = np.dot(rotation_matrix, np.array([x, y]))
x += xcenter
y += ycenter
e1 = patches.Ellipse((xcenter, ycenter), width, height,
angle=ang,
linewidth=1,
fill=False,
linestyle='--',
edgecolor='black')
return e1
def to_mpl_patch(self):
"""
This function ...
:return:
"""
return mpl_Ellipse((self.center.x, self.center.y), self.major_axis_length, self.minor_axis_length, self.angle.to("deg").value, edgecolor='green', facecolor='none', lw=3, alpha=0.7)
# -----------------------------------------------------------------
def to_mpl_patch(self):
"""
This function ...
:return:
"""
return mpl_Ellipse((self.center.x, self.center.y), self.major_axis_length, self.minor_axis_length, self.angle.to("deg").value, edgecolor='green', facecolor='none', lw=3, alpha=0.7)
# -----------------------------------------------------------------
def draw_ellipse(fig, ax, x, y, w, h, a, fillcolor, bordercolor):
e = patches.Ellipse(
xy=(x, y),
width=w,
ls='solid',
lw=1.0,
ec=bordercolor,
height=h,
angle=a,
color=fillcolor)
ax.add_patch(e)
def _plotCovarianceEllipse(self, eta2):
from matplotlib.patches import Ellipse
lambda_, _ = np.linalg.eig(self.kalmanFilter.S)
ell = Ellipse(xy=(self.kalmanFilter.x_bar[0], self.kalmanFilter.x_bar[1]),
width=np.sqrt(lambda_[0]) * np.sqrt(eta2) * 2,
height=np.sqrt(lambda_[1]) * np.sqrt(eta2) * 2,
angle=np.rad2deg(np.arctan2(lambda_[1], lambda_[0])),
linewidth=2,
)
ell.set_facecolor('none')
ell.set_linestyle("dotted")
ell.set_alpha(0.5)
ax = plt.subplot(111)
ax.add_artist(ell)
def plot_cov_ellipse(cov, pos, nstd=2, **kwargs):
"""Plot confidence ellipse."""
r1, r2, theta = cov_ellipse(cov, nstd)
ellip = Ellipse(xy=pos, width=2*r1, height=2*r2, angle=theta, **kwargs)
plt.gca().add_artist(ellip)
return ellip
def _plot_cov_ellipse(cov, pos, nstd=2, ax=None, **kwargs):
"""
Plots an `nstd` sigma error ellipse based on the specified covariance
matrix (`cov`). Additional keyword arguments are passed on to the
ellipse patch artist.
Parameters
----------
cov : The 2x2 covariance matrix to base the ellipse on
pos : The location of the center of the ellipse. Expects a 2-element
sequence of [x0, y0].
nstd : The radius of the ellipse in numbers of standard deviations.
Defaults to 2 standard deviations.
ax : The axis that the ellipse will be plotted on. Defaults to the
current axis.
Additional keyword arguments are pass on to the ellipse patch.
Returns
-------
A matplotlib ellipse artist
"""
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse
def eigsorted(cov):
vals, vecs = np.linalg.eigh(cov)
order = vals.argsort()[::-1]
return vals[order], vecs[:, order]
if ax is None:
ax = plt.gca()
vals, vecs = eigsorted(cov)
theta = np.degrees(np.arctan2(*vecs[:, 0][::-1]))
# Width and height are "full" widths, not radius
width, height = 2 * nstd * np.sqrt(vals)
ellip = Ellipse(xy=pos, width=width, height=height, angle=theta,
**kwargs)
ax.add_artist(ellip)
return ellip
def plot_cov_ellipse(cov, pos, nstd=2, ax=None, **kwargs):
"""
Plots an `nstd` sigma error ellipse based on the specified covariance
matrix (`cov`). Additional keyword arguments are passed on to the
ellipse patch artist.
Parameters
----------
cov : The 2x2 covariance matrix to base the ellipse on
pos : The location of the center of the ellipse. Expects a 2-element
sequence of [x0, y0].
nstd : The radius of the ellipse in numbers of standard deviations.
Defaults to 2 standard deviations.
ax : The axis that the ellipse will be plotted on. Defaults to the
current axis.
Additional keyword arguments are pass on to the ellipse patch.
Returns
-------
A matplotlib ellipse artist
"""
def eigsorted(cov):
vals, vecs = np.linalg.eigh(cov)
order = vals.argsort()[::-1]
return vals[order], vecs[:,order]
if ax is None:
ax = plt.gca()
vals, vecs = eigsorted(cov)
theta = np.degrees(np.arctan2(*vecs[:,0][::-1]))
# Width and height are "full" widths, not radius
width, height = 2 * nstd * np.sqrt(vals)
ellip = Ellipse(xy=pos, width=width, height=height, angle=theta, **kwargs)
ax.add_artist(ellip)
return ellip
def plot_cov_ellipse(cov, pos, nstd=2, **kwargs):
"""Plot confidence ellipse."""
r1, r2, theta = cov_ellipse(cov, nstd)
ellip = Ellipse(xy=pos, width=2*r1, height=2*r2, angle=theta, **kwargs)
plt.gca().add_artist(ellip)
return ellip
def draw_ellipse(fig, ax, x, y, w, h, a, fillcolor):
e = patches.Ellipse(
xy=(x, y),
width=w,
height=h,
angle=a,
color=fillcolor)
ax.add_patch(e)
def run(n, gen, figpos, xlabel, ylabel, xlo, xhi, ylo, yhi):
obs = gen(n)
s = SPN(3, 1, SPNParams(mvmaxscope=0))
s.update(obs)
s.display()
ells = []
for x in s.root.children[0].children[0].children[1].children:
if x.children[0].index == 0:
c2, c1 = x.children
else:
c1, c2 = x.children
ell = Ellipse(xy=[c1.mean, c2.mean], width=np.sqrt(c1.var),
height=np.sqrt(c2.var), fill=False, linewidth=3,
zorder=2, color='r')
ells.append(ell)
fig = plt.figure(0)
ax = fig.add_subplot(figpos, aspect='equal')
ax.plot(obs[:,1], obs[:,0], '.')
for e in ells:
ax.add_artist(e)
ax.set_xlim(xlo, xhi)
ax.set_ylim(ylo, yhi)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)