From b9b9223a2fd82849d03f4037552e2b357b4402b6 Mon Sep 17 00:00:00 2001 From: ammekk Date: Wed, 5 Jan 2022 10:55:50 -0500 Subject: [PATCH 1/2] basic structure up --- hail/python/hail/plot2/__init__.py | 2 ++ hail/python/hail/plot2/coord_cartesian.py | 17 +++++++++++++++++ hail/python/hail/plot2/ggplot.py | 6 +++++- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 hail/python/hail/plot2/coord_cartesian.py diff --git a/hail/python/hail/plot2/__init__.py b/hail/python/hail/plot2/__init__.py index 6672e7c5809..36020f437dd 100644 --- a/hail/python/hail/plot2/__init__.py +++ b/hail/python/hail/plot2/__init__.py @@ -1,3 +1,4 @@ +from .coord_cartesian import coord_cartesian from .ggplot import ggplot from .aes import aes from .geoms import geom_line, geom_point, geom_text, geom_bar, geom_histogram, geom_hline, geom_func, geom_vline, geom_tile @@ -20,6 +21,7 @@ "ggtitle", "xlab", "ylab", + "coord_cartesian", "scale_x_continuous", "scale_y_continuous", "scale_x_discrete", diff --git a/hail/python/hail/plot2/coord_cartesian.py b/hail/python/hail/plot2/coord_cartesian.py new file mode 100644 index 00000000000..f06a7592b66 --- /dev/null +++ b/hail/python/hail/plot2/coord_cartesian.py @@ -0,0 +1,17 @@ +from .geoms import FigureAttribute + + +class CoordCartesian(FigureAttribute): + def __init__(self, xlim, ylim): + self.xlim = xlim + self.ylim = ylim + + def apply_to_fig(self, fig_so_far): + if self.xlim is not None: + fig_so_far.update_xaxes(range=list(self.xlim)) + if self.ylim is not None: + fig_so_far.update_yaxes(range=list(self.ylim)) + + +def coord_cartesian(xlim=None, ylim=None): + return CoordCartesian(xlim, ylim) diff --git a/hail/python/hail/plot2/ggplot.py b/hail/python/hail/plot2/ggplot.py index 87fcffff6b9..8029d4d8753 100644 --- a/hail/python/hail/plot2/ggplot.py +++ b/hail/python/hail/plot2/ggplot.py @@ -5,6 +5,7 @@ import hail as hl +from .coord_cartesian import CoordCartesian from .geoms import Geom, FigureAttribute from .labels import Labels from .scale import Scale, scale_x_continuous, scale_x_genomic, scale_y_continuous, scale_x_discrete, scale_y_discrete @@ -13,7 +14,7 @@ class GGPlot: - def __init__(self, ht, aes, geoms=[], labels=Labels(), scales=None, + def __init__(self, ht, aes, geoms=[], labels=Labels(), coord_cartesian=None, scales=None, discrete_color_scale=plotly.colors.qualitative.D3, continuous_color_scale=plotly.colors.sequential.Viridis): if scales is None: scales = {} @@ -22,6 +23,7 @@ def __init__(self, ht, aes, geoms=[], labels=Labels(), scales=None, self.aes = aes self.geoms = geoms self.labels = labels + self.coord_cartesian = coord_cartesian self.scales = scales self.discrete_color_scale = discrete_color_scale self.discrete_color_dict = {} @@ -39,6 +41,8 @@ def __add__(self, other): copied.add_default_scales(other.aes) elif isinstance(other, Labels): copied.labels = copied.labels.merge(other) + elif isinstance(other, CoordCartesian): + copied.coord_cartesian = other elif isinstance(other, Scale): copied.scales[other.aesthetic_name] = other elif isinstance(other, Aesthetic): From cf3cc1fa56b3cf1d0486cddd873bbba9a81c0de7 Mon Sep 17 00:00:00 2001 From: ammekk Date: Wed, 5 Jan 2022 11:24:46 -0500 Subject: [PATCH 2/2] added coord_cartesian.apply_to_fig in ggplot.render --- hail/python/hail/plot2/ggplot.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hail/python/hail/plot2/ggplot.py b/hail/python/hail/plot2/ggplot.py index 8029d4d8753..e511edfd731 100644 --- a/hail/python/hail/plot2/ggplot.py +++ b/hail/python/hail/plot2/ggplot.py @@ -81,7 +81,7 @@ def is_genomic_type(dtype): self.scales["y"] = scale_y_discrete() def copy(self): - return GGPlot(self.ht, self.aes, self.geoms[:], self.labels, self.scales, + return GGPlot(self.ht, self.aes, self.geoms[:], self.labels, self.coord_cartesian, self.scales, self.discrete_color_scale, self.continuous_color_scale) def render(self): @@ -122,6 +122,8 @@ def render(self): self.scales["x"].apply_to_fig(self, fig) if self.scales.get("y") is not None: self.scales["y"].apply_to_fig(self, fig) + if self.coord_cartesian is not None: + self.coord_cartesian.apply_to_fig(fig) return fig