Exemplo n.º 1
0
    def squarify_size_nodes(path, nodes, x, y, width, height, min_size):
        # for n in nodes:
        #     print "%s -> %s" % (n.name, n.size)

        nodes = [n for n in nodes if n.size > min_size]
        nodes = sorted(nodes, key=lambda n: n.size, reverse=True)

        sizes = [n.size for n in nodes]
        sizes = squarify.normalize_sizes(sizes, width, height)
        rects = squarify.squarify(sizes, x, y, width, height)

        for i in range(len(rects)):
            rect = rects[i]
            node = nodes[i]
            child_path = path + "/" + node.name

            # print "[DEBUG] child_path", i, child_path, node.size
            # assert rect['x'] >= x, "%s (%s)" % (rect, (x, y, width, height))
            # assert rect['y'] >= y, "%s (%s)" % (rect, (x, y, width, height))
            # assert rect['dx'] <= width + 1, "%s (%s)" % (rect, (x, y, width, height))
            # assert rect['dy'] <= height + 1, "%s (%s)" % (rect, (x, y, width, height))

            rect['name'] = node.name
            rect['path'] = child_path
            rect['size'] = int(node.size)

            child_rects = SquareTreeMap.squarify_size_nodes(
                child_path, node.name2childs.values(), rect['x'], rect['y'], rect['dx'], rect['dy'], min_size)
            rects.extend(child_rects)

        return rects
Exemplo n.º 2
0
    def _plot_otu(self, axis, sizes, colors, max_count, name):
        width_and_height = math.sqrt(float(sum(sizes))/max_count*100)
        offset = (10 - width_and_height)/2

        normed = squarify.normalize_sizes(sizes, width_and_height, width_and_height)
        rects = squarify.squarify(normed, offset, offset , width_and_height, width_and_height)

        x = [rect['x'] for rect in rects]
        y = [rect['y'] for rect in rects]
        dx = [rect['dx'] for rect in rects]
        dy = [rect['dy'] for rect in rects]

        axis.bar(x, dy, width=dx, bottom=y, color=colors, align='edge', edgecolor='black', linewidth=0.2)

        axis.set_aspect('equal')
        axis.set_ylim(-0.3,10.3) # Add 0.1 so the edges are not truncated.
        axis.set_xlim(-0.3,10.3)
        axis.set_xticks([])
        axis.set_yticks([])
        axis.set_axis_off()

        if name is not None:
            fp = matplotlib.font_manager.FontProperties(size=9)
            axis.text(-1, 5,
                      name,
                      font_properties=fp,
                      rotation='vertical',
                      horizontalalignment='center',
                      verticalalignment='center')
Exemplo n.º 3
0
def tree_layout(id, path=''):
    response.content_type = 'application/json'
    
    # get node metadata
    tree = get_tree(cache[id]['tree'], path)
    children = tree['children'].keys()
    sizes = [tree['children'][child]['size'] for child in children]
    
    # filter out zero values
    children = filter(lambda child: tree['children'][child]['size'] > 0, children)
    sizes = filter(lambda size: size > 0, sizes)
    
    # sort the objects by size, descending
    order = argsort(sizes)[::-1]
    children = [children[i] for i in order]
    sizes = [sizes[i] for i in order]
    
    # compute the treemap layout
    sizes = squarify.normalize_sizes(sizes, width, height)
    rects = squarify.padded_squarify(sizes, 0, 0, width, height)
    
    # annotate rects with some metadata
    for (child, rect) in zip(children, rects):
        rect['host'] = cache[id]['meta']['host']
        rect['path'] = os.path.join(path, child)
        rect['name'] = child
        rect['size'] = bytes2human(tree['children'][child]['size'])
        if len(tree['children'][child]['children']) == 0:
            rect['type'] = 'file'
        else:
            rect['type'] = 'dir'
    
    # if I am at a leaf node, then rects should be empty
    # in that case, add a single rectangle for the whole canvas
    if len(rects) == 0:
        rects.append({'x': 0, 'y': 0,
                      'dx': width, 'dy': height,
                      'host': cache[id]['meta']['host'], 'path': path, 'name': path.split('/')[-1],
                      'size': bytes2human(tree['size']),
                      'type': 'file'})
    
    data = {}
    data['rects'] = rects
    data['size'] = tree['size']
    data['humansize'] = bytes2human(tree['size'])
    data['date'] = cache[id]['tree']['date']
    
    return json.dumps(data)
Exemplo n.º 4
0
    def _plot_scale(self, axis, max_total_count):
        axis.set_aspect('equal')
        axis.set_ylim(-0.3,10.3) # Add 0.1 so the edges are not truncated.
        axis.set_xlim(-0.3,10.3)
        axis.set_xticks([])
        axis.set_yticks([])
        axis.set_axis_off()
        scale_values = [1]
        while sum(scale_values)+scale_values[0]*10 < max_total_count:
            scale_values = [scale_values[0]*10] + scale_values
        to_normalise = scale_values + [max_total_count-sum(scale_values)]
        normalised = squarify.normalize_sizes(to_normalise, 10, 10)
        ylim = [0.,10.]
        sides = list([math.sqrt(normalised[i]) for i, value in enumerate(scale_values)])
        overlap = (ylim[1]-ylim[0]-sum(sides))/(len(sides)-1)
        next_bottom = ylim[0]
        xoffset = ylim[1]-ylim[0]-sides[0]
        fp = matplotlib.font_manager.FontProperties(size=6)
        for i, value in enumerate(scale_values):
            side = sides[i]
            axis.bar(10-side/2-0.1-xoffset,
                     bottom=next_bottom,
                     height=side,width=side,
                     color='0.7',edgecolor='black',linewidth=0.2)

            # Add text
            if value == 1:
                text = "1 read"
            else:
                text = "%i reads" % value
            axis.text(10.2-xoffset, next_bottom+side/2, text,
                      verticalalignment='center', font_properties=fp)
            # Setup for next loop iter
            next_bottom += overlap + side
        fp = matplotlib.font_manager.FontProperties(size=9)
        axis.text(0.2, 10.6, 'OTU count', font_properties=fp)
