def draw_features(features, ax=None, alpha=0.65, width=2e-5, **kwargs):
if ax is None:
fig, ax = plt.subplots(1)
ellipses = []
kwargs.setdefault("lw", 0.05)
lw = kwargs.get("linewidth", kwargs.get("lw"))
for feat in features:
if feat is None:
continue
center = (feat.end_time + feat.start_time) / 2.
height = feat.end_time - feat.start_time
center_mz = feat.mz
mz_width = center_mz * width
ellipses.append(
FancyBboxPatch((feat.mz - mz_width / 4., center - height / 2.), width=mz_width / 2., height=height,
boxstyle=mpatches.BoxStyle.Round(pad=mz_width / 2.)))
for ell in ellipses:
ell.set_alpha(alpha)
ell.set_facecolor("blue")
ell.set_edgecolor("blue")
ell.set_linewidth(lw)
ax.add_artist(ell)
ax.set_xlim(
min(features, key=lambda x: x.mz if x is not None else float('inf')).mz - 1,
max(features, key=lambda x: x.mz if x is not None else -float('inf')).mz + 1)
ax.set_ylim(
min(features, key=lambda x: x.start_time if x is not None else float('inf')).start_time - 1,
max(features, key=lambda x: x.end_time if x is not None else -float('inf')).end_time + 1)
return ax
python类FancyBboxPatch()的实例源码
def draw_strip_text(self, text_lines, location, pid):
"""
Create a background patch and put a label on it
"""
ax = self.axs[pid]
themeable = self.figure._themeable
dim = self.strip_dimensions(text_lines, location, pid)
if location == 'right':
rotation = -90
label = '\n'.join(reversed(text_lines))
else:
rotation = 0
label = '\n'.join(text_lines)
rect = mpatch.FancyBboxPatch((dim.box_x, dim.box_y),
width=dim.box_width,
height=dim.box_height,
facecolor='lightgrey',
edgecolor='None',
transform=ax.transAxes,
zorder=2.2, # > ax line & boundary
boxstyle='square, pad=0',
clip_on=False)
text = mtext.Text(dim.x, dim.y, label,
rotation=rotation,
verticalalignment='center',
horizontalalignment='center',
transform=ax.transAxes,
zorder=3.3, # > rect
clip_on=False)
ax.add_artist(rect)
ax.add_artist(text)
for key in ('strip_text_x', 'strip_text_y',
'strip_background_x', 'strip_background_y'):
if key not in themeable:
themeable[key] = []
if location == 'right':
themeable['strip_background_y'].append(rect)
themeable['strip_text_y'].append(text)
else:
themeable['strip_background_x'].append(rect)
themeable['strip_text_x'].append(text)
def draw_small_clustering(clustering, axs=None, params=None):
""" given a mem_dict and an axis object, draw the clustering """
# processing the parameters.
if params is None:
params = {}
w_padding = params.get('w_padding', 0.05)
fontsize = params.get('fontsize', 10)
cmap = params.get('cmap', 'jet')
alpha = params.get('alpha', 0.3)
xlim = params.get('xlim', (-0.07, 1))
ylim = params.get('xlim', (-0.1, 0.1))
boxstyle = params.get('boxstyle', mpatches.BoxStyle("Round", pad=0.02))
xmin = 0.0 + w_padding
xmax = 1.0 - w_padding
xspacing = (xmax - xmin)/float(clustering.number_of_elements())
# create ax object if there is none provided.
if axs is None:
_, axs = plt.subplots(1, 1, figsize=(10, 1))
axs = blank_axis(axs)
axs.set_xlim(*xlim)
axs.set_ylim(*ylim)
patches = []
for _, elms in sorted(iteritems(clustering.clu_dict),
key=lambda x: int(x[0].strip('.'))):
cstart = xmin + min(elms) * xspacing #- 0.95 * w_padding
clength = (max(elms) - min(elms)) * xspacing
fancybox = mpatches.FancyBboxPatch([cstart, -0.05],
clength,
0.1,
boxstyle=boxstyle)
patches.append(fancybox)
colors = np.linspace(0, 1, len(patches))
collection = PatchCollection(patches, cmap=cmap, alpha=alpha)
collection.set_array(np.array(colors))
axs.add_collection(collection)
for elm_idx, elm in enumerate(sorted(clustering.elements)):
axs.text(xmin + elm_idx * xspacing,
0.0, str(elm), ha='center', va='center', fontsize=fontsize)
def draw_feature_sets(feature_sets, ax=None, alpha=0.65, width=2e-5, **kwargs):
if ax is None:
fig, ax = plt.subplots(1)
kwargs.setdefault("lw", 0.05)
lw = kwargs.get("linewidth", kwargs.get("lw"))
features = []
ellipse_sets = []
for feature_set in feature_sets:
ellipses = []
for feat in feature_set:
if feat is None:
continue
center = (feat.end_time + feat.start_time) / 2.
height = feat.end_time - feat.start_time
center_mz = feat.mz
mz_width = center_mz * width
ellipses.append(
FancyBboxPatch((feat.mz - mz_width / 4., center - height / 2.),
width=mz_width / 2., height=height,
boxstyle=mpatches.BoxStyle.Round(pad=mz_width / 2.)))
features.append(feat)
ellipse_sets.append(ellipses)
for ellipses in ellipse_sets:
color = np.random.rand(3.)
for ell in ellipses:
ell.set_alpha(alpha)
ell.set_facecolor(color)
ell.set_edgecolor(color)
ell.set_linewidth(lw)
ax.add_artist(ell)
ax.set_xlim(
min(features, key=lambda x: x.mz if x is not None else float('inf')).mz - 1,
max(features, key=lambda x: x.mz if x is not None else -float('inf')).mz + 1)
ax.set_ylim(
min(features, key=lambda x: x.start_time if x is not None else float('inf')).start_time - 1,
max(features, key=lambda x: x.end_time if x is not None else -float('inf')).end_time + 1)
return ax
def draw(self):
self.clear()
self._mplot_objects['text'] = []
UIM = UIManager()
controller = UIM.get(self._controller_uid)
toc_uid = UIM._getparentuid(self._controller_uid)
track_controller_uid = UIM._getparentuid(toc_uid)
track_controller = UIM.get(track_controller_uid)
y_min = controller._data[0]
y_max = controller._data[-1]
if y_min%controller.model.step:
y_min = (y_min//controller.model.step + 1) * controller.model.step
y_positions = np.arange(y_min, y_max, controller.model.step)
for pos_y in y_positions:
text = track_controller._append_artist('Text',
controller.model.pos_x, pos_y,
"%g"%pos_y,
color=controller.model.color,
horizontalalignment=controller.model.ha,
verticalalignment=controller.model.va,
fontsize=controller.model.fontsize
)
if controller.model.bbox:
pad = 0.2
boxstyle = controller.model.bbox_style
boxstyle += ",pad=%0.2f" % pad
text._bbox_patch = FancyBboxPatch(
(0., 0.),
1., 1.,
boxstyle=boxstyle,
color=controller.model.bbox_color,
alpha=controller.model.bbox_alpha
)
#text.zorder = controller.model.zorder
self._mplot_objects['text'].append(text)
try:
obj = controller.get_object()
self.set_title(obj.name)
self.set_subtitle(obj.unit)
except:
pass
self.draw_canvas()
###############################################################################
###############################################################################