def style_edges(self, edges, line, legendgroup=None, tip2=None, **kwargs): """adjust display layout for the edges Parameters ---------- edges : str or series names of edges line : dict with plotly line style to applied to these edges legendgroup : str or None if str, a legend will be presented tip2 : str if provided, and edges is a str, passes edges (as tip1) and kwargs to get_edge_names kwargs keyword arguments passed onto get_edge_names """ if tip2: assert type(edges) == str, "cannot use a series of edges and tip2" edges = self.get_edge_names(edges, tip2, **kwargs) if type(edges) == str: edges = [edges] edges = frozenset(edges) style = UnionDict(width=self._line_width, color=self._line_color) style.update(line) self._edge_sets[edges] = UnionDict(legendgroup=legendgroup, line=style) mapping = {e: edges for e in edges} self._edge_mapping.update(mapping) if legendgroup: self.layout["showlegend"] = True # need to trigger recreation of figure self._traces = []
def test_construct_from_empty(self): """successfully define from an empty""" data = {"width": 600.0, "xaxis": {"title": {"text": "Alignment Position"}}} # empty object d = UnionDict() self.assertTrue(len(d) == 0) # using update d.update(data) self.assertEqual(d.xaxis.title.text, "Alignment Position")
class Drawable: """container object for Plotly figures""" def __init__( self, title=None, traces=None, width=None, height=None, showlegend=True, visible_axes=True, layout=None, xtitle=None, ytitle=None, ): self._traces = traces or [] title = title if title is None else dict(text=title) self._default_layout = UnionDict( title=title, font=dict(family="Balto", size=14), width=width, height=height, autosize=False, showlegend=showlegend, xaxis=dict(visible=visible_axes), yaxis=dict(visible=visible_axes), hovermode="closest", template=None, plot_bgcolor=None, margin=dict(l=50, r=50, t=50, b=50, pad=4), ) layout = layout or {} self.layout = UnionDict(self._default_layout) self.layout |= layout self.xtitle = xtitle self.ytitle = ytitle def _repr_html_(self): self.show() @property def layout(self): if not hasattr(self, "_layout"): self._layout = UnionDict() return self._layout @layout.setter def layout(self, value): self.layout.update(value) @property def traces(self): return self._traces def get_trace_titles(self): titles = [tr.name for tr in self.traces] return titles def pop_trace(self, title): """removes the trace with a matching title attribute""" try: index = self.get_trace_titles().index(title) except ValueError: UserWarning(f"no trace with name {title}") return return self.traces.pop(index) def remove_traces(self, names): """removes traces by name Parameters ---------- names : str or iterable of str trace names """ if not self.traces: self._build_fig() names = names if type(names) != str else [names] for name in names: _ = self.pop_trace(name) def add_trace(self, trace): self.traces.append(trace) def bound_to(self, obj): """returns obj with self bound to it""" return bind_drawable(obj, self) @property def figure(self): if not self.traces: self._build_fig() xtitle = self.xtitle if not self.xtitle else dict(text=self.xtitle) ytitle = self.ytitle if not self.ytitle else dict(text=self.ytitle) self.layout.xaxis.title = xtitle self.layout.yaxis.title = ytitle return UnionDict(data=self.traces, layout=self.layout) def iplot(self, *args, **kwargs): from plotly.offline import iplot as _iplot _iplot(self.figure, *args, **kwargs) @extend_docstring_from(_show_) def show(self, renderer=None, **kwargs): _show_(self, renderer, **kwargs) def write(self, path, **kwargs): """writes static image file, suffix dictates format""" from plotly.io import write_image fig = self.figure kwargs["width"] = kwargs.get("width", fig.layout.width) kwargs["height"] = kwargs.get("height", fig.layout.height) write_image(fig, path, **kwargs) def to_image(self, format="png", **kwargs): """creates static image, suffix dictates format""" from plotly.io import to_image fig = self.figure kwargs["width"] = kwargs.get("width", fig.layout.width) kwargs["height"] = kwargs.get("height", fig.layout.height) return to_image(fig, format=format, **kwargs) @property def fig_width(self): """figure width, also settable via .layout.width""" return self.layout.width @fig_width.setter def fig_width(self, width): self.layout.width = width @property def fig_height(self): """figure height, also settable via .layout.height""" return self.layout.height @fig_height.setter def fig_height(self, height): self.layout.height = height