Exemplo n.º 5
0
def gen_chart(title):
    x = 0.
    y = 0.
    width = 100.
    height = 100.

    values = parties.values()

    normed = squarify.normalize_sizes(values, width, height)
    rects = squarify.squarify(normed, x, y, width, height)

    shapes = []
    annotations = []
    counter = 0

    for r in rects:
        shapes.append(
            dict(
            type ='rect',
                x0=r['x'],
                y0=r['y'],
                x1=r['x']+r['dx'],
                y1=r['y']+r['dy'],
                line=dict(width=2),
                fillcolor=_coalision_col(parties.keys()[counter]),
                opacity=_committed(parties.keys()[counter])
            )
        )
        annotations.append(
        dict(
            x=r['x']+(r['dx']/2),
            y=r['y']+(r['dy']/2),
            text=parties.keys()[counter],
            showarrow=False
        )
        )
        counter = counter + 1


    # For hover text
    trace0 = go.Scatter(
        x=[r['x']+(r['dx']/2) for r in rects],
        y=[r['y']+(r['dy']/2) for r in rects],
        text=[str(v) for v in values],
        mode='text')

    layout = dict(
        title="{}".format(title),
        font=dict(size=18),
        height=700,
        width=700,
        xaxis=dict(showgrid=False, zeroline=False,
                   ticks='', showticklabels=False),
        yaxis=dict(showgrid=False, zeroline=False,
                   ticks='', showticklabels=False),
        shapes=shapes,
        annotations=annotations,
        hovermode='closest')

    # Without hovertext
    return dict(data=[go.Scatter()], layout=layout)
Exemplo n.º 6
0
def treemap(value):
    x = 0.
    y = 0.
    width = 100.
    height = 100.
    test_data = {
        'BTC': {
            'size': '500',
            'marketcap': -0.34,
        },
        'BCH': {
            'size': '433',
            'marketcap': -0.34,
        },
        'ETH': {
            'size': '78',
            'marketcap': -0.34,
        },
        'XRP': {
            'size': '25',
            'marketcap': 0.34,
        },
        'XLM': {
            'size': '25',
            'marketcap': 0.34,
        },
        'ADA': {
            'size': '25',
            'marketcap': -0.34,
        },
    }
    coin_name_list = test_data.keys()
    coin_name_list2 = []
    for i in coin_name_list:
        coin_name_list2.append(i)

    values = []
    for key in coin_name_list:
        values.append(test_data[key]['size'])
    values = list(map(int, values))  # string to list

    normed = squarify.normalize_sizes(values, width, height)
    rects = squarify.squarify(normed, x, y, width, height)

    # Choose colors from http://colorbrewer2.org/ under "Export"
    color_brewer = ['rgb(255,0,255)', 'rgb(0,255,0)']
    shapes = []
    annotations = []
    counter = 0

    for r in rects:
        coin_name = coin_name_list2[counter]
        if test_data[coin_name]['marketcap'] > 0:
            color = 0
        else:
            color = 1
        shapes.append(
            dict(type='rect',
                 x0=r['x'],
                 y0=r['y'],
                 x1=r['x'] + r['dx'],
                 y1=r['y'] + r['dy'],
                 line=dict(width=2),
                 fillcolor=color_brewer[color]))
        annotations.append(
            dict(
                x=r['x'] + (r['dx'] / 2),
                y=r['y'] + (r['dy'] / 2),
                text="<span><a id='hover_info'>" + coin_name_list2[counter] +
                "</a><br> <b>" +
                str(test_data[coin_name_list2[counter]]['marketcap']) +
                "</b></span>",
                #text=coin_name_list2[counter],
                showarrow=False))
        counter = counter + 1

    figure = {
        'data': [
            go.Scatter(
                x=[r['x'] + (r['dx'] / 2) for r in rects],
                y=[r['y'] + (r['dy'] / 2) for r in rects],
                text=[str(v) for v in values],
                mode='text',
            )
        ],
        'layout':
        go.Layout(
            height=700,
            width=700,
            xaxis={
                'showgrid': False,
                'zeroline': False,
                'showticklabels': False
            },
            yaxis={
                'showgrid': False,
                'zeroline': False,
                'showticklabels': False
            },
            shapes=shapes,
            annotations=annotations,
            hovermode=False,
        )
    }

    return figure
import plotly.graph_objects as go

import squarify

fig = go.Figure()

x = 0.
y = 0.
width = 100.
height = 100.

values = [500, 433, 78, 25, 25, 7]

normed = squarify.normalize_sizes(values, width, height)
rects = squarify.squarify(normed, x, y, width, height)

# Choose colors from http://colorbrewer2.org/ under "Export"
color_brewer = [
    'rgb(166,206,227)', 'rgb(31,120,180)', 'rgb(178,223,138)',
    'rgb(51,160,44)', 'rgb(251,154,153)', 'rgb(227,26,28)'
]
shapes = []
annotations = []
counter = 0

