Exemple #1
0
def graph_table(filename, title, vector, base_year, intensity_divisor,
                sector_groups):

    plot = GNUPlot(filename, title, "usa")

    data = {}
    sortby = {}

    numerators = {}
    denominators = {}

    for key in sector_groups.keys():
        data[key] = {}
        denominators[key] = dict((year, 0) for year in config.STUDY_YEARS)
        numerators[key] = dict((year, 0) for year in config.STUDY_YEARS)

    max_year = max(config.STUDY_YEARS)
    for sector in vector.keys():
        print(sector)
        numerator = vector[sector]

        for (key, sectors) in sector_groups.items():
            if sector in sectors:
                for year in config.STUDY_YEARS:
                    if intensity_divisor is not None:
                        denominators[key][year] += \
                            intensity_divisor[sector][year]
                    else:
                        denominators[key][year] += 1

                    numerators[key][year] += numerator[year]
                break

    for key in sector_groups.keys():
        print(key, denominators[key])
        data[key] = dict(
            (year, numerators[key][year] / denominators[key][year])
            for year in config.STUDY_YEARS)
        sortby[data[key][max_year] / data[key][base_year]] = key

    sorted_groups = [
        sortby[key] for key in sorted(sortby.keys(), reverse=True)
    ]

    plot.add_custom_setup("set style data linespoints")
    plot.add_custom_setup("unset colorbox")
    plot.add_custom_setup("set grid")

    min_x = 5 * math.floor(min(config.STUDY_YEARS) / 5)
    max_x = 5 * math.ceil(max(config.STUDY_YEARS) / 5) + 5  # room for labels
    plot.add_custom_setup("set xrange [ %d : %d ]" % (min_x, max_x))

    interval = 1 / (len(sorted_groups) - 1)
    for i in range(len(sorted_groups)):
        plot.add_custom_setup("set style lines %d lc palette frac %.2f lw 2" %
                              (i + 1, i * interval))
    series_values = []

    def name_for_group(group):
        if group in bea.short_nipa:
            group_name = bea.short_nipa[group]
        else:
            group_name = group
        return group_name

    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        group_name = name_for_group(group)

        series_values.append(group_name)
        plot.suppress_title(group_name)
        base_value = data[group][base_year]
        for year in config.STUDY_YEARS:
            plot.set_value(group_name, year,
                           data[group][year] / base_value * 100)

        plot.set_series_style(group_name, "linestyle %d" % (i + 1))

    plot.series_values = series_values
    plot.write_tables()
    plot.get_axis_specs()  # make sure max_y is adjusted

    prev_pos = None
    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        group_name = name_for_group(group)

        ending_value = data[group][max_year] / data[group][base_year] * 100
        position = ending_value / plot.max_y + 0.01  # line up baseline

        # space labels out by at least 0.04 so they don't overlap
        if prev_pos is not None and prev_pos - position < 0.03:
            position = prev_pos - 0.03

        plot.add_custom_setup(
            "set label %d '%s' at graph 0.81, %.2f font 'Arial,8'" %
            (i + 1, group_name, position))

        prev_pos = position

    plot.generate_plot()
Exemple #2
0
def graph_table(filename, title, vector,
                base_year, is_intensities, collapse=[]):

    plot = GNUPlot(filename, title, "usa")

    data = {}
    sortby = {}
    other_category = "Other"
    max_year = max(config.STUDY_YEARS)

    other_category_numer = dict((year, 0) for year in config.STUDY_YEARS)
    other_category_denom = dict((year, 0) for year in config.STUDY_YEARS)

    for group in vector.keys():
        numerator = vector[group]

        if is_intensities:
            denominator = pce_dollars[group]
        else:
            denominator = dict((year, 1) for year in config.STUDY_YEARS)

        base_value = numerator[base_year] / denominator[base_year]

        if group in collapse:
            for year in config.STUDY_YEARS:
                other_category_numer[year] += numerator[year]

                # adding 1 for each year is fine since it's relative to the base
                other_category_denom[year] += denominator[year]

        else:
            data[group] = {}

            for year in config.STUDY_YEARS:
                data[group][year] = numerator[year] / denominator[year]

            sortby[ data[group][max_year] / data[group][base_year] ] = group

    if len(other_category_numer):
        data[other_category] = dict(
            (year, other_category_numer[year] / other_category_denom[year]) \
                for year in config.STUDY_YEARS)
        sortby[ data[other_category][max_year] /\
                    data[other_category][base_year] ] = other_category

    sorted_groups = [sortby[key] for key in sorted(sortby.keys(), reverse=True)]

    plot.add_custom_setup("set style data linespoints")
    plot.add_custom_setup("unset colorbox")
    plot.add_custom_setup("set grid")

    min_x = 5 * math.floor(min(config.STUDY_YEARS) / 5)
    max_x = 5 * math.ceil(max(config.STUDY_YEARS) / 5) + 5 # room for labels
    plot.add_custom_setup("set xrange [ %d : %d ]" % (min_x, max_x))

    interval = 1 / (len(sorted_groups) - 1)
    for i in range(len(sorted_groups)):
        plot.add_custom_setup("set style lines %d lc palette frac %.2f lw 2"
                              % (i + 1,  i * interval))
    series_values = []

    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        if group in bea.short_nipa:
            group_name = bea.short_nipa[group]
        else:
            group_name = group

        series_values.append(group_name)
        plot.suppress_title(group_name)

        base_value = data[group][base_year]
        for year in config.STUDY_YEARS:
            print(group, year, data[group][year], base_value, data[group][year] / base_value)
            plot.set_value(group_name, year,
                           data[group][year] / base_value * 100)

        plot.set_series_style(group_name, "linestyle %d" % (i + 1))

    plot.series_values = series_values
    plot.write_tables()
    plot.get_axis_specs() # make sure max_y is adjusted

    prev_pos = None
    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        if group in bea.short_nipa:
            group_name = bea.short_nipa[group]
        else:
            group_name = group

        ending_value = data[group][max_year] / data[group][base_year] * 100
        position = ending_value / plot.max_y + 0.01 # line up baseline

        # space labels out by at least 0.04 so they don't overlap
        if prev_pos is not None and prev_pos - position < 0.03:
            position = prev_pos - 0.03

        plot.add_custom_setup(
            "set label %d '%s' at graph 0.81, %.2f font 'Arial,8'"
            % (i + 1, group_name, position))

        prev_pos = position

    plot.generate_plot()
