Example #1
0
def test_different_markers():
    from ggplot.components import assign_visual_mapping
    from ggplot.components.shapes import shape_gen
    # First the generator which assigns the shapes
    shape = shape_gen()
    assert_true(
        six.next(shape) != six.next(shape),
        "Subsequent shapes are not different!")
    shape = shape_gen()
    assert_true(six.next(shape) == 'o', "First shape is not 'o'")
    assert_true(six.next(shape) == '^', "Second shape is not '^'")
    # Do shapes show up in the transformed layer?
    df = pd.DataFrame({
        "x": [1, 2],
        "y": [1, 2],
        "a": ["a", "b"],
        "b": ["c", "d"]
    })
    gg = ggplot(aes(x="x", y="y", shape="a", color="b"), data=df)
    new_df = assign_visual_mapping(df, aes(x="x", y="y", shape="a", color="b"),
                                   gg)
    layer = gg._get_layers(new_df)
    assert_true("shape" in layer[0], "no shape was assigned")
    assert_true(layer[0]["shape"] != layer[1]["shape"],
                "wrong marker was assigned")
    # And now a visual test that both shapes are there. Make them big so that the test is failing
    # if something is wrong
    gg = ggplot(aes(x="x", y="y", shape="a", color="b"), data=df)
    assert_same_ggplot(gg + geom_point(size=3000), "geom_point_marker")
Example #2
0
def test_legend_structure():
    df = pd.DataFrame({
        'xmin': [1, 3, 5],
        'xmax': [2, 3.5, 7],
        'ymin': [1, 4, 6],
        'ymax': [5, 5, 9],
        'fill': ['blue', 'red', 'green'],
        'quality': ['good', 'bad', 'ugly'],
        'alpha': [0.1, 0.5, 0.9],
        'texture': ['hard', 'soft', 'medium']})

    gg = ggplot(df, aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax',
                        colour='quality', fill='fill', alpha='alpha',
                        linetype='texture'))
    new_df, legend = assign_visual_mapping(df, gg.aesthetics, gg)

    # All mapped aesthetics must have an entry in the legend
    for aesthetic in ('color', 'fill', 'alpha', 'linetype'):
        assert(aesthetic in legend)

    # None of the unassigned aesthetic should have an entry in the legend
    assert('size' not in legend)
    assert('shape' not in legend)

    # legend entries should remember the column names
    # to which they were mapped
    assert(legend['fill']['column_name'] == 'fill')
    assert(legend['color']['column_name'] == 'quality')
    assert(legend['linetype']['column_name'] == 'texture')
    assert(legend['alpha']['column_name'] == 'alpha')

    # Discrete columns for non-numeric data
    assert(legend['fill']['scale_type'] == 'discrete')
    assert(legend['color']['scale_type'] == 'discrete')
    assert(legend['linetype']['scale_type'] == 'discrete')
    assert(legend['alpha']['scale_type'] == 'continuous')

    # Alternate
    df2 = pd.DataFrame.copy(df)
    df2['fill'] = [90, 3.2, 8.1]
    gg = ggplot(df2, aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax',
                        colour='quality', fill='fill', alpha='alpha',
                        linetype='texture'))
    new_df, legend = assign_visual_mapping(df2, gg.aesthetics, gg)
    assert(legend['fill']['scale_type'] == 'continuous')
def test_different_markers():
    from ggplot.components import assign_visual_mapping
    from ggplot.components.shapes import shape_gen
    # First the generator which assigns the shapes
    shape = shape_gen()
    assert_true(six.next(shape) != six.next(shape), "Subsequent shapes are not different!")
    shape = shape_gen()
    assert_true(six.next(shape) == 'o', "First shape is not 'o'")
    assert_true(six.next(shape) == '^', "Second shape is not '^'")
    # Do shapes show up in the transformed layer?
    df = pd.DataFrame({"x":[1,2],"y":[1,2], "a":["a","b"], "b":["c","d"]})
    gg = ggplot(aes(x="x", y="y", shape="a", color="b"), data=df)
    new_df, _ = assign_visual_mapping(df,aes(x="x", y="y", shape="a", color="b"), gg)
    data = gg._make_plot_data(new_df)
    assert_true("shape" in data, "no shape was assigned")
    assert_true(data["shape"][0] != data["shape"][1], "wrong marker was assigned")
    # And now a visual test that both shapes are there. Make them big so that the test is failing
    # if something is wrong
    gg = ggplot(aes(x="x", y="y", shape="a", color="b"), data=df)
    assert_same_ggplot(gg + geom_point(size=3000), "geom_point_marker")
