本文整理汇总了Python中matplotlib.patches.Ellipse方法的典型用法代码示例。如果您正苦于以下问题:Python patches.Ellipse方法的具体用法?Python patches.Ellipse怎么用?Python patches.Ellipse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类matplotlib.patches
的用法示例。
在下文中一共展示了patches.Ellipse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _plot_gaussian
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
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
))
示例2: plot_fitted_data
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
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
示例3: _make_ellipse
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def _make_ellipse(mean, cov, ax, level=0.95, color=None):
"""Support function for scatter_ellipse."""
from matplotlib.patches import Ellipse
v, w = np.linalg.eigh(cov)
u = w[0] / np.linalg.norm(w[0])
angle = np.arctan(u[1]/u[0])
angle = 180 * angle / np.pi # convert to degrees
v = 2 * np.sqrt(v * stats.chi2.ppf(level, 2)) #get size corresponding to level
ell = Ellipse(mean[:2], v[0], v[1], 180 + angle, facecolor='none',
edgecolor=color,
#ls='dashed', #for debugging
lw=1.5)
ell.set_clip_box(ax.bbox)
ell.set_alpha(0.5)
ax.add_artist(ell)
示例4: on_press
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def on_press(event):
if event.inaxes != GSel.ax: return
GSel.pressed = True
GSel.xs, GSel.ys = event.xdata, event.ydata
#print 'PRESS button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (
# event.button, event.x, event.y, event.xdata, event.ydata)
if hasattr(GSel, 'r'):
GSel.r.set_height(0); GSel.r.set_width(0)
GSel.r.set_xy((GSel.xs,GSel.ys))
GSel.e.height = 0; GSel.e.width = 0
GSel.e.center = (GSel.xs,GSel.ys)
else:
GSel.r = Rectangle(xy=(GSel.xs,GSel.ys), height=0, width=0,
fill=False, lw=2, alpha=0.6, color='blue')
GSel.e = Ellipse(xy=(GSel.xs,GSel.ys), height=0, width=0,
fill=False, lw=2, alpha=0.6, color='blue')
GSel.ax.add_artist(GSel.r)
GSel.r.set_clip_box(GSel.ax.bbox); GSel.r.set_zorder(10)
GSel.ax.add_artist(GSel.e)
GSel.e.set_clip_box(GSel.ax.bbox); GSel.e.set_zorder(10)
GSel.fig.canvas.draw()
GSel.id_motion = GSel.fig.canvas.mpl_connect('motion_notify_event',
on_motion)
示例5: on_press_draw
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def on_press_draw(self):
if 'r' in self.__dict__:
self.r.set_height(0)
self.r.set_width(0)
self.r.set_xy((self.xs, self.ys))
self.e.height = 0
self.e.width = 0
self.e.center = (self.xs, self.ys)
else:
self.r = Rectangle(xy=(self.xs, self.ys), height=0, width=0,
fill=False, lw=2, alpha=0.5, color='blue')
self.e = Ellipse(xy=(self.xs, self.ys), height=0, width=0,
fill=False, lw=2, alpha=0.6, color='blue')
self.ax.add_artist(self.r)
self.ax.add_artist(self.e)
self.r.set_clip_box(self.ax.bbox)
self.r.set_zorder(10)
self.e.set_clip_box(self.ax.bbox)
self.e.set_zorder(10)
示例6: make_ellipses
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def make_ellipses(gmm, pl, colors):
covariances = None
for k in range(gmm.n_components):
color = colors[k]
# logger.debug("color: %s" % color)
if gmm.covariance_type == 'full':
covariances = gmm.covariances_[k][:2, :2]
elif gmm.covariance_type == 'tied':
covariances = gmm.covariances_[:2, :2]
elif gmm.covariance_type == 'diag':
covariances = np.diag(gmm.covariances_[k][:2])
elif gmm.covariance_type == 'spherical':
covariances = np.eye(gmm.means_.shape[1]) * gmm.covariances_[k]
# find the ellipse size and orientation w.r.t largest eigen value
v, w = np.linalg.eigh(covariances)
u = w[0] / np.linalg.norm(w[0]) # normalize direction of largest eigen value
angle = np.arctan2(u[1], u[0]) # find direction of the vector with largest eigen value
angle = 180 * angle / np.pi # convert to degrees
v = 2. * np.sqrt(2.) * np.sqrt(v)
ell = Ellipse(xy=gmm.means_[k, :2], width=v[0], height=v[1], angle=180 + angle,
edgecolor=color, facecolor='none', linewidth=2)
ell.set_clip_box(pl.bbox)
# ell.set_alpha(0.5)
# ell.set_facecolor('none')
pl.add_artist(ell)
示例7: set_selected
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def set_selected(self, shape):
self.deSelectShape()
self.picked = True
self.toggle_selector_ES.set_active(False)
self.toggle_selector_RS.set_active(False)
self.toggle_selector_LS.set_active(False)
self.selectedShape = shape
if type(self.selectedShape) is Rectangle or Ellipse:
self.selectedShape.set_edgecolor('black')
self.draw_idle()
self.set_state(2)
self.edit_selectedShape(self.selectedShape)
elif type(self.selectedShape) is PathPatch:
self.selectedShape.set_edgecolor('black')
self.draw_idle()
self.set_state(2)
self.selectionChanged.emit(True)
示例8: selectShape
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def selectShape(self, event):
self.deSelectShape()
self.picked = True
self.toggle_selector_ES.set_active(False)
self.toggle_selector_RS.set_active(False)
self.toggle_selector_LS.set_active(False)
self.selectedShape = event.artist
if type(self.selectedShape) is Rectangle or Ellipse:
self.selectedShape.set_edgecolor('black')
self.draw_idle()
self.set_state(2)
self.edit_selectedShape(self.selectedShape)
elif type(self.selectedShape) is PathPatch:
self.selectedShape.set_edgecolor('black')
self.draw_idle()
self.set_state(2)
self.selectionChanged.emit(True)
示例9: _edit_on_release
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def _edit_on_release(self, event):
self.picked = False
self.set_state(1)
# turn off the rect animation property and reset the background
self.to_draw.set_animated(False)
try:
canvas = self.to_draw.get_figure().canvas
axes = self.to_draw.axes
self.background = canvas.copy_from_bbox(self.selectedShape.axes.bbox)
canvas.restore_region(self.background)
axes.draw_artist(self.to_draw)
axes.draw_artist(self._corner_handles.artist)
axes.draw_artist(self._edge_handles.artist)
axes.draw_artist(self._center_handle.artist)
# blit just the redrawn area
canvas.blit(axes.bbox)
except:
pass
self.df = pandas.read_csv('Markings/marking_records.csv')
if type(self.to_draw) is Rectangle or Ellipse:
self.df.loc[self.selectind, 'artist'] = self.to_draw
self.df.to_csv('Markings/marking_records.csv', index=False)
示例10: ell_onselect
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def ell_onselect(self, eclick, erelease):
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
ell = Ellipse(xy=(min(x1, x2) + np.abs(x1 - x2) / 2, min(y1, y2) + np.abs(y1 - y2) / 2),
width=np.abs(x1 - x2), height=np.abs(y1 - y2), alpha=.2, edgecolor=None)
plist = np.ndarray.tolist(ell.get_path().vertices)
plist = ', '.join(str(x) for x in plist)
self.ax1.add_patch(ell)
self.figure.canvas.draw()
if self.mode == 1 or self.mode == 4 or self.mode == 7:
onslice = 'Z %s' % (self.ind + 1)
elif self.mode == 2 or self.mode == 5 or self.mode == 8:
onslice = 'X %s' % (self.ind + 1)
elif self.mode == 3 or self.mode == 6 or self.mode == 9:
onslice = 'Y %s' % (self.ind + 1)
self.df = pandas.read_csv('Markings/marking_records.csv')
df_size = pandas.DataFrame.count(self.df)
df_rows = df_size['artist']
self.df.loc[df_rows, 'artist'] = ell
self.df.loc[df_rows, 'labelshape'] = 'ell'
self.df.loc[df_rows, 'slice'] = onslice
self.df.loc[df_rows, 'path'] = plist
self.df.loc[df_rows, 'status'] = 0
self.df.to_csv('Markings/marking_records.csv', index=False)
示例11: draw_ellipse
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def draw_ellipse(position, covariance, ax=None, color=None, **kwargs):
"""Draw an ellipse with a given position and covariance"""
ax = ax or plt.gca()
covariance = covariance[0:2,0:2]
position = position[0:2]
# Convert covariance to principal axes
if covariance.shape == (2, 2):
U, s, Vt = np.linalg.svd(covariance)
angle = np.degrees(np.arctan2(U[1, 0], U[0, 0]))
width, height = 2 * np.sqrt(s)
else:
angle = 0
width, height = 2 * np.sqrt(covariance)
# Draw the Ellipse
for nsig in range(2, 3):
ax.add_patch(Ellipse(position, nsig * width, nsig * height,
angle, **kwargs, color=color))
示例12: draw_sky
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def draw_sky( galaxies ):
"""adapted from Vishal Goklani"""
size_multiplier = 45
fig = plt.figure(figsize=(10,10))
#fig.patch.set_facecolor("blue")
ax = fig.add_subplot(111, aspect='equal')
n = galaxies.shape[0]
for i in xrange(n):
_g = galaxies[i,:]
x,y = _g[0], _g[1]
d = np.sqrt( _g[2]**2 + _g[3]**2 )
a = 1.0/ ( 1 - d )
b = 1.0/( 1 + d)
theta = np.degrees( np.arctan2( _g[3], _g[2])*0.5 )
ax.add_patch( Ellipse(xy=(x, y), width=size_multiplier*a, height=size_multiplier*b, angle=theta) )
ax.autoscale_view(tight=True)
return fig
开发者ID:mauricevb80,项目名称:Probabilistic-Programming-and-Bayesian-Methods-for-Hackers,代码行数:21,代码来源:draw_sky2.py
示例13: error_ellipse
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
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
示例14: show_dataset
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
def show_dataset():
fig, ax = plt.subplots(figsize=(20, 15))
g1 = Ellipse(xy=m1, width=3 * np.sqrt(c1[0, 0]), height=3 * np.sqrt(c1[1, 1]), fill=False, linestyle='dashed',
linewidth=1)
g1_1 = Ellipse(xy=m1, width=2 * np.sqrt(c1[0, 0]), height=2 * np.sqrt(c1[1, 1]), fill=False, linestyle='dashed',
linewidth=2)
g1_2 = Ellipse(xy=m1, width=1.4 * np.sqrt(c1[0, 0]), height=1.4 * np.sqrt(c1[1, 1]), fill=False, linestyle='dashed',
linewidth=3)
g2 = Ellipse(xy=m2, width=3 * np.sqrt(c2[0, 0]), height=3 * np.sqrt(c2[1, 1]), fill=False, linestyle='dashed',
linewidth=1)
g2_1 = Ellipse(xy=m2, width=2 * np.sqrt(c2[0, 0]), height=2 * np.sqrt(c2[1, 1]), fill=False, linestyle='dashed',
linewidth=2)
g2_2 = Ellipse(xy=m2, width=1.4 * np.sqrt(c2[0, 0]), height=1.4 * np.sqrt(c2[1, 1]), fill=False, linestyle='dashed',
linewidth=3)
ax.scatter(X[Y == 0, 0], X[Y == 0, 1], color='#88d7f0', s=100)
ax.scatter(X[Y == 1, 0], X[Y == 1, 1], color='#55ffec', s=100)
ax.scatter(X[Y == -1, 0], X[Y == -1, 1], color='r', marker='d', s=25)
ax.add_artist(g1)
ax.add_artist(g1_1)
ax.add_artist(g1_2)
ax.add_artist(g2)
ax.add_artist(g2_1)
ax.add_artist(g2_2)
ax.set_xlabel(r'$x_0$')
ax.set_ylabel(r'$x_1$')
ax.grid()
plt.show()
开发者ID:PacktPublishing,项目名称:Mastering-Machine-Learning-Algorithms,代码行数:35,代码来源:generative_gaussian_mixtures.py
示例15: plot_cov_ellipse
# 需要导入模块: from matplotlib import patches [as 别名]
# 或者: from matplotlib.patches import Ellipse [as 别名]
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