Exemple #3
0
def graph_table(filename, title, vector,
                base_year, intensity_divisor, sector_groups):

    plot = GNUPlot(filename, title, "usa")

    data = {}
    sortby = {}

    numerators = {}
    denominators = {}

    for key in sector_groups.keys():
        data[key] = {}
        denominators[key] = dict((year, 0) for year in config.STUDY_YEARS)
        numerators[key] = dict((year, 0) for year in config.STUDY_YEARS)

    max_year = max(config.STUDY_YEARS)
    for sector in vector.keys():
        print(sector)
        numerator = vector[sector]

        for (key, sectors) in sector_groups.items():
            if sector in sectors:
                for year in config.STUDY_YEARS:
                    if intensity_divisor is not None:
                        denominators[key][year] += \
                            intensity_divisor[sector][year]
                    else:
                        denominators[key][year] += 1

                    numerators[key][year] += numerator[year]
                break

    for key in sector_groups.keys():
        print(key, denominators[key])
        data[key] = dict(
            (year, numerators[key][year] / denominators[key][year])
            for year in config.STUDY_YEARS)
        sortby[ data[key][max_year] / data[key][base_year] ] = key

    sorted_groups = [sortby[key] for key in sorted(sortby.keys(), reverse=True)]

    plot.add_custom_setup("set style data linespoints")
    plot.add_custom_setup("unset colorbox")
    plot.add_custom_setup("set grid")

    min_x = 5 * math.floor(min(config.STUDY_YEARS) / 5)
    max_x = 5 * math.ceil(max(config.STUDY_YEARS) / 5) + 5 # room for labels
    plot.add_custom_setup("set xrange [ %d : %d ]" % (min_x, max_x))

    interval = 1 / (len(sorted_groups) - 1)
    for i in range(len(sorted_groups)):
        plot.add_custom_setup("set style lines %d lc palette frac %.2f lw 2"
                              % (i + 1,  i * interval))
    series_values = []

    def name_for_group(group):
        if group in bea.short_nipa:
            group_name = bea.short_nipa[group]
        else:
            group_name = group
        return group_name

    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        group_name = name_for_group(group)

        series_values.append(group_name)
        plot.suppress_title(group_name)
        base_value = data[group][base_year]
        for year in config.STUDY_YEARS:
            plot.set_value(group_name, year,
                           data[group][year] / base_value * 100)

        plot.set_series_style(group_name, "linestyle %d" % (i + 1))

    plot.series_values = series_values
    plot.write_tables()
    plot.get_axis_specs() # make sure max_y is adjusted

    prev_pos = None
    for i in range(len(sorted_groups)):
        group = sorted_groups[i]
        group_name = name_for_group(group)

        ending_value = data[group][max_year] / data[group][base_year] * 100
        position = ending_value / plot.max_y + 0.01 # line up baseline

        # space labels out by at least 0.04 so they don't overlap
        if prev_pos is not None and prev_pos - position < 0.03:
            position = prev_pos - 0.03

        plot.add_custom_setup(
            "set label %d '%s' at graph 0.81, %.2f font 'Arial,8'"
            % (i + 1, group_name, position))

        prev_pos = position

    plot.generate_plot()