本文整理匯總了Python中matplotlib.patches.Rectangle方法的典型用法代碼示例。如果您正苦於以下問題:Python patches.Rectangle方法的具體用法?Python patches.Rectangle怎麽用?Python patches.Rectangle使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類matplotlib.patches
的用法示例。
在下文中一共展示了patches.Rectangle方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _plot_box_with_label
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def _plot_box_with_label(self, time_start, time_end,
n_qubit_start, n_qubit_end, label, **kwargs):
"""
Parameters
----------
time_start : float
time_end : float
n_qubit_start : int
n_qubit_end : int
label : str
kwargs
"""
box_y = n_qubit_start - 0.5 * _golden_mean
box_dy = n_qubit_end - n_qubit_start + _golden_mean
box_x = time_start + 0.5*self.gate_offset
box_dx = time_end - time_start - self.gate_offset
rect = Rectangle((box_x, box_y), box_dx, box_dy,
**kwargs)
self.ax.add_patch(rect)
if label is not None:
self.ax.text(box_x + 0.5 * box_dx, box_y + 0.5 * box_dy,
label, ha='center', va='center',
zorder=self.zorders['text'])
示例2: test_SquareElectrode
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def test_SquareElectrode():
with pytest.raises(TypeError):
SquareElectrode(0, 0, 0, [1, 2])
with pytest.raises(TypeError):
SquareElectrode(0, np.array([0, 1]), 0, 1)
# Invalid radius:
with pytest.raises(ValueError):
SquareElectrode(0, 0, 0, -5)
# Check params:
electrode = SquareElectrode(0, 1, 2, 100)
npt.assert_almost_equal(electrode.x, 0)
npt.assert_almost_equal(electrode.y, 1)
npt.assert_almost_equal(electrode.z, 2)
npt.assert_almost_equal(electrode.a, 100)
# Slots:
npt.assert_equal(hasattr(electrode, '__slots__'), True)
npt.assert_equal(hasattr(electrode, '__dict__'), False)
# Plots:
ax = electrode.plot()
npt.assert_equal(len(ax.texts), 0)
npt.assert_equal(len(ax.patches), 1)
npt.assert_equal(isinstance(ax.patches[0], Rectangle), True)
示例3: __init__
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def __init__(self, x, y, z):
if isinstance(x, (Sequence, np.ndarray)):
raise TypeError("x must be a scalar, not %s." % (type(x)))
if isinstance(y, (Sequence, np.ndarray)):
raise TypeError("y must be a scalar, not %s." % type(y))
if isinstance(z, (Sequence, np.ndarray)):
raise TypeError("z must be a scalar, not %s." % type(z))
self.x = x
self.y = y
self.z = z
# A matplotlib.patches object (e.g., Circle, Rectangle) that can be
# used to plot the electrode:
self.plot_patch = None
# Any keyword arguments that should be passed to the call above:
# (e.g., {'radius': 5}):
self.plot_kwargs = {}
示例4: draw_boxes
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def draw_boxes(filename, v_boxes, v_labels, v_scores, output_photo_name):
# load the image
data = pyplot.imread(filename)
# plot the image
pyplot.imshow(data)
# get the context for drawing boxes
ax = pyplot.gca()
# plot each box
for i in range(len(v_boxes)):
box = v_boxes[i]
# get coordinates
y1, x1, y2, x2 = box.ymin, box.xmin, box.ymax, box.xmax
# calculate width and height of the box
width, height = x2 - x1, y2 - y1
# create the shape
rect = Rectangle((x1, y1), width, height, fill=False, color='white')
# draw the box
ax.add_patch(rect)
# draw text and score in top left corner
label = "%s (%.3f)" % (v_labels[i], v_scores[i])
pyplot.text(x1, y1, label, color='white')
# show the plot
#pyplot.show()
pyplot.savefig(output_photo_name)
示例5: box2rect
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def box2rect(self, box2d):
"""generate individual bounding box from label"""
x1 = box2d['x1']
y1 = box2d['y1']
x2 = box2d['x2']
y2 = box2d['y2']
# Pick random color for each box
box_color = random_color()
# Draw and add one box to the figure
return mpatches.Rectangle(
(x1, y1), x2 - x1, y2 - y1,
linewidth=3 * self.scale, edgecolor=box_color, facecolor='none',
fill=False, alpha=0.75
)
示例6: show_img
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def show_img(img, object_lb_x1, object_lb_y1, object_width, object_height):
fig, ax = plt.subplots(1)
ax.imshow(img)
rect = patches.Rectangle(
(object_lb_x1, object_lb_y1),
object_width,
object_height,
linewidth=1,
edgecolor='r',
facecolor='none'
)
ax.add_patch(rect)
plt.show()
# CHeck for the case 3_11_20 is in 3_11_n
# This method converts the specific obj_class to the common one
# using traffic_sign_classes data structure.
示例7: __init__
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def __init__(self, transform, size, label, loc,
pad=0.1, borderpad=0.1, sep=2, prop=None, frameon=True,
**kwargs):
"""
Draw a horizontal bar with the size in data coordinate of the give axes.
A label will be drawn underneath (center-aligned).
pad, borderpad in fraction of the legend font size (or prop)
sep in points.
"""
self.size_bar = AuxTransformBox(transform)
self.size_bar.add_artist(Rectangle((0,0), size, 0, fc="none"))
self.txt_label = TextArea(label, minimumdescent=False)
self._box = VPacker(children=[self.size_bar, self.txt_label],
align="center",
pad=0, sep=sep)
AnchoredOffsetbox.__init__(self, loc, pad=pad, borderpad=borderpad,
child=self._box,
prop=prop,
frameon=frameon, **kwargs)
示例8: _gen_axes_patch
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def _gen_axes_patch(self):
"""
Returns
-------
Patch
The patch used to draw the background of the axes. It is also used
as the clipping path for any data elements on the axes.
In the standard axes, this is a rectangle, but in other projections
it may not be.
Notes
-----
Intended to be overridden by new projection types.
"""
return mpatches.Rectangle((0.0, 0.0), 1.0, 1.0)
示例9: read_JSON_file
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def read_JSON_file(f):
with open(f, 'r') as g:
d = json.loads(g.read())
img_paths = [x['file_name'] for x in d['images']]
rects = [{'bbox': x['segmentation'][0], 'class': x['category_id'], 'image_id': x['image_id']} for x in d['annotations']]
annotations = defaultdict(list)
for rect in rects:
r = rect['bbox']
x0, y0 = min(r[0], r[2], r[4], r[6]), min(r[1], r[3], r[5], r[7])
x1, y1 = max(r[0], r[2], r[4], r[6]), max(r[1], r[3], r[5], r[7])
r = patches.Rectangle((x0,y0),x1-x0,y1-y0,linewidth=1,edgecolor='g',facecolor='g', alpha=0.4)
annotations[img_paths[rect['image_id']]].append({'bbox': r, 'cls': d['classes'][rect['class']-1]})
return d['classes'], img_paths, annotations
示例10: plot_renko
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def plot_renko(ax, bricks):
ymax = max(bricks)
ymin = min(np.absolute(bricks))
width = 1.0 / len(bricks)
prev_height = 0
for index, brick in enumerate(bricks):
facecolor = 'red' if brick > 0 else 'green'
ypos = (abs(brick) - ymin) / (ymax - ymin)
if index == len(bricks)-1:
pass
elif bricks[index] == bricks[index+1]:
height = prev_height
else:
aux1 = (abs(bricks[index+1]) - ymin) / (ymax - ymin)
height = abs(aux1 - ypos)
prev_height = height
rect = Rectangle((index * width, ypos), width, height,
facecolor=facecolor, alpha=0.5)
ax.add_patch(rect)
pass
示例11: show_frame
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def show_frame(self):
self.capture.set(cv2.CAP_PROP_POS_FRAMES, self.current_frame_num)
_, frame = self.capture.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.ax.imshow(frame)
if self.is_recording:
current_width = self.br_record[0] - self.tl_record[0]
current_height = self.br_record[1] - self.tl_record[1]
self.rect_patch = patches.Rectangle(self.tl_record,
current_width, current_height,
linewidth=1, edgecolor='r', facecolor='none')
self.ax.add_patch(self.rect_patch)
plt.draw()
# Function called when left mouse button is clicked and released.
示例12: on_press
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [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)
示例13: on_press_draw
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [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)
示例14: _plot_bursts
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def _plot_bursts(d, i, tmin_clk, tmax_clk, pmax=1e3, pmin=0, color="#999999",
ytext=20):
"""Highlights bursts in a timetrace plot."""
b = d.mburst[i]
if b.num_bursts == 0:
return
burst_mask = (tmin_clk < b.start) * (b.start < tmax_clk)
bs = b[burst_mask]
burst_indices = np.where(burst_mask)[0]
start = bs.start * d.clk_p
end = bs.stop * d.clk_p
R = []
width = end - start
ax = gca()
for b, bidx, s, w, sign, va in zip(bs, burst_indices, start, width,
cycle([-1, 1]),
cycle(['top', 'bottom'])):
r = Rectangle(xy=(s, pmin), height=pmax - pmin, width=w)
r.set_clip_box(ax.bbox)
r.set_zorder(0)
R.append(r)
ax.text(s, sign * ytext, _burst_info(d, i, bidx), fontsize=6, rotation=45,
horizontalalignment='center', va=va)
ax.add_artist(PatchCollection(R, lw=0, color=color))
示例15: UpdateImage
# 需要導入模塊: from matplotlib import patches [as 別名]
# 或者: from matplotlib.patches import Rectangle [as 別名]
def UpdateImage(x, y):
if(type(x) == None.__class__ or type(x) == None.__class__):
return
x = int(round(x))
y = int(round(y))
image_y = -(y + 1)
if (image_y - 2) < 0 or image_y >= image_height or (x + 2) >= image_width:
return
axes.add_patch(patches.Rectangle((x , y), 3, 3))
plt.draw()
image[image_y][x] = image[image_y][x + 1] = image[image_y][x + 2] = gray
image[image_y - 1][x] = image[image_y - 1][x + 1] = image[image_y - 1][x + 2] = gray
image[image_y - 2 ][x] = image[image_y - 2][x + 1] = image[image_y - 2][x + 2] = gray