Example #1
0
def main():
    args = get_args(__file__)

    labels = [
        'N/A', 'Id. Física', 'Id. Historiográfica', 'Desconocido', 'Perdido'
    ]
    colornames = ['light grey', 'medium green', 'denim blue', 'pale red']

    df = pd.DataFrame(read_table(args.table))
    df['ident'] = df.apply(categorize, axis=1)
    data = df\
        .drop_duplicates(['bid', 'lid'], keep='first')\
        .pivot(index='bid', columns='lid', values='ident')\
        .fillna(0)

    #colors = sns.color_palette('hls', len(data))
    #colors = sns.color_palette('husl', len(data))
    #colors = sns.light_palette('red', len(data))
    colors = sns.xkcd_palette(colornames)

    f, ax = plt.subplots()

    sns.heatmap(data,
                ax=ax,
                square=True,
                linewidth=0.5,
                cmap=ListedColormap(colors),
                cbar=False)

    set_axis(ax, data, as_letters(set(df.year.values)), 'Libros')
    legend(f, ax, labels, colors)
    plotting(plt, args)
Example #2
0
def plot(args):
    df = pd.DataFrame(read_table(args.table))
    configurer = configs[args.color_by]
    data, labels, colors = configurer(df, args.color_by)

    f, ax = plt.subplots()

    fig = sns.heatmap(
        data,
        ax=ax,
        #square=True,
        linewidth=0.5,
        cmap=ListedColormap(colors),
        cbar=False)

    set_axis(ax, data, as_letters(set(df.year.values)), ylabel='Posición')
    legend(f, ax, labels, colors)

    if args.annotated and args.color_by in ['bid', 'year']:
        df_by_bid = df.drop_duplicates('bid').set_index('bid')
        texts = [
            fig.text(
                15,
                bid,
                df.loc[bid, 'short'],
                fontsize=8,
            ) for bid, row in data.iterrows()
        ]

    plotting(plt, args)
Example #3
0
def main():
    args = get_args(__file__)

    df = pd.DataFrame(read_table(args.table))

    def track(row):
        # Mark it as id, which will go in red
        return 1000 if row['bid'] == 25 else row['bid']

    df['track'] = df.apply(track, axis=1)
    data = df.pivot(index='pos', columns='lid', values='track')
    colornames = ['light blue', 'bright red']
    colors = sns.xkcd_palette(colornames)

    f, ax = plt.subplots()

    sns.heatmap(data,
                ax=ax,
                square=True,
                linewidth=0.5,
                cmap=ListedColormap(colors),
                cbar=False)

    set_axis(ax, data, as_letters(set(df.year.values)), 'Posición')
    plotting(plt, args)
Example #4
0
def main():
    args = get_args(__file__)

    df = pd.DataFrame(read_table(args.table))
    data = df.pivot(index='pos', columns='lid', values='bid')
    # colors = sns.color_palette('hls', len(data))
    # colors = sns.color_palette('husl', len(data))
    # colors = sns.light_palette('red', len(data))
    colors = sns.light_palette('navy', len(data))

    f, ax = plt.subplots()

    sns.heatmap(data,
                ax=ax,
                square=True,
                linewidth=0.5,
                cmap=ListedColormap(colors),
                cbar=False)

    set_axis(ax, data, as_letters(set(df.year.values)), 'Posición')
    plotting(plt, args)
Example #5
0
def main():
    args = get_args(__file__)

    # TODO fix axis
    df = pd.DataFrame(read_table(args.table))\
        .pivot(index='pos', columns='lid')\
        .fillna(float('NaN'))

    title = 'Altura de libro por inventario y posición'

    plots = df.height.plot(kind='bar', subplots=True, title=title, grid=True)

    for plot in plots:
        plot.set_title('')
        plot.set_ylabel('')
        plot.legend(loc='upper right', bbox_to_anchor=(1.1, 1))

    visible = set(range(1, len(df), 5))
    for n, label in enumerate(plots[-1].xaxis.get_ticklabels()):
        label.set_visible(n + 1 in visible)

    plotting(plt, args)
Example #6
0
def main():
    args = get_args(__file__)

    names = ['NA', 'LAT', 'ROM', 'FRAN']
    labels = ['N/A', 'Latín', 'Romance', 'Francés']
    colornames = ['light grey', 'pale red', 'medium green', 'denim blue']

    df = pd.DataFrame(read_table(args.table))
    data = categorical_by(df, 'lang', names)
    colors = sns.xkcd_palette(colornames)

    f, ax = plt.subplots()

    sns.heatmap(data,
                ax=ax,
                square=True,
                linewidth=0.5,
                cmap=ListedColormap(colors),
                cbar=False)

    set_axis(ax, data, as_letters(set(df.year.values)), 'Libros')
    legend(f, ax, labels, colors)
    plotting(plt, args)
Example #7
0
def main():
    args = get_args(__file__)

    names = ['NA', 'REL', 'CRONIC', 'ANTI']
    labels = ['N/A', 'Religioso', 'Crónicas y Leyes', 'Historia Antigua']
    colornames = ['light grey', 'pale red', 'medium green', 'denim blue']

    df = pd.DataFrame(read_table(args.table))
    #df = pd.read_csv(args.table)
    data = categorical_by(df, 'topic', names)
    colors = sns.xkcd_palette(colornames)

    f, ax = plt.subplots()

    sns.heatmap(data,
                ax=ax,
                square=True,
                linewidth=0.5,
                cmap=ListedColormap(colors),
                cbar=False)

    set_axis(ax, data, as_letters(df.year.values), ylabel='Posición')
    legend(f, ax, labels, colors)
    plotting(plt, args)
Example #8
0
def main():
    args = get_args(__file__)

    df = pd.DataFrame(read_table(args.table))
    sizes = len(range(int(df['height'].max())))
    data = df.pivot(index='pos', columns='lid', values='height').fillna(0)
    #colors = sns.color_palette('cubehelix', sizes)
    #colors = sns.color_palette('hls', len(data))
    #colors = sns.color_palette('husl', len(data))
    #colors = sns.light_palette('red', sizes)
    #colors = sns.light_palette('navy', sizes)
    colors = sns.light_palette('green', sizes)

    f, ax = plt.subplots()

    sns.heatmap(data,
                ax=ax,
                square=True,
                linewidth=0.5,
                cmap=ListedColormap(colors),
                cbar=False)

    set_axis(ax, data, as_letters(set(df.year.values)), 'Tamaño')
    plotting(plt, args)
Example #9
0
def main():
    args = get_args(__file__)

    df = pd.DataFrame(read_table(args.table))
    data = df\
        .drop_duplicates(['bid', 'lid'], keep='first')\
        .pivot(index='bid', columns='lid', values='year')\
        .fillna(False)

    #colors = sns.color_palette('hls', len(data))
    #colors = sns.color_palette('husl', len(data))
    colors = sns.light_palette('red', len(data))

    f, ax = plt.subplots()

    sns.heatmap(data,
                ax=ax,
                square=True,
                linewidth=0.5,
                cmap=ListedColormap(colors),
                cbar=False)

    set_axis(ax, data, as_letters(set(df.year.values)), 'Libros')
    plotting(plt, args)
Example #10
0
def main():
    parser = get_parser()
    parser.add_argument('--first', default=3, type=int)
    parser.add_argument('--second', default=4, type=int)
    parser.add_argument('--annotated', action='store_true')
    parser.add_argument('--iterations', default=10, type=int)
    parser.add_argument('--color-by')
    args = get_args(__file__, parser)

    columns = [args.first, args.second]
    df = pd.DataFrame(read_table(args.table))
    data = df[(df.lid == args.first) | (df.lid == args.second)]\
        .pivot(index='bid', columns='lid', values='pos')\
        .sort_values(by=args.first)\
        .fillna(0)\
        .reindex_axis(columns, axis=1)  # assure column order

    # Reindex by position
    meta = Metadata(index='pos',
                    dfs=[df[df.lid == args.first], df[df.lid == args.second]])

    palette_name = None
    title = 'Orden/Orden inventarios {} y {}'\
        .format(to_letter(args.first), to_letter(args.second))
    if not args.color_by:
        # Color based on wether theyre in both inventaries or missing
        data['color'] = data.apply(
            lambda row: any(not row[c] for c in columns), 1)
    else:
        variable_name = variable_names.get(args.color_by, args.color_by)
        title += ' variable "{}"'.format(variable_name)
        data['color'] = data.apply(
            lambda row: meta.get_field(args.color_by, *
                                       [row[c] for c in columns]), 1)

    # Group numerical values in 5 bins/categories
    color_sorter = None
    if args.color_by in ['area', 'height']:
        palette_name = 'YlOrRd'  # yellow to red
        bins = 10 if args.color_by == 'height' else 5
        data['color'] = pd.cut(data['color'], bins, precision=0)

        def color_sorter(e):
            return float(str(e).strip('(').strip(']').split(', ', 1)[0])

    # Assure repeteable colors by setting category-color map
    # before lmplot does it randomly on each run and confuse us
    values = sorted(data['color'].unique(), key=color_sorter)
    colors = sns.color_palette(palette=palette_name, n_colors=len(values))
    palette = dict(zip(values, colors))

    # Use str as column names, otherwise lmplot goes wild
    columns = list(map(str, columns))
    data.columns = columns + ['color']

    p = sns.lmplot(*columns,
                   data=data,
                   hue='color',
                   palette=palette,
                   legend=False,
                   legend_out=True,
                   fit_reg=False,
                   size=7,
                   aspect=1.3)

    # Set top title and space for it
    plt.suptitle(title)
    p.fig.subplots_adjust(top=0.92)

    p.set(ylim=(0, None), xlim=(0, None))

    # Set legend outside graph at center right
    if args.color_by:
        p.fig.subplots_adjust(right=0.85)
        variable_name = variable_names.get(args.color_by, args.color_by)
        plt.legend(bbox_to_anchor=(1.18, 0.7),
                   borderaxespad=0.,
                   title=variable_name)

    if args.annotated:
        texts = [
            p.ax.text(
                first,
                second,
                meta.get_field('short', first, second),
                fontsize=8,
            ) for first, second, color in data.values
        ]
        # for first, second, na in data.values:
        #    # plt.annotate(
        #    #     meta.get(first, second)['short'],
        #    #     #str((first, second)),
        #    #     xy=(first, second),
        #    #     xytext=(first + 1, second + 1),
        #    #     fontsize=8,
        #    # )

        adjust_text(texts,
                    force_points=1.5,
                    lim=args.iterations,
                    arrowprops=dict(arrowstyle="-", color='r', alpha=0.8))

    plotting(plt, args)