for r, val, color in zip(rects, values, color_brewer):
    shapes.append(-dict(type='rect',
                        x0=r['x'],
                        y0=r['y'],
                        x1=r['x'] + r['dx'],
                        y1=r['y'] + r['dy'],
def update_graph(n_clicks, county, year, age, gender, figure):

    selected_data = df[(df['Location'] == county) & (df['Year'] == year) &
                       (df['Age'] == age) & (df['Sex'] == gender)]
    selected_data.set_index(
        'Disease', inplace=True
    )  # set the disease type to be the index to call, not sure if it's a good idea

    Year = selected_data['Year'].unique()
    Gender = selected_data['Sex'].unique()
    Age = selected_data['Age'].unique()

    #Dict_data = {}
    #for disease in selected_data.index:
    #    Dict_data[disease] = selected_data.loc[county]

    ## Put things into the graph.

    #    NOTE: There will/should be a section that categorize disease type into say three groups.
    #    A specific color should be assigned to each categoryself.

    x = 0.
    y = 0.
    width = 100.
    height = 100.

    #values = [500, 433, 78, 25, 25, 7]
    values = selected_data['Value']

    normed = squarify.normalize_sizes(values, width, height)
    rects = squarify.squarify(normed, x, y, width, height)

    # Choose colors from http://colorbrewer2.org/ under "Export"
    color_brewer = ['rgb(166,206,227)', 'rgb(31,120,180)', 'rgb(227,26,28)']
    shapes = []
    annotations = []
    counter = 0

    for r in rects:
        shapes.append(
            dict(type='rect',
                 x0=r['x'],
                 y0=r['y'],
                 x1=r['x'] + r['dx'],
                 y1=r['y'] + r['dy'],
                 line=dict(width=2),
                 fillcolor=color_brewer[counter]))
        annotations.append(
            dict(x=r['x'] + (r['dx'] / 2),
                 y=r['y'] + (r['dy'] / 2),
                 text=values[counter],
                 showarrow=False))
        counter = counter + 1
        if counter >= len(color_brewer):
            counter = 0

    figure = {
        'data': [
            go.Scatter(
                x=[r['x'] + (r['dx'] / 2) for r in rects],
                y=[r['y'] + (r['dy'] / 2) for r in rects],
                text=[str(v) for v in values],
                mode='text',
            )
        ],
        'layout':
        go.Layout(
            height=700,
            width=700,
            xaxis={
                'showgrid': False,
                'zeroline': False,
                'showticklabels': False
            },
            yaxis={
                'showgrid': False,
                'zeroline': False,
                'showticklabels': False
            },
            shapes=shapes,
            annotations=annotations,
            hovermode='closest',
        )
    }
    return figure
y = 0.
width = 500.
height = 500.
#values of products or sum of products
#values = [500, 433, 78, 25, 25, 7]
# Determine and collect data for most frequent product. WARNING!: opp has 500 rows only!
product_count=opp.product_id.value_counts()

aisle_name=[]
for e in product_count:
    print findProductName(product_count.index[e])
    aisle_name.append(findProductName(product_count.index[e]))
allNames=pd.Series(aisle_name)

#normed = squarify.normalize_sizes(values, width, height)
normed1 = squarify.normalize_sizes(allNames, width, height)
#rects = squarify.squarify(normed, x, y, width, height)
rects1 = squarify.squarify(normed1,x,y,width,height)

# Choose colors from http://colorbrewer2.org/ under "Export"
#color_brewer = ['rgb(166,206,227)','rgb(31,120,180)','rgb(178,223,138)',
#                'rgb(51,160,44)','rgb(251,154,153)','rgb(227,26,28)']

color_brewer1 = ['rgb(239,138,98)','rgb(247,247,247)','rgb(103,169,207)']
bupu500 = cl.interp( color_brewer1, 10 )

def findProductName(val):
    aNum=products.aisle_id[products.product_id==val]
    aNam=aisle.aisle[aisle.aisle_id==int(aNum)].values[0]
    return aNam.strip()
Exemplo n.º 10
0
def treemap(value):
    data = pd.read_csv("../../data/data_levantamientos_diciembre_2018.csv")

    data_set = data[['Cadena', 'Tienda', 'Ofertas y precios']]
    data_set = data_set.sort_values(by=['Ofertas y precios'], ascending=False)
    data_set = data_set.reset_index(drop=True)
    dd = data_set[['Tienda', 'Cadena']]
    x = 0.
    y = 0.
    width = 100.
    height = 100.

    #tiendas= ['SECAGV', 'SCADE', 'SCADE2', 'SEC', 'ZTM', 'ZTC', 'ZES', 'ZE3', 'SN3', 'ZTL', 'STL', 'SAT', 'ZN2', 'ZTN', 'ZNR', 'ZAT', 'SN2', 'ZAN', 'CNA', 'ZEC', 'ZPR', 'ZE2', 'ZLG', 'ZRH']

    values = pd.Series(data_set['Ofertas y precios'])
    tiendas = pd.Series(data_set['Tienda'])
    cadenas = pd.Series(data_set['Cadena'])
    #print(data_set)

    normed = squarify.normalize_sizes(values, width, height)
    rects = squarify.squarify(normed, x, y, width, height)

    # Choose colors from http://colorbrewer2.org/ under "Export"
    #http://colorbrewer2.org/?type=sequential&scheme=Reds&n=3
    color_brewer = ['red', 'blue']
    colores = {'Zorro Abarrotero': 0, 'Scorpion': 1}
    shapes = []
    annotations = []
    counter = 0
    i = 0
    for r in rects:
        #print(i)
        #print(tiendas[i])
        #print(values[i])
        #print(cadenas[i])
        #print(color_brewer[colores[cadenas[i]]])
        shapes.append(
            dict(type='rect',
                 x0=r['x'],
                 y0=r['y'],
                 x1=r['x'] + r['dx'],
                 y1=r['y'] + r['dy'],
                 line=dict(width=2),
                 fillcolor=color_brewer[colores[cadenas[i]]]))
        annotations.append(
            dict(
                x=r['x'] + (r['dx'] / 2),
                y=r['y'] + (r['dy'] / 2),
                #text = tiendas[i] + ': ' + '\n' + str(values[i]),
                text=str(values[i]),
                showarrow=False))
        counter = counter + 1
        i = i + 1
        if counter >= len(color_brewer):
            counter = 0

    figure = {
        'data': [
            go.Scatter(x=[r['x'] + (r['dx'] / 2) for r in rects],
                       y=[r['y'] + (r['dy'] / 2) for r in rects],
                       text=['Tienda: ' + str(c) for c in tiendas],
                       mode='none',
                       hoverinfo='text')
        ],
        'layout':
        go.Layout(title='Treemap de Ofertas y Precios por Tienda',
                  height=900,
                  width=1200,
                  xaxis={
                      'showgrid': False,
                      'zeroline': False,
                      'showticklabels': False
                  },
                  yaxis={
                      'showgrid': False,
                      'zeroline': False,
                      'showticklabels': False
                  },
                  shapes=shapes,
                  annotations=annotations,
                  hovermode='closest',
                  plot_bgcolor=colors['background'],
                  paper_bgcolor=colors['background'],
                  font={'color': colors['text']})
    }
    return figure
Exemplo n.º 11
0
def draw_tree9(vs, n, exp, title_text):
    fig = go.Figure()

    x = 0.
    y = 0.
    width = 100.
    height = 100.

    values = vs
    names = n

    normed = squarify.normalize_sizes(values, width, height)
    rects = squarify.squarify(normed, x, y, width, height)

    # Choose colors from http://colorbrewer2.org/ under "Export"
    color_brewer = cartocolors.Prism_9.hex_colors

    shapes = []
    annotations = []

    m_names = [nn if vv > 0.5 else '' for nn, vv in zip(names, values)]

    for r, val, color, n in zip(rects, values, color_brewer, m_names):
        shapes.append(
            dict(type='rect',
                 x0=r['x'],
                 y0=r['y'],
                 x1=r['x'] + r['dx'],
                 y1=r['y'] + r['dy'],
                 line=dict(width=2),
                 fillcolor=color))
        annotations.append(
            dict(x=r['x'] + (r['dx'] / 2),
                 y=r['y'] + (r['dy'] / 2),
                 text=n,
                 showarrow=False,
                 font=dict(size=18, color='#ebfffb', family='Noto Sans TC')))

    # For hover text
    fig.add_trace(
        go.Scatter(
            x=[r['x'] + (r['dx'] / 2) for r in rects],
            y=[r['y'] + (r['dy'] / 2) for r in rects],
            text=exp,
            mode='text',
            hoverinfo='text',
            textfont=dict(size=16, color='#ebfffb', family='Noto Sans TC'),
        ))

    fig.update_layout(
        title=title_text,
        #width=500,
        #height=500,
        xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
        yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
        shapes=shapes,
        annotations=annotations,
        hovermode='closest',
        dragmode=False,
        # margin=dict(l=10,r=10,b=10,t=10),
        margin=dict(l=0, r=0, b=0, t=50),
        # plot_bgcolor='#ebfffb',
        plot_bgcolor='rgba(0,0,0,0)',
        paper_bgcolor='rgba(0,0,0,0)',
        font=dict(family='Noto Sans TC')
        #     paper_bgcolor="#00ff00",
    )

    #    fig.update_xaxes()
    #    fig.update_yaxes(showticklabels=False)

    return fig
Exemplo n.º 12
0
def main():
    sections = parseSections(sys.stdin)
    if sections is None:
        print(
            'start of memory config not found, did you invoke the compiler/linker with LANG=C?'
        )
        return

    sectionWhitelist = {'.text', '.data', '.bss', '.rodata'}
    plots = []
    whitelistedSections = list(
        filter(lambda x: x.section in sectionWhitelist, sections))
    allObjects = list(chain(*map(lambda x: x.children, whitelistedSections)))
    allFiles = list(set(map(lambda x: x.basepath, allObjects)))
    for s in whitelistedSections:
        objects = s.children
        groupsize = {}
        for k, g in groupby(sorted(objects, key=lambda x: x.basepath),
                            lambda x: x.basepath):
            groupsize[k] = sum(map(lambda x: x.size, g))
        objects.sort(reverse=True, key=lambda x: x.size)
        values = list(map(lambda x: x.size, objects))
        totalsize = sum(values)

        x = 0
        y = 0
        width = 1000
        height = 1000
        values = squarify.normalize_sizes(values, width, height)
        rects = squarify.squarify(values, x, y, width, height)
        padded_rects = squarify.padded_squarify(values, x, y, width, height)

        # plot with bokeh
        output_file('linkermap.html', title='Linker map')

        left = list(map(lambda x: x['x'], padded_rects))
        top = list(map(lambda x: x['y'], padded_rects))
        rectx = list(map(lambda x: x['x'] + x['dx'] / 2, padded_rects))
        recty = list(map(lambda x: x['y'] + x['dy'] / 2, padded_rects))
        rectw = list(map(lambda x: x['dx'], padded_rects))
        recth = list(map(lambda x: x['dy'], padded_rects))
        files = list(map(lambda x: x.basepath, objects))
        size = list(map(lambda x: x.size, objects))
        children = list(
            map(
                lambda x: ','.join(map(lambda x: x[1], x.children))
                if x.children else x.section, objects))
        legend = list(
            map(lambda x: '{} ({})'.format(x.basepath, groupsize[x.basepath]),
                objects))
        source = ColumnDataSource(data=dict(
            left=left,
            top=top,
            x=rectx,
            y=recty,
            width=rectw,
            height=recth,
            file=files,
            size=size,
            children=children,
            legend=legend,
        ))

        hover = HoverTool(tooltips=[
            ("size", "@size"),
            ("file", "@file"),
            ("symbol", "@children"),
        ])

        p = figure(title='Linker map for section {} ({} bytes)'.format(
            s.section, totalsize),
                   plot_width=width,
                   plot_height=height,
                   tools=[hover, 'pan', 'wheel_zoom', 'box_zoom', 'reset'],
                   x_range=(0, width),
                   y_range=(0, height))

        p.xaxis.visible = False
        p.xgrid.visible = False
        p.yaxis.visible = False
        p.ygrid.visible = False

        palette = Category10[10]
        mapper = CategoricalColorMapper(palette=palette, factors=allFiles)
        p.rect(x='x',
               y='y',
               width='width',
               height='height',
               source=source,
               color={
                   'field': 'file',
                   'transform': mapper
               },
               legend='legend')

        # set up legend, must be done after plotting
        p.legend.location = "top_left"
        p.legend.orientation = "horizontal"

        plots.append(p)
    show(column(*plots, sizing_mode="scale_width"))
Exemplo n.º 13
0
def updateTreemap(clickData, year):
    if clickData is not None:
        clickedGraph = clickData['points'][0]['curveNumber']
    else:
        clickedGraph = 0

    if clickedGraph == 0:
        sub_df = df_inc[(df_inc['Periodo']) == year]
        sub_df_gb = sub_df.groupby(['Periodo', 'NIVEL3'])['Real_amount'].sum()
        sub_df = sub_df_gb.reset_index()
    elif clickedGraph == 1:
        sub_df = df_exp[(df_exp['Periodo']) == year]
        sub_df_gb = sub_df.groupby(['Periodo', 'NIVEL3'])['Real_amount'].sum()
        sub_df = sub_df_gb.reset_index()

    total = sub_df.groupby(['Periodo'], as_index=False)['Real_amount'].sum()
    total = total.iloc[0]['Real_amount']

    x, y = 0., 0.
    width, height = 200., 100.

    normed = sq.normalize_sizes(sub_df['Real_amount'], width, height)
    rects = sq.squarify(normed, x, y, width, height)

    color_brewer = ['rgb(166,206,227)', 'rgb(31,120,180)', 'rgb(178,223,138)',
                    'rgb(51,160,44)', 'rgb(251,154,153)', 'rgb(227,26,28)',
                    'rgb(137,45,89)']

    shapes = []
    annotations = []
    counter = 0
    df_counter = 0

    for r in rects:
        shapes.append(dict(
            type='rect',
            x0=r['x'],
            y0=r['y'],
            x1=r['x']+r['dx'],
            y1=r['y']+r['dy'],
            line=dict(width=2),
            fillcolor=color_brewer[counter]
        ))

        if r['dx'] < 20 or r['dy'] < 20:
            text = ''
        else:
            text = sub_df.iloc[df_counter]['NIVEL3'].replace(' ', '<br>')

        annotations.append(dict(
            x=r['x']+(r['dx']/2),
            y=r['y']+(r['dy']/2),
            text=text,
            showarrow=False,
            font=dict(size=10, color='#000000')
        ))

        counter += 1
        df_counter += 1
        if counter >= len(color_brewer):
            counter = 0

    trace = go.Scatter(
        x=[r['x']+(r['dx']/2) for r in rects],
        y=[r['y']+(r['dy']/2) for r in rects],
        text=[
            str(row['NIVEL3']) + '<br>' +
            str(round(row['Real_amount'] / total * 100, 2)) +
            '%' for (i, row) in sub_df.iterrows()
        ],
        customdata=[],
        hoverinfo='text',
        textfont=dict(size=2),
        mode='text',
    )

    layout = dict(
        height=700,
        width=1700,
        xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
        yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
        shapes=shapes,
        annotations=annotations,
        hovermode='closest'
    )

    return go.Figure(data=[trace], layout=layout)
Exemplo n.º 14
0
def ST(data, groups, path, dir, file, width, height, use):
    # these values define the coordinate system for the returned rectangles
    # the values will range from x to x + width and y to y + height
    x = 0.
    y = 0.
    total = 0
    groupSize = []
    length = len(groups)
    for i in range(length):
        total += len(groups[i])
    for i in range(length):
        dic = {}
        dic['size'] = (len(groups[i]) / total)
        dic['name'] = i
        groupSize.append(dic)
    groupSize.sort(key=itemgetter('size'), reverse=True)

    index = []
    values = []
    for i in groupSize:
        values.append(i['size'])
        index.append(i['name'])

    # the sum of the values must equal the total area to be laid out
    # i.e., sum(values) == width * height
    values = squarify.normalize_sizes(values, width, height)

    # returns a list of rectangles
    rects = squarify.squarify(values, x, y, width, height)
    # padded rectangles will probably visualize better for certain cases
    # padded_rects = squarify.padded_squarify(values, x, y, width, height)
    # print(padded_rects)
    for i in range(length):
        rects[i]['name'] = index[i]

    rects.sort(key=itemgetter('name'))
    # for i in range(length):
    #     del rects[i]['index']

    dic = {}
    dic['x'] = 0
    dic['y'] = 0
    dic['dx'] = width
    dic['dy'] = height
    rects.append(dic)

    links = data['links']
    nodes = data['nodes']

    forWrite = {}
    forWrite['nodes'] = nodes
    forWrite['links'] = links
    forWrite['groups'] = rects
    forWrite['groupSize'] = data['groupSize']
    forWrite['pgroup'] = data['pgroup']
    forWrite['pout'] = data['pout']
    forWrite['mostConnected'] = data['mostConnected']
    forWrite['nodeSize'] = data['nodeSize']
    forWrite['linkSize'] = data['linkSize']
    forWrite['nodeMax'] = data['nodeMax']
    forWrite['nodeMin'] = data['nodeMin']
    forWrite['linkMax'] = data['linkMax']
    forWrite['linkMin'] = data['linkMin']

    try:
        # verify = os.listdir('../data/' + use  + '/temp/' + dir)
        verify = os.listdir('../data/origin/' + use)
    except:
        # os.mkdir('../data/' + use  + '/temp/' + dir)
        os.mkdir('../data/origin/' + use)
    # f = open('../data/' + use  + '/temp/' + dir + '/' + file, 'w')
    f = open('../data/origin/' + use + '/' + file, 'w')
    json.dump(forWrite,
              f,
              ensure_ascii=False,
              indent=4,
              sort_keys=True,
              separators=(',', ': '))
Exemplo n.º 15
0
def ft_treemap_figure(percent_dict, percent_tuples):
    """
    This method creates the figure attribute for the treemap.
    Uses squarify to compute the necessary rectangles.

    Arguments
    ----------
    percent_dict: {topic nbr:sum(topic probability over all docs)}
    percent_tuples: same as percent_dict but under list of tuples

    Return
    ----------
    figure: plotly graph object which results in a treemap when ploted
    """
    x = 0.
    y = 0.
    width = 4.
    height = 4.

    values = list(percent_dict.values())
    values.sort(reverse=True)
    normed = squarify.normalize_sizes(values, width, height)
    rects = squarify.squarify(normed, x, y, width, height)

    # color_brewer = ['#800026', '#bd0026', '#e31a1c', '#fc4e2a',
    #                 '#fd8d3c', '#feb24c', '#fed976', '#ffeda0', '#ffffcc']

    shapes = []
    annotations = []
    # counter = 0
    topic_counter = 0

    for r in rects:
        shapes.append(
            dict(
                type='rect',
                x0=r['x'],
                y0=r['y'],
                x1=r['x'] + r['dx'],
                y1=r['y'] + r['dy'],
                line=dict(width=2),
                # fillcolor=color_brewer[counter]
            ))
        annotations.append(
            dict(x=r['x'] + (r['dx'] / 2),
                 y=r['y'] + (r['dy'] / 2),
                 text=percent_tuples[topic_counter][0],
                 showarrow=False))
        # counter += 1
        topic_counter += 1
        # if counter >= len(color_brewer):
        #     counter = 0

    figure = {
        'data':
        go.Scatter(
            x=[r['x'] + (r['dx'] / 2) for r in rects],
            y=[r['y'] + (r['dy'] / 2) for r in rects],
            text=[ptuple[0] for ptuple in percent_tuples],
            mode='text',
        ),
        'layout':
        go.Layout(
            height=700,
            width=700,
            xaxis={
                'fixedrange': True,
                'showgrid': False,
                'zeroline': False,
                'showticklabels': False
            },
            yaxis={
                'fixedrange': True,
                'showgrid': False,
                'zeroline': False,
                'showticklabels': False
            },
            shapes=shapes,
            annotations=annotations,
            hovermode='closest',
        )
    }

    return figure
Exemplo n.º 16
0
def treemap(df):
    bs = df
    liab = ["totalcurrentliabilities", "totalnoncurrentliabilities"]

    ass = ["netppe", "totalcurrentassets", "totalnoncurrentassets"]

    equ = [
        "totalcommonequity",
    ]

    tras = bs.T[bs.T.index.isin([
        "netppe", "totalnoncurrentassets", "totalcurrentassets",
        "totalcurrentliabilities", "totalcommonequity",
        "totalnoncurrentliabilities"
    ])]

    tras["cat"] = np.where(
        tras.index.isin(ass), "asset",
        (np.where(tras.index.isin(liab), "liability",
                  (np.where(tras.index.isin(equ), "equity", np.nan)))))

    tras = tras[tras["cat"].isin(["liability", "equity", "asset"])]

    tras = tras.sort_values("cat", ascending=True)

    lit = list(tras.index.values)

    dam = [str(v) for v in ['ppe', 'ca', 'nca', 'ce', 'cl', 'ncl']]

    x = 0.
    y = 0.
    width = 100.
    height = 100.

    # values = [500, 433, 78, 25, 25, 7]
    values = list(tras[6].values)

    normed = squarify.normalize_sizes(values, width, height)
    rects = squarify.squarify(normed, x, y, width, height)

    # Choose colors from http://colorbrewer2.org/ under "Export"
    color_brewer = [
        'rgb(166,206,227)', '#CAFF70', '#CDB7B5', '#EED5B7', 'pink', '#EBEBEB'
    ]
    shapes = []
    annotations = []
    counter = 0

    for r in rects:
        shapes.append(
            dict(type='rect',
                 x0=r['x'],
                 y0=r['y'],
                 x1=r['x'] + r['dx'],
                 y1=r['y'] + r['dy'],
                 line=dict(width=2),
                 fillcolor=color_brewer[counter]))
        annotations.append(
            dict(x=r['x'] + (r['dx'] / 2),
                 y=r['y'] + (r['dy'] / 2),
                 text=dam[counter],
                 textangle=15,
                 showarrow=False))
        counter = counter + 1
        if counter >= len(color_brewer):
            counter = 0

    # For hover text
    trace0 = go.Scatter(
        x=[r['x'] + (r['dx'] / 2) for r in rects],
        y=[r['y'] + (r['dy'] / 2) for r in rects],
        text=[str(v) for v in lit],
        mode='text',
    )

    layout = dict(height=350,
                  width=350,
                  xaxis=dict(showgrid=False, zeroline=False),
                  yaxis=dict(showgrid=False, zeroline=False),
                  shapes=shapes,
                  annotations=annotations,
                  hovermode='closest')

    # With hovertext
    figure = dict(data=[trace0], layout=layout)

    # Without hovertext
    # figure = dict(data=[Scatter()], layout=layout)

    return figure
Exemplo n.º 17
0
def create_tree_map(series, title, treeMapColors, negative=False):
    hashtagThreshold = 0.03
    blankThreshold = 0.01

    x = 0.
    y = 0.
    width = 0.6
    height = 1

    if negative:
        series = series[series < 0]
        values = series.values * -1
    else:
        series = series[series > 0]
        values = series.values

    normed = squarify.normalize_sizes(values, width, height)
    rects = squarify.squarify(normed, x, y, width, height)

    df = pd.DataFrame(series, columns=['exposure'])
    df['percentage'] = df.exposure.values / df.exposure.sum()
    df['label'] = ''
    df['label'][df.percentage > hashtagThreshold] = df.index.to_series()[
        df.percentage > hashtagThreshold]
    df['number'] = df.reset_index().index.values + 1
    try:
        df['label'][(df.percentage < hashtagThreshold)
                    & (df.percentage > blankThreshold)] = '#' + df.number[
                        (df.percentage < hashtagThreshold)
                        & (df.percentage > blankThreshold)].apply(str)
    except:
        pass

    labels = df['label'].values

    color_brewer = [
        '#8dd3c7', '#ffffb3', '#bebada', '#fb8072', '#80b1d3', '#fdb462',
        '#b3de69', '#fccde5', '#d9d9d9', '#bc80bd', '#ccebc5', '#ffed6f'
    ]

    shapes = []
    annotations = []
    counter = 0
    label_counter = 0

    for r in rects:
        trade = series.index[counter]
        shapes.append(
            dict(type='rect',
                 x0=r['x'],
                 y0=r['y'],
                 x1=r['x'] + r['dx'],
                 y1=r['y'] + r['dy'],
                 line=dict(width=2),
                 fillcolor=treeMapColors.color[trade]))
        annotations.append(
            dict(x=r['x'] + (r['dx'] / 2),
                 y=r['y'] + (r['dy'] / 2),
                 text=labels[label_counter],
                 showarrow=False))
        counter = counter + 1
        label_counter = label_counter + 1

    # For hover text
    trace0 = go.Scatter(
        x=[r['x'] + (r['dx'] / 2) for r in rects],
        y=[r['y'] + (r['dy'] / 2) for r in rects],
        text=[
            str(index + ': ' + '{0:,.2f}'.format(value))
            for (index, value) in series.iteritems()
        ],
        # text=[str(v) for v in values],
        mode='none',
        hoverinfo='text')

    layout = dict(title=title,
                  xaxis=dict(showgrid=False, zeroline=False),
                  yaxis=dict(showgrid=False, zeroline=False),
                  shapes=shapes,
                  annotations=annotations,
                  hovermode='closest',
                  margin=go.layout.Margin(l=0, r=0, b=0, t=25))

    # With hovertext
    figure = dict(data=[trace0], layout=layout)

    return figure
Exemplo n.º 18
0
def treemap():
    x = 0.
    y = 0.
    width = 100.
    height = 100.

    values = [500, 433, 78, 25, 25, 7]

    normed = squarify.normalize_sizes(values, width, height)
    rects = squarify.squarify(normed, x, y, width, height)

    # Choose colors from http://colorbrewer2.org/ under "Export"
    color_brewer = [
        'rgb(166,206,227)', 'rgb(31,120,180)', 'rgb(178,223,138)',
        'rgb(51,160,44)', 'rgb(251,154,153)', 'rgb(227,26,28)'
    ]
    shapes = []
    annotations = []
    counter = 0

    for r in rects:
        shapes.append(
            dict(type='rect',
                 x0=r['x'],
                 y0=r['y'],
                 x1=r['x'] + r['dx'],
                 y1=r['y'] + r['dy'],
                 line=dict(width=2),
                 fillcolor=color_brewer[counter]))
        annotations.append(
            dict(x=r['x'] + (r['dx'] / 2),
                 y=r['y'] + (r['dy'] / 2),
                 text=values[counter],
                 showarrow=False))
        counter = counter + 1
        if counter >= len(color_brewer):
            counter = 0

    # For hover text
    trace0 = go.Scatter(
        x=[r['x'] + (r['dx'] / 2) for r in rects],
        y=[r['y'] + (r['dy'] / 2) for r in rects],
        text=[str(v) for v in values],
        mode='text',
    )

    layout = dict(height=700,
                  width=700,
                  xaxis=dict(showgrid=False, zeroline=False),
                  yaxis=dict(showgrid=False, zeroline=False),
                  shapes=shapes,
                  annotations=annotations,
                  hovermode='closest')

    # With hovertext
    figure = dict(data=[trace0], layout=layout)

    # Without hovertext
    # figure = dict(data=[Scatter()], layout=layout)

    return div_output(figure)
Exemplo n.º 19
0
            
x = 0.
y = 0.
width = 100.
height = 100.

values = []
values_text = []

for tier_3_format, count in Counter(tier_3_formats).most_common():
    # otherwise this gets a little unwieldy...
    if tier_3_format != "" and count > 100:
        values.append(count)
        values_text.append(tier_3_format)
    
normed = squarify.normalize_sizes(values, width, height)
rects = squarify.squarify(normed, x, y, width, height)

# Choose colors from http://colorbrewer2.org/ under "Export"
color_brewer = ['rgb(166,206,227)','rgb(31,120,180)','rgb(178,223,138)',
                'rgb(51,160,44)','rgb(251,154,153)','rgb(227,26,28)',
                'rgb(253,191,111)','rgb(255,127,0)','rgb(202,178,214)',
                'rgb(106,61,154)','rgb(255,255,153)','rgb(177,89,40)']

shapes = []
annotations = []
color_counter = 0
name_counter = 0

for r in rects:
    shapes.append( 
Exemplo n.º 20
0
def treemap(sizes, diff, labels, x=0, y=0, width=1, height=1, title=None):
    # 准备画布
    fig, ax = plt.subplots(1, figsize=(15, 15 * height / width))
    ax.set_xlim(x, width)
    ax.set_ylim(y, height)

    # 创造和同比净增长关联的颜色方案
    min_diff = min(diff)
    max_diff = max(diff)
    if min_diff > 0:
        cmap = mpl.cm.Blues
        norm = mpl.colors.Normalize(vmin=min_diff, vmax=max_diff)
    elif max_diff < 0:
        cmap = mpl.cm.Reds
        norm = mpl.colors.Normalize(vmin=min_diff, vmax=max_diff)
    else:
        cmap = mpl.cm.bwr_r
        norm = mpl.colors.TwoSlopeNorm(
            vmin=min_diff, vcenter=0, vmax=max_diff
        )  # 强制0为中点的正太分布渐变色

    colors = [cmap(norm(value)) for value in diff]

    # 使用Squarify原生方法画图
    # ax = squarify.plot(sizes=sizes,
    #               label=labels[:label_limit],
    #               ax=ax,
    #               bar_kwargs=dict(linewidth=1, edgecolor="#222222"),
    #               text_kwargs={'fontname': 'SimHei',
    #                            'fontsize': 20},
    #               alpha=.8,
    #               color=colors
    #               )

    # 使用Squarify导出四边形数据,以数据手动画图,可以控制更多元素
    sizes.sort(reverse=True)  # sizes必须先由大到小排序
    sizes = squarify.normalize_sizes(sizes, width, height)  # 根据设置的总体宽高正态化数据
    rects_data = squarify.squarify(sizes, x, y, width, height)  # Squarify算法计算出所有四边形的数据

    # 根据数据循环创建矩形并添加标签
    for i, r in enumerate(rects_data):
        rect = patches.Rectangle(
            (r["x"], r["y"]),
            r["dx"],
            r["dy"],
            linewidth=2,
            edgecolor="#222222",
            facecolor=colors[i],
        )  # 创建四边形
        ax.add_patch(rect)  # Add patch到轴
        # 动态添加标签并设置标签字体大小
        if r["dx"] > 0.02 * (width * height) or r["dx"] * r["dy"] > 0.01 * (
            width * height
        ):
            plt.text(
                r["x"] + r["dx"] / 2,  # rect的水平中心
                r["y"] + r["dy"] / 2,  # rect的垂直中心
                labels[i],
                ha="center",
                va="center",
                multialignment="center",
                fontproperties=myfont,
                fontsize=80 * r["dx"] / (width * height),
            )
        # 前十名左上角添加Rank
        if i < 10:
            plt.text(
                r["x"] + r["dx"] * 0.1,  # rect的left稍往右偏移
                r["y"] + r["dy"] - r["dx"] * 0.1,  # rect的Top稍往下偏移
                i + 1,
                ha="center",
                va="center",
                multialignment="center",
                fontproperties=myfont,
                fontsize=80 * r["dx"] / (width * height),
            )

    # 去除边框的刻度
    ax.set_xticks([])
    ax.set_yticks([])

    # 添加标题
    if title is not None:
        fig.suptitle(title, fontproperties=myfont)

    # 保存到字符串
    sio = BytesIO()
    plt.savefig(sio, format="png", bbox_inches="tight", transparent=True, dpi=600)
    data = base64.encodebytes(sio.getvalue()).decode()  # 解码为base64编码的png图片数据
    src = "data:image/png;base64," + str(data)  # 增加Data URI scheme

    # 关闭绘图进程
    plt.clf()
    plt.cla()
    plt.close()

    return src