plotutils.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:clusim 作者: ajgates42 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号