Example #4
0
def test_legend_structure():
    df = get_test_df()

    gg = ggplot(df, aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax',
                        colour='quality', fill='fill', alpha='alpha',
                        linetype='texture'))
    new_df, legend = assign_visual_mapping(df, gg.aesthetics, gg)

    # All mapped aesthetics must have an entry in the legend
    for aesthetic in ('color', 'fill', 'alpha', 'linetype'):
        assert(aesthetic in legend)

    # None of the unassigned aesthetic should have an entry in the legend
    assert('size' not in legend)
    assert('shape' not in legend)

    # legend entries should remember the column names
    # to which they were mapped
    assert(legend['fill']['column_name'] == 'fill')
    assert(legend['color']['column_name'] == 'quality')
    assert(legend['linetype']['column_name'] == 'texture')
    assert(legend['alpha']['column_name'] == 'alpha')

    # Discrete columns for non-numeric data
    assert(legend['fill']['scale_type'] == 'discrete')
    assert(legend['color']['scale_type'] == 'discrete')
    assert(legend['linetype']['scale_type'] == 'discrete')
    assert(legend['alpha']['scale_type'] == 'discrete')

    # Alternate
    df2 = pd.DataFrame.copy(df)
    df2['fill'] = [90, 3.2, 8.1]
    gg = ggplot(df2, aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax',
                        colour='quality', fill='fill', alpha='alpha',
                        linetype='texture'))
    new_df, legend = assign_visual_mapping(df2, gg.aesthetics, gg)
    assert(legend['fill']['scale_type'] == 'discrete')

    # Test if legend switches to continuous for more than 8 numerical values
    df3 = pd.DataFrame({
        'xmin': [1, 3, 5, 8, 2, 1, 4, 7, 9],
        'xmax': [2, 3.5, 7, 12, 3, 2, 6, 8, 10],
        'ymin': [1, 4, 6, 0, 0, 0, 0, 0, 0],
        'ymax': [5, 5, 9, 1, 1, 1, 1, 1, 1],
        'fill': ['blue', 'red', 'green', 'green', 'green',
                 'green', 'green', 'green', 'brown'],
        'quality': ['good', 'bad', 'ugly', 'horrible', 'quite awful',
                    'impertinent', 'jolly', 'hazardous', 'ok'],
        'alpha': [0.1, 0.2, 0.4, 0.5, 0.6, 0.65, 0.8, 0.82, 0.83],
        'texture': ['hard', 'soft', 'medium', 'fluffy', 'slimy', 'rough',
                    'edgy', 'corny', 'slanted']
    })
    gg = ggplot(df2, aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax',
                        colour='quality', fill='fill', alpha='alpha',
                        linetype='texture'))
    new_df, legend = assign_visual_mapping(df3, gg.aesthetics, gg)
    assert(legend['alpha']['scale_type'] == 'continuous')

    # Test if legend raises GgplotError when size and alpha is fed non numeric data
    gg = ggplot(df3, aes(size="fill"))
    assert_raises(GgplotError, assign_visual_mapping, df3, gg.aesthetics, gg)
    gg = ggplot(df3, aes(alpha="fill"))
    assert_raises(GgplotError, assign_visual_mapping, df3, gg.aesthetics, gg)
Example #5
0
def test_legend_structure():
    df = get_test_df()

    gg = ggplot(
        df,
        aes(xmin='xmin',
            xmax='xmax',
            ymin='ymin',
            ymax='ymax',
            colour='quality',
            fill='fill',
            alpha='alpha',
            linetype='texture'))
    new_df, legend = assign_visual_mapping(df, gg.aesthetics, gg)

    # All mapped aesthetics must have an entry in the legend
    for aesthetic in ('color', 'fill', 'alpha', 'linetype'):
        assert (aesthetic in legend)

    # None of the unassigned aesthetic should have an entry in the legend
    assert ('size' not in legend)
    assert ('shape' not in legend)

    # legend entries should remember the column names
    # to which they were mapped
    assert (legend['fill']['column_name'] == 'fill')
    assert (legend['color']['column_name'] == 'quality')
    assert (legend['linetype']['column_name'] == 'texture')
    assert (legend['alpha']['column_name'] == 'alpha')

    # Discrete columns for non-numeric data
    assert (legend['fill']['scale_type'] == 'discrete')
    assert (legend['color']['scale_type'] == 'discrete')
    assert (legend['linetype']['scale_type'] == 'discrete')
    assert (legend['alpha']['scale_type'] == 'discrete')

    # Alternate
    df2 = pd.DataFrame.copy(df)
    df2['fill'] = [90, 3.2, 8.1]
    gg = ggplot(
        df2,
        aes(xmin='xmin',
            xmax='xmax',
            ymin='ymin',
            ymax='ymax',
            colour='quality',
            fill='fill',
            alpha='alpha',
            linetype='texture'))
    new_df, legend = assign_visual_mapping(df2, gg.aesthetics, gg)
    assert (legend['fill']['scale_type'] == 'discrete')

    # Test if legend switches to continuous for more than 8 numerical values
    df3 = pd.DataFrame({
        'xmin': [1, 3, 5, 8, 2, 1, 4, 7, 9],
        'xmax': [2, 3.5, 7, 12, 3, 2, 6, 8, 10],
        'ymin': [1, 4, 6, 0, 0, 0, 0, 0, 0],
        'ymax': [5, 5, 9, 1, 1, 1, 1, 1, 1],
        'fill': [
            'blue', 'red', 'green', 'green', 'green', 'green', 'green',
            'green', 'brown'
        ],
        'quality': [
            'good', 'bad', 'ugly', 'horrible', 'quite awful', 'impertinent',
            'jolly', 'hazardous', 'ok'
        ],
        'alpha': [0.1, 0.2, 0.4, 0.5, 0.6, 0.65, 0.8, 0.82, 0.83],
        'texture': [
            'hard', 'soft', 'medium', 'fluffy', 'slimy', 'rough', 'edgy',
            'corny', 'slanted'
        ]
    })
    gg = ggplot(
        df2,
        aes(xmin='xmin',
            xmax='xmax',
            ymin='ymin',
            ymax='ymax',
            colour='quality',
            fill='fill',
            alpha='alpha',
            linetype='texture'))
    new_df, legend = assign_visual_mapping(df3, gg.aesthetics, gg)
    assert (legend['alpha']['scale_type'] == 'continuous')

    # Test if legend raises GgplotError when size and alpha is fed non numeric data
    gg = ggplot(df3, aes(size="fill"))
    assert_raises(GgplotError, assign_visual_mapping, df3, gg.aesthetics, gg)
    gg = ggplot(df3, aes(alpha="fill"))
    assert_raises(GgplotError, assign_visual_mapping, df3, gg.aesthetics, gg)