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")
def assign_linestyles(data, aes, gg): """Assigns line styles to the given data based on the aes and adds the right legend Parameters ---------- data : DataFrame dataframe which should have shapes assigned to aes : aesthetic mapping, including a mapping from line style to variable gg : ggplot object, which holds information and gets a legend assigned Returns ------- data : DataFrame the changed dataframe """ if 'linestyle' in aes: linestyle_col = aes['linestyle'] possible_linestyles = np.unique(data[linestyle_col]) linestyle = line_gen() linestyle_mapping = {value: six.next(linestyle) for value in possible_linestyles} data['linestyle_mapping'] = data[linestyle_col].apply(lambda x: linestyle_mapping[x]) gg.add_to_legend('linestyle', {v: k for k, v in linestyle_mapping.items()}) return data
def assign_shapes(data, aes, gg): """Assigns shapes to the given data based on the aes and adds the right legend Parameters ---------- data : DataFrame dataframe which should have shapes assigned to aes : aesthetic mapping, including a mapping from shapes to variable gg : ggplot object, which holds information and gets a legend assigned Returns ------- data : DataFrame the changed dataframe """ if 'shape' in aes: shape_col = aes['shape'] possible_shapes = np.unique(data[shape_col]) shape = shape_gen() # marker in matplotlib are not unicode ready in 1.3.1 :-( -> use explicit str()... shape_mapping = { value: str(six.next(shape)) for value in possible_shapes } data['shape_mapping'] = data[shape_col].apply( lambda x: shape_mapping[x]) gg.add_to_legend("marker", {v: k for k, v in shape_mapping.items()}) return data
def assign_colors(data, aes, gg): """ Assigns colors to the given data based on the aes and adds the right legend We need to take a value an convert it into colors that we can actually plot. This means checking to see if we're colorizing a discrete or continuous value, checking if their is a colormap, etc. Parameters ---------- data : DataFrame dataframe which should have shapes assigned to aes : aesthetic mapping, including a mapping from color to variable gg : ggplot object, which holds information and gets a legend assigned Returns ------- data : DataFrame the changed dataframe """ if 'color' in aes: color_col = aes['color'] # Handle continuous colors here. We're going to use whatever colormap # is defined to evaluate for each value. We're then going to convert # each color to HEX so that it can fit in 1 column. This will make it # much easier when creating layers. We're also going to evaluate the # quantiles for that particular column to generate legend scales. This # isn't what ggplot does, but it's good enough for now. if color_col in data._get_numeric_data().columns: values = data[color_col].tolist() # Normalize the values for the colormap values = [(i - min(values)) / (max(values) - min(values)) for i in values] color_mapping = gg.colormap(values)[::, :3] data["color_mapping"] = [rgb2hex(value) for value in color_mapping] quantiles = np.percentile(gg.data[color_col], [0, 25, 50, 75, 100]) key_colors = gg.colormap([0, 25, 50, 75, 100])[::, :3] key_colors = [rgb2hex(value) for value in key_colors] gg.add_to_legend("color", dict(zip(key_colors, quantiles)), scale_type="continuous") # Handle discrete colors here. We're going to check and see if the user # has defined their own color palette. If they have then we'll use those # colors for mapping. If not, then we'll generate some default colors. # We also have to be careful here because for some odd reason the next() # function is different in Python 2.7 and Python 3.0. Once we've done that # we generate the legends based off the the (color -> value) mapping. else: possible_colors = np.unique(data[color_col]) if gg.manual_color_list: color = color_gen(len(possible_colors), gg.manual_color_list) else: color = color_gen(len(possible_colors)) color_mapping = {value: six.next(color) for value in possible_colors} data["color_mapping"] = data[color_col].apply(lambda x: color_mapping[x]) gg.add_to_legend("color", {v: k for k, v in color_mapping.items()}) return data
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")