""" Renderer Module This module defines the PlotlyRenderer class and a single function, fig_to_plotly, which is intended to be the main way that user's will interact with the matplotlylib package. """ import math import warnings import matplotlib.patches as mpatches from matplotlib import lines as mlines from matplotlib import transforms import plotly.graph_objs as go from plotly.matplotlylib.mplexporter import Renderer from plotly.matplotlylib import mpltools from plotly.matplotlylib.mpltools import _export_color # Artist class created by ``Axes.axline``: ``AxLine`` in matplotlib >= 3.8, # ``_AxLine`` in earlier versions. _AXLINE_CLASS = getattr(mlines, "AxLine", None) or getattr(mlines, "_AxLine", ()) def _per_path(values, i, default): """Return the style value for the i-th path of a path collection. A scalar (or string) value applies to every path, a sequence is cycled through like matplotlib does, and None or an empty sequence yields `default`. """ if isinstance(values, str): return values if values is None: return default try: n = len(values) except TypeError: return values return values[i % n] if n else default def _convert_collection_dash(linestyle): """Convert a matplotlib collection line style to a plotly dash string. Collections report line styles as (offset, dashes) tuples, with dashes in points and already scaled by line width (None for solid lines). Line widths are exported with their point values used as px, so the dashes are exported the same way, which keeps matplotlib's dash-to-width ratio. plotly has no dash offset, so the offset is dropped. """ dashes = linestyle[1] if linestyle is not None else None if not dashes: return "solid" return ",".join("{0:g}px".format(d) for d in dashes) class PlotlyRenderer(Renderer): """A renderer class inheriting from base for rendering mpl plots in plotly. A renderer class to be used with an exporter for rendering matplotlib plots in Plotly. This module defines the PlotlyRenderer class which handles the creation of the JSON structures that get sent to plotly. All class attributes available are defined in __init__(). Basic Usage: # (mpl code) # fig = gcf() renderer = PlotlyRenderer(fig) exporter = Exporter(renderer) exporter.run(fig) # ... et voila """ def __init__(self): """Initialize PlotlyRenderer obj. PlotlyRenderer obj is called on by an Exporter object to draw matplotlib objects like figures, axes, text, etc. All class attributes are listed here in the __init__ method. """ self.plotly_fig = go.Figure() self.mpl_fig = None self.current_mpl_ax = None self.bar_containers = None self.current_bars = [] self.axis_ct = 0 self.x_is_mpl_date = False self.mpl_x_bounds = (0, 1) self.mpl_y_bounds = (0, 1) self.msg = "Initialized PlotlyRenderer\n" self._processing_legend = False self._legend_visible = False self.axes_list = [] def _convert_x_dates(self, x): """Convert x values to date strings when the x-axis is a date axis.""" if self.x_is_mpl_date: formatter = ( self.current_mpl_ax.get_xaxis().get_major_formatter().__class__.__name__ ) x = mpltools.mpl_dates_to_datestrings(x, formatter) return x def open_figure(self, fig, props): """Creates a new figure by beginning to fill out layout dict. The 'autosize' key is set to false so that the figure will mirror sizes set by mpl. The 'hovermode' key controls what shows up when you mouse around a figure in plotly, it's set to show the 'closest' point. Positional agurments: fig -- a matplotlib.figure.Figure object. props.keys(): [ 'figwidth', 'figheight', 'dpi' ] """ self.msg += "Opening figure\n" self.mpl_fig = fig self.plotly_fig["layout"] = go.Layout( width=int(props["figwidth"] * props["dpi"]), height=int(props["figheight"] * props["dpi"]), autosize=False, hovermode="closest", # plotly.js auto-names unnamed traces "trace N" and shows them # in the legend; the legend is only enabled when the mpl figure # actually has one (see open_legend) showlegend=False, ) self.plotly_fig["layout"].paper_bgcolor = _export_color(props["figbg"]) self.mpl_x_bounds, self.mpl_y_bounds = mpltools.get_axes_bounds(fig) margin = go.layout.Margin( l=int(self.mpl_x_bounds[0] * self.plotly_fig["layout"]["width"]), r=int((1 - self.mpl_x_bounds[1]) * self.plotly_fig["layout"]["width"]), t=int((1 - self.mpl_y_bounds[1]) * self.plotly_fig["layout"]["height"]), b=int(self.mpl_y_bounds[0] * self.plotly_fig["layout"]["height"]), pad=0, ) self.plotly_fig["layout"]["margin"] = margin def close_figure(self, fig): """Closes figure by cleaning up data and layout dictionaries. The PlotlyRenderer's job is to create an appropriate set of data and layout dictionaries. When the figure is closed, some cleanup and repair is necessary. This method removes inappropriate dictionary entries, freeing up Plotly to use defaults and best judgements to complete the entries. This method is called by an Exporter object. Positional arguments: fig -- a matplotlib.figure.Figure object. """ self.msg += "Closing figure\n" def open_axes(self, ax, props): """Setup a new axes object (subplot in plotly). Plotly stores information about subplots in different 'xaxis' and 'yaxis' objects which are numbered. These are just dictionaries included in the layout dictionary. This function takes information from the Exporter, fills in appropriate dictionary entries, and updates the layout dictionary. PlotlyRenderer keeps track of the number of plots by incrementing the axis_ct attribute. Setting the proper plot domain in plotly is a bit tricky. Refer to the documentation for mpltools.convert_x_domain and mpltools.convert_y_domain. Positional arguments: ax -- an mpl axes object. This will become a subplot in plotly. props.keys() -- [ 'axesbg', (background color for axes obj) 'axesbgalpha', (alpha, or opacity for background) 'bounds', ((x0, y0, width, height) for axes) 'dynamic', (zoom/pan-able?) 'axes', (list: [xaxis, yaxis]) 'xscale', (log, linear, or date) 'yscale', 'xlim', (range limits for x) 'ylim', 'xdomain' (xdomain=xlim, unless it's a date) 'ydomain' ] """ self.msg += " Opening axes\n" self.current_mpl_ax = ax self.bar_containers = [ c for c in ax.containers # empty is OK if c.__class__.__name__ == "BarContainer" ] self.current_bars = [] self.axis_ct += 1 if props.get("patch_visible", True): self.plotly_fig["layout"].plot_bgcolor = _export_color(props["axesbg"]) # set defaults in axes xaxis = go.layout.XAxis( anchor="y{0}".format(self.axis_ct), zeroline=False, ticks="inside" ) yaxis = go.layout.YAxis( anchor="x{0}".format(self.axis_ct), zeroline=False, ticks="inside" ) # update defaults with things set in mpl mpl_xaxis, mpl_yaxis = mpltools.prep_xy_axis( ax=ax, props=props, x_bounds=self.mpl_x_bounds, y_bounds=self.mpl_y_bounds ) xaxis.update(mpl_xaxis) yaxis.update(mpl_yaxis) x_spine = "top" if xaxis["side"] == "top" else "bottom" y_spine = "right" if yaxis["side"] == "right" else "left" xaxis["linecolor"] = _export_color(ax.spines[x_spine].get_edgecolor()) yaxis["linecolor"] = _export_color(ax.spines[y_spine].get_edgecolor()) bottom_spine = mpltools.get_spine_visible(ax, "bottom") top_spine = mpltools.get_spine_visible(ax, "top") left_spine = mpltools.get_spine_visible(ax, "left") right_spine = mpltools.get_spine_visible(ax, "right") x_tick_params = ax.xaxis.get_tick_params() y_tick_params = ax.yaxis.get_tick_params() bottom_tick_markers = x_tick_params.get( "bottom", x_tick_params.get("left", True) ) top_tick_markers = x_tick_params.get("top", x_tick_params.get("right", False)) left_tick_markers = y_tick_params.get("left", True) right_tick_markers = y_tick_params.get("right", False) if xaxis["side"] == "top": x_main_spine, x_mirror_spine = top_spine, bottom_spine x_main_ticks, x_mirror_ticks = top_tick_markers, bottom_tick_markers else: x_main_spine, x_mirror_spine = bottom_spine, top_spine x_main_ticks, x_mirror_ticks = bottom_tick_markers, top_tick_markers xaxis["mirror"] = mpltools.get_axis_mirror( x_main_spine, x_mirror_spine, x_main_ticks, x_mirror_ticks ) xaxis["showline"] = x_main_spine # hide tick markers when the mpl main-side tick markers are hidden if not x_main_ticks: xaxis["ticks"] = "" if yaxis["side"] == "right": y_main_spine, y_mirror_spine = right_spine, left_spine y_main_ticks, y_mirror_ticks = right_tick_markers, left_tick_markers else: y_main_spine, y_mirror_spine = left_spine, right_spine y_main_ticks, y_mirror_ticks = left_tick_markers, right_tick_markers yaxis["mirror"] = mpltools.get_axis_mirror( y_main_spine, y_mirror_spine, y_main_ticks, y_mirror_ticks ) yaxis["showline"] = y_main_spine if not y_main_ticks: yaxis["ticks"] = "" overlay_ax_ct = None for prev_ax, prev_ct in self.axes_list: # Overlay only axes that cover the same area. Shared-axis subplots, such # as the ones from plt.subplots(sharex=True), sit in different places. if ax.get_position().bounds == prev_ax.get_position().bounds: overlay_ax_ct = prev_ct break if overlay_ax_ct is not None: overlay_x = "x" if overlay_ax_ct == 1 else "x{0}".format(overlay_ax_ct) overlay_y = "y" if overlay_ax_ct == 1 else "y{0}".format(overlay_ax_ct) xaxis["overlaying"] = overlay_x yaxis["overlaying"] = overlay_y if not props["axes"][0]["visible"]: xaxis["visible"] = False if not props["axes"][1]["visible"]: yaxis["visible"] = False self.axes_list.append((ax, self.axis_ct)) # put axes in our figure self.plotly_fig["layout"]["xaxis{0}".format(self.axis_ct)] = xaxis self.plotly_fig["layout"]["yaxis{0}".format(self.axis_ct)] = yaxis # let all subsequent dates be handled properly if required if "type" in dir(xaxis) and xaxis["type"] == "date": self.x_is_mpl_date = True def close_axes(self, ax): """Close the axes object and clean up. Bars from bar charts are given to PlotlyRenderer one-by-one, thus they need to be taken care of at the close of each axes object. The self.current_bars variable should be empty unless a bar chart has been created. Positional arguments: ax -- an mpl axes object, not required at this time. """ self.draw_bars(self.current_bars) self.msg += " Closing axes\n" self.x_is_mpl_date = False def open_legend(self, legend, props): """Enable Plotly's native legend when matplotlib legend is detected. This method is called when a matplotlib legend is found. It enables Plotly's showlegend only if the matplotlib legend is visible. Positional arguments: legend -- matplotlib.legend.Legend object props -- legend properties dictionary """ self.msg += " Opening legend\n" self._processing_legend = True self._legend_visible = props.get("visible", True) if self._legend_visible: self.msg += ( " Enabling native plotly legend (matplotlib legend is visible)\n" ) self.plotly_fig["layout"]["showlegend"] = True else: self.msg += " Not enabling legend (matplotlib legend is not visible)\n" def close_legend(self, legend): """Finalize legend processing. Positional arguments: legend -- matplotlib.legend.Legend object """ self.msg += " Closing legend\n" self._processing_legend = False self._legend_visible = False def draw_bars(self, bars): # sort bars according to bar containers mpl_traces = [] for container in self.bar_containers: mpl_traces.append( [ bar_props for bar_props in self.current_bars if bar_props["mplobj"] in container ] ) for trace in mpl_traces: self.draw_bar(trace) def draw_bar(self, coll): """Draw a collection of similar patches as a bar chart. After bars are sorted, an appropriate data dictionary must be created to tell plotly about this data. Just like draw_line or draw_markers, draw_bar translates patch/path information into something plotly understands. Positional arguments: patch_coll -- a collection of patches to be drawn as a bar chart. """ tol = 1e-10 trace = [mpltools.make_bar(**bar_props) for bar_props in coll] widths = [bar_props["x1"] - bar_props["x0"] for bar_props in trace] heights = [bar_props["y1"] - bar_props["y0"] for bar_props in trace] vertical = abs(sum(widths[0] - widths[iii] for iii in range(len(widths)))) < tol horizontal = ( abs(sum(heights[0] - heights[iii] for iii in range(len(heights)))) < tol ) if vertical and horizontal: # Check for monotonic x. Can't both be true! x_zeros = [bar_props["x0"] for bar_props in trace] if all( (x_zeros[iii + 1] > x_zeros[iii] for iii in range(len(x_zeros[:-1]))) ): orientation = "v" else: orientation = "h" elif vertical: orientation = "v" else: orientation = "h" if orientation == "v": self.msg += " Attempting to draw a vertical bar chart\n" old_heights = [bar_props["y1"] for bar_props in trace] for bar in trace: bar["y0"], bar["y1"] = 0, bar["y1"] - bar["y0"] new_heights = [bar_props["y1"] for bar_props in trace] # check if we're stacked or not... for old, new in zip(old_heights, new_heights): if abs(old - new) > tol: self.plotly_fig["layout"]["barmode"] = "stack" self.plotly_fig["layout"]["hovermode"] = "x" x = [bar["x0"] + (bar["x1"] - bar["x0"]) / 2 for bar in trace] y = [bar["y1"] for bar in trace] bar_gap = mpltools.get_bar_gap( [bar["x0"] for bar in trace], [bar["x1"] for bar in trace] ) if self.x_is_mpl_date: x = self._convert_x_dates([bar["x0"] for bar in trace]) else: self.msg += " Attempting to draw a horizontal bar chart\n" old_rights = [bar_props["x1"] for bar_props in trace] for bar in trace: bar["x0"], bar["x1"] = 0, bar["x1"] - bar["x0"] new_rights = [bar_props["x1"] for bar_props in trace] # check if we're stacked or not... for old, new in zip(old_rights, new_rights): if abs(old - new) > tol: self.plotly_fig["layout"]["barmode"] = "stack" self.plotly_fig["layout"]["hovermode"] = "y" x = [bar["x1"] for bar in trace] y = [bar["y0"] + (bar["y1"] - bar["y0"]) / 2 for bar in trace] bar_gap = mpltools.get_bar_gap( [bar["y0"] for bar in trace], [bar["y1"] for bar in trace] ) bar = go.Bar( orientation=orientation, x=x, y=y, xaxis="x{0}".format(self.axis_ct), yaxis="y{0}".format(self.axis_ct), opacity=trace[0]["alpha"], # TODO: get all alphas if array? marker=go.bar.Marker( color=_export_color(trace[0]["facecolor"]), # TODO: get all line=dict(width=trace[0]["edgewidth"]), ), ) # TODO ditto if len(bar["x"]) > 1: self.msg += " Heck yeah, I drew that bar chart\n" self.plotly_fig.add_trace(bar) if bar_gap is not None: self.plotly_fig["layout"]["bargap"] = bar_gap else: self.msg += " Bar chart not drawn\n" warnings.warn( "found box chart data with length <= 1, " "assuming data redundancy, not plotting." ) def draw_marked_line(self, **props): """Create a data dict for a line obj. This will draw 'lines', 'markers', or 'lines+markers'. For legend elements, this will use layout.shapes, so they can be positioned with paper refs. props.keys() -- [ 'coordinates', ('data', 'axes', 'figure', or 'display') 'data', (a list of xy pairs) 'mplobj', (the matplotlib.lines.Line2D obj being rendered) 'label', (the name of the Line2D obj being rendered) 'linestyle', (linestyle dict, can be None, see below) 'markerstyle', (markerstyle dict, can be None, see below) ] props['linestyle'].keys() -- [ 'alpha', (opacity of Line2D obj) 'color', (color of the line if it exists, not the marker) 'linewidth', 'dasharray', (code for linestyle, see DASH_MAP in mpltools.py) 'zorder', (viewing precedence when stacked with other objects) ] props['markerstyle'].keys() -- [ 'alpha', (opacity of Line2D obj) 'marker', (the mpl marker symbol, see SYMBOL_MAP in mpltools.py) 'facecolor', (color of the marker face) 'edgecolor', (color of the marker edge) 'edgewidth', (width of marker edge) 'markerpath', (an SVG path for drawing the specified marker) 'zorder', (viewing precedence when stacked with other objects) ] """ self.msg += " Attempting to draw a line " line, marker, shape = {}, {}, {} if props["linestyle"] and props["markerstyle"]: self.msg += "... with both lines+markers\n" mode = "lines+markers" elif props["linestyle"]: self.msg += "... with just lines\n" mode = "lines" elif props["markerstyle"]: self.msg += "... with just markers\n" mode = "markers" if props["linestyle"]: color = _export_color( props["linestyle"]["color"], props["linestyle"]["alpha"] ) if props["coordinates"] == "data": line = go.scatter.Line( color=color, width=props["linestyle"]["linewidth"], dash=mpltools.convert_dash(props["linestyle"]["dasharray"]), shape=mpltools.convert_drawstyle(props["linestyle"]["drawstyle"]), ) else: shape = dict( line=dict( color=color, width=props["linestyle"]["linewidth"], dash=mpltools.convert_dash(props["linestyle"]["dasharray"]), ) ) if props["markerstyle"]: if props["coordinates"] == "data": marker = go.scatter.Marker( opacity=props["markerstyle"]["alpha"], color=_export_color(props["markerstyle"]["facecolor"]), symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]), size=props["markerstyle"]["markersize"], line=dict( color=_export_color(props["markerstyle"]["edgecolor"]), width=props["markerstyle"]["edgewidth"], ), ) else: shape = dict( opacity=props["markerstyle"]["alpha"], fillcolor=_export_color(props["markerstyle"]["facecolor"]), symbol=mpltools.convert_symbol(props["markerstyle"]["marker"]), size=props["markerstyle"]["markersize"], line=dict( color=_export_color(props["markerstyle"]["edgecolor"]), width=props["markerstyle"]["edgewidth"], ), ) if props["coordinates"] == "data": label = props["label"] # matplotlib uses "_nolegend_" and auto-generated "_childN" # labels for artists that must not appear in a legend if not label or (isinstance(label, str) and label.startswith("_")): label = None showlegend = False else: showlegend = None marked_line = go.Scatter( mode=mode, name=label, showlegend=showlegend, x=[xy_pair[0] for xy_pair in props["data"]], y=[xy_pair[1] for xy_pair in props["data"]], xaxis="x{0}".format(self.axis_ct), yaxis="y{0}".format(self.axis_ct), line=line, marker=marker, ) if self.x_is_mpl_date: marked_line["x"] = self._convert_x_dates(marked_line["x"]) self.plotly_fig.add_trace(marked_line) self.msg += " Heck yeah, I drew that line\n" elif props["coordinates"] == "axes" and self._processing_legend: # dealing with legend graphical elements self.msg += " Using native legend\n" elif self._is_axes_reference_line(props) and self._draw_axes_line(props): pass else: self.msg += " Line didn't have 'data' coordinates, not drawing\n" warnings.warn( "Bummer! Plotly can currently only draw Line2D " "objects from matplotlib that are in 'data' " "coordinates!" ) def _is_axes_reference_line(self, props): """Check whether a line qualifies as an axes-coordinate reference line (e.g. axhline, axvline, horizontal/vertical axline, or a 2-point segment drawn with ``transform=ax.transAxes``) that can be drawn as a 2-point layout shape.""" if not props.get("linestyle") or len(props.get("data", [])) != 2: return False if props["coordinates"] == "axes" and not self._processing_legend: return True if props["coordinates"] == "display" and isinstance( props["mplobj"].get_transform(), transforms.BlendedGenericTransform ): return True return False def _draw_axes_line(self, props): """Draw an axes-coordinate reference line as a layout shape. axhline/axvline and horizontal/vertical axline span the axes domain along one axis and sit at a data value on the other. Segments in axes coordinates keep their exact endpoints in axes domain coordinates. Returns True when a shape is drawn.""" if not props.get("linestyle") or len(props.get("data", [])) != 2: return False ax = self.current_mpl_ax line = props["mplobj"] trans = line.get_transform() axis_suffix = str(self.axis_ct) if self.axis_ct > 1 else "" x_axis = "x{0}".format(axis_suffix) y_axis = "y{0}".format(axis_suffix) x_domain = "{0} domain".format(x_axis).strip() y_domain = "{0} domain".format(y_axis).strip() if isinstance(trans, transforms.BlendedGenericTransform) and ( trans == ax.get_yaxis_transform() or trans._x == ax.transAxes ): # axhline: x spans the axes domain [xmin, xmax], y is in data coordinates x_data = line.get_xdata(orig=False) y_data = line.get_ydata(orig=False) x0, x1 = float(x_data[0]), float(x_data[1]) y0, y1 = float(y_data[0]), float(y_data[1]) xref = x_domain yref = y_axis elif isinstance(trans, transforms.BlendedGenericTransform) and ( trans == ax.get_xaxis_transform() or trans._y == ax.transAxes ): # axvline: x is in data coordinates, y spans the axes domain [ymin, ymax] x_data = line.get_xdata(orig=False) y_data = line.get_ydata(orig=False) x0, x1 = float(x_data[0]), float(x_data[1]) y0, y1 = float(y_data[0]), float(y_data[1]) if self.x_is_mpl_date: x0, x1 = self._convert_x_dates([x0, x1]) xref = x_axis yref = y_domain elif props["coordinates"] == "axes" and not isinstance(line, _AXLINE_CLASS): # Segment fixed to the axes (e.g. transform=ax.transAxes): props["data"] # holds its endpoints as axes fractions, which map directly onto the # axes domain, so the segment keeps its extent and stays put on pan/zoom (x0, y0), (x1, y1) = [(float(x), float(y)) for x, y in props["data"]] xref = x_domain yref = y_domain else: # general reference line (e.g. horizontal or vertical axline) if props["coordinates"] == "display": px_points = props["data"] elif props["coordinates"] == "axes": px_points = [ax.transAxes.transform(pt) for pt in props["data"]] else: px_points = [trans.transform(pt) for pt in props["data"]] (x0, y0), (x1, y1) = [ ax.transData.inverted().transform(pt) for pt in px_points ] dx = x1 - x0 dy = y1 - y0 if math.isclose(dy, 0.0, abs_tol=1e-12): # Horizontal line: use x domain so it spans the chart, y in data coordinates x0, x1 = 0.0, 1.0 y0, y1 = float(y0), float(y1) xref = x_domain yref = y_axis elif math.isclose(dx, 0.0, abs_tol=1e-12): # Vertical line: use y domain so it spans the chart, x in data coordinates y0, y1 = 0.0, 1.0 if self.x_is_mpl_date: x0, x1 = self._convert_x_dates([x0, x1]) else: x0, x1 = float(x0), float(x1) xref = x_axis yref = y_domain else: return False color = _export_color(props["linestyle"]["color"], props["linestyle"]["alpha"]) shape = go.layout.Shape( type="line", x0=x0, y0=y0, x1=x1, y1=y1, xref=xref, yref=yref, line=go.layout.shape.Line( color=color, width=props["linestyle"]["linewidth"], dash=mpltools.convert_dash(props["linestyle"]["dasharray"]), ), layer="above", ) self.plotly_fig["layout"]["shapes"] += (shape,) self.msg += " Heck yeah, I drew that reference line\n" return True def draw_image(self, **props): """Draw image. Not implemented yet! """ self.msg += " Attempting to draw image\n" self.msg += " Not drawing image\n" warnings.warn( "Aw. Snap! You're gonna have to hold off on " "the selfies for now. Plotly can't import " "images from matplotlib yet!" ) def draw_path_collection(self, **props): """Add a path collection to data list as a scatter plot. Current implementation defaults such collections as scatter plots. Matplotlib supports collections that have many of the same parameters in common like color, size, path, etc. However, they needn't all be the same. Plotly does not currently support such functionality and therefore, the style for the first object is taken and used to define the remaining paths in the collection. props.keys() -- [ 'paths', (structure: [vertices, path_code]) 'path_coordinates', ('data', 'axes', 'figure', or 'display') 'path_transforms', (mpl transform, including Affine2D matrix) 'offsets', (offset from axes, helpful if in 'data') 'offset_coordinates', ('data', 'axes', 'figure', or 'display') 'offset_order', 'styles', (style dict, see below) 'mplobj' (the collection obj being drawn) ] props['styles'].keys() -- [ 'linewidth', (one or more linewidths) 'facecolor', (one or more facecolors for path) 'edgecolor', (one or more edgecolors for path) 'linestyle', (one or more (offset, dashes) line styles) 'alpha', (one or more opacites for path) 'zorder', (precedence when stacked) ] """ self.msg += " Attempting to draw a path collection\n" if props["offset_coordinates"] == "data": markerstyle = mpltools.get_markerstyle_from_collection(props) scatter_props = { "coordinates": "data", "data": props["offsets"], "label": None, "markerstyle": markerstyle, "linestyle": None, } self.msg += " Drawing path collection as markers\n" self.draw_marked_line(**scatter_props) elif props["path_coordinates"] == "data": if len(props["styles"]["facecolor"]) == 0: # no face colors: a line collection (e.g. contour lines) self.msg += " Drawing path collection as lines\n" self._draw_line_collection(props) else: self.msg += " Drawing path collection as filled polygons\n" self._draw_filled_path_collection(props) else: self.msg += " Path collection not linked to 'data', not drawing\n" warnings.warn( "Dang! That path collection is out of this " "world. I totally don't know what to do with " "it yet! Plotly can only import path " "collections linked to 'data' coordinates" ) def _draw_line_collection(self, props): """Draw a path collection without face colors (e.g. contour lines) as plain lines, grouping consecutive same-style paths into single traces.""" edgecolors = mpltools.convert_rgba_array(props["styles"]["edgecolor"]) linewidths = mpltools.convert_linewidth_array(props["styles"]["linewidth"]) linestyles = props["styles"].get("linestyle") current_style = None grouped_x = [] grouped_y = [] def flush(): if current_style is not None and grouped_x: ec, lw, d = current_style self.plotly_fig.add_trace( go.Scatter( x=grouped_x, y=grouped_y, mode="lines", showlegend=False, line=go.scatter.Line( color=_export_color(ec), width=lw, dash=d, ), xaxis="x{0}".format(self.axis_ct), yaxis="y{0}".format(self.axis_ct), ) ) for i, (verts, codes) in enumerate(props["paths"]): # a path may contain several disjoint lines (e.g. contour lines # of the same level); separate disjoint subpaths with None so # plotly does not connect them. # In SVG paths, codes carry different numbers of vertices: # M/L: 1, C: 3 (cubic curve), S: 2 (smooth/quad curve), Z: 0. code_steps = {"M": 1, "L": 1, "C": 3, "S": 2, "Z": 0} subpaths = [] current = [] closed = False vi = 0 for c in codes: step = code_steps.get(c, 1) if c == "M": if current: subpaths.append((current, closed)) current = [verts[vi]] closed = False vi += 1 elif c == "Z": closed = True else: current.extend(verts[vi : vi + step]) vi += step if current: subpaths.append((current, closed)) path_x = [] path_y = [] for sub, closed in subpaths: if len(sub) < 2: continue # a closed subpath (Z code) must be closed explicitly since # plotly's lines mode does not close the loop if closed: sub = sub + [sub[0]] sub_x = self._convert_x_dates([v[0] for v in sub]) sub_y = [v[1] for v in sub] if path_x: path_x.append(None) path_y.append(None) path_x.extend(sub_x) path_y.extend(sub_y) if not path_x: continue edgecolor = _per_path(edgecolors, i, "rgba(0,0,0,0)") linewidth = _per_path(linewidths, i, 0) dash = _convert_collection_dash(_per_path(linestyles, i, None)) style = (edgecolor, linewidth, dash) if style != current_style: flush() current_style = style grouped_x = list(path_x) grouped_y = list(path_y) else: grouped_x.append(None) grouped_y.append(None) grouped_x.extend(path_x) grouped_y.extend(path_y) flush() def _draw_filled_path_collection(self, props): """Draw a path collection (e.g. violin plot bodies) as filled polygons.""" facecolors = mpltools.convert_rgba_array(props["styles"]["facecolor"]) edgecolors = mpltools.convert_rgba_array(props["styles"]["edgecolor"]) linewidths = mpltools.convert_linewidth_array(props["styles"]["linewidth"]) for i, (verts, codes) in enumerate(props["paths"]): facecolor = _per_path(facecolors, i, "rgba(0,0,0,0)") edgecolor = _per_path(edgecolors, i, "rgba(0,0,0,0)") linewidth = _per_path(linewidths, i, 0) self.plotly_fig.add_trace( go.Scatter( x=self._convert_x_dates([v[0] for v in verts]), y=[v[1] for v in verts], mode="lines", line=go.scatter.Line( color=_export_color(edgecolor), width=linewidth ), fill="toself", fillcolor=_export_color(facecolor), xaxis="x{0}".format(self.axis_ct), yaxis="y{0}".format(self.axis_ct), ) ) def draw_path(self, **props): """Draw a bar chart path or a matplotlib step patch. This function attempts to sort a given path into a collection of horizontal or vertical bar charts, and draws matplotlib StepPatch artists as step traces. Most of the actual code takes place in functions from mpltools.py. props.keys() -- [ 'data', (a list of vertices for the path) 'coordinates', ('data', 'axes', 'figure', or 'display') 'pathcodes', (code for the path, structure: ['M', 'L', 'Z', etc.]) 'style', (style dict, see below) 'mplobj' (the mpl path object) ] props['style'].keys() -- [ 'alpha', (opacity of path obj) 'edgecolor', 'facecolor', 'edgewidth', 'dasharray', (style for path's enclosing line) 'zorder' (precedence of obj when stacked) ] """ self.msg += " Attempting to draw a path\n" is_bar = mpltools.is_bar(self.current_mpl_ax.containers, **props) if is_bar: self.current_bars += [props] elif isinstance(props["mplobj"], mpatches.StepPatch): self.msg += " Drawing a step path\n" self._draw_step_path(props) else: self.msg += " This path isn't a bar, not drawing\n" warnings.warn( "I found a path object that I don't think is part " "of a bar chart. Ignoring." ) def _draw_step_path(self, props): """Draw a matplotlib StepPatch as a step trace.""" if props["coordinates"] != "data": self.msg += " Step path is not in data coordinates, not drawing\n" return style = props["style"] segments = [] segment_x = [] segment_y = [] for (x0, y0), code in zip(props["data"], props["pathcodes"]): if code == "M" and segment_x: segments.append((segment_x, segment_y)) segment_x = [] segment_y = [] if not segment_x or x0 != segment_x[-1] or y0 != segment_y[-1]: segment_x.append(x0) segment_y.append(y0) if segment_x: segments.append((segment_x, segment_y)) x = [] y = [] for sub_x, sub_y in segments: if x: x.append(None) y.append(None) x.extend(self._convert_x_dates(sub_x)) y.extend(sub_y) if len(x) < 2: self.msg += " Step path has fewer than 2 points, not drawing\n" return label = props["mplobj"].get_label() if not label or (isinstance(label, str) and label.startswith("_")): label = None showlegend = False else: showlegend = None self.plotly_fig.add_trace( go.Scatter( x=x, y=y, mode="lines", name=label, showlegend=showlegend, line=go.scatter.Line( color=_export_color(style["edgecolor"], style["alpha"]), width=style["edgewidth"], dash=mpltools.convert_dash(style["dasharray"]), ), fill="toself" if style["facecolor"] != "none" else None, fillcolor=_export_color(style["facecolor"]), xaxis="x{0}".format(self.axis_ct), yaxis="y{0}".format(self.axis_ct), ) ) def draw_text(self, **props): """Create an annotation dict for a text obj. Currently, plotly uses either 'page' or 'data' to reference annotation locations. These refer to 'display' and 'data', respectively for the 'coordinates' key used in the Exporter. Appropriate measures are taken to transform text locations to reference one of these two options. props.keys() -- [ 'text', (actual content string, not the text obj) 'position', (an x, y pair, not an mpl Bbox) 'coordinates', ('data', 'axes', 'figure', 'display') 'text_type', ('title', 'xlabel', or 'ylabel') 'style', (style dict, see below) 'mplobj' (actual mpl text object) ] props['style'].keys() -- [ 'alpha', (opacity of text) 'fontsize', (size in points of text) 'color', (hex color) 'halign', (horizontal alignment, 'left', 'center', or 'right') 'valign', (vertical alignment, 'baseline', 'center', or 'top') 'rotation', 'zorder', (precedence of text when stacked with other objs) ] """ self.msg += " Attempting to draw an mpl text object\n" if not mpltools.check_corners(props["mplobj"], self.mpl_fig): warnings.warn( "Looks like the annotation(s) you are trying \n" "to draw lies/lay outside the given figure size.\n\n" "Therefore, the resulting Plotly figure may not be \n" "large enough to view the full text. To adjust \n" "the size of the figure, use the 'width' and \n" "'height' keys in the Layout object. Alternatively,\n" "use the Margin object to adjust the figure's margins." ) align = props["mplobj"]._multialignment if not align: align = props["style"]["halign"] # mpl default if "annotations" not in self.plotly_fig["layout"]: self.plotly_fig["layout"]["annotations"] = [] if props["text_type"] == "xlabel": self.msg += " Text object is an xlabel\n" self.draw_xlabel(**props) elif props["text_type"] == "ylabel": self.msg += " Text object is a ylabel\n" self.draw_ylabel(**props) elif props["text_type"] == "title": self.msg += " Text object is a title\n" self.draw_title(**props) else: # just a regular text annotation... self.msg += " Text object is a normal annotation\n" # Skip creating annotations for legend text when using native legend if ( self._processing_legend and self._legend_visible and props["coordinates"] == "axes" ): self.msg += ( " Skipping legend text annotation (using native legend)\n" ) return if props["coordinates"] != "data": self.msg += " Text object isn't linked to 'data' coordinates\n" x_px, y_px = ( props["mplobj"].get_transform().transform(props["position"]) ) x, y = mpltools.display_to_paper(x_px, y_px, self.plotly_fig["layout"]) xref = "paper" yref = "paper" xanchor = props["style"]["halign"] # no difference here! yanchor = mpltools.convert_va(props["style"]["valign"]) else: self.msg += " Text object is linked to 'data' coordinates\n" x, y = props["position"] axis_ct = self.axis_ct xaxis = self.plotly_fig["layout"]["xaxis{0}".format(axis_ct)] yaxis = self.plotly_fig["layout"]["yaxis{0}".format(axis_ct)] if ( xaxis["range"][0] < x < xaxis["range"][1] and yaxis["range"][0] < y < yaxis["range"][1] ): xref = "x{0}".format(self.axis_ct) yref = "y{0}".format(self.axis_ct) else: self.msg += ( " Text object is outside " "plotting area, making 'paper' reference.\n" ) x_px, y_px = ( props["mplobj"].get_transform().transform(props["position"]) ) x, y = mpltools.display_to_paper( x_px, y_px, self.plotly_fig["layout"] ) xref = "paper" yref = "paper" xanchor = props["style"]["halign"] # no difference here! yanchor = mpltools.convert_va(props["style"]["valign"]) annotation = go.layout.Annotation( text=( str(props["text"]) if isinstance(props["text"], str) else props["text"] ), opacity=props["style"]["alpha"], x=x, y=y, xref=xref, yref=yref, align=align, xanchor=xanchor, yanchor=yanchor, showarrow=False, # change this later? font=go.layout.annotation.Font( color=_export_color(props["style"]["color"]), size=props["style"]["fontsize"], ), ) self.plotly_fig["layout"]["annotations"] += (annotation,) self.msg += " Heck, yeah I drew that annotation\n" def draw_title(self, **props): """Add a title to the current subplot in layout dictionary. If there exists more than a single plot in the figure, titles revert to 'page'-referenced annotations. props.keys() -- [ 'text', (actual content string, not the text obj) 'position', (an x, y pair, not an mpl Bbox) 'coordinates', ('data', 'axes', 'figure', 'display') 'text_type', ('title', 'xlabel', or 'ylabel') 'style', (style dict, see below) 'mplobj' (actual mpl text object) ] props['style'].keys() -- [ 'alpha', (opacity of text) 'fontsize', (size in points of text) 'color', (hex color) 'halign', (horizontal alignment, 'left', 'center', or 'right') 'valign', (vertical alignment, 'baseline', 'center', or 'top') 'rotation', 'zorder', (precedence of text when stacked with other objs) ] """ self.msg += " Attempting to draw a title\n" if len(self.mpl_fig.axes) > 1: self.msg += " More than one subplot, adding title as annotation\n" x_px, y_px = props["mplobj"].get_transform().transform(props["position"]) x, y = mpltools.display_to_paper(x_px, y_px, self.plotly_fig["layout"]) annotation = go.layout.Annotation( text=props["text"], font=go.layout.annotation.Font( color=_export_color(props["style"]["color"]), size=props["style"]["fontsize"], ), xref="paper", yref="paper", x=x, y=y, xanchor="center", yanchor="bottom", showarrow=False, # no arrow for a title! ) self.plotly_fig["layout"]["annotations"] += (annotation,) else: self.msg += " Only one subplot found, adding as a plotly title\n" self.plotly_fig["layout"]["title"] = props["text"] title_font = dict( size=props["style"]["fontsize"], color=_export_color(props["style"]["color"]), ) self.plotly_fig["layout"]["title_font"] = title_font def draw_xlabel(self, **props): """Add an xaxis label to the current subplot in layout dictionary. props.keys() -- [ 'text', (actual content string, not the text obj) 'position', (an x, y pair, not an mpl Bbox) 'coordinates', ('data', 'axes', 'figure', 'display') 'text_type', ('title', 'xlabel', or 'ylabel') 'style', (style dict, see below) 'mplobj' (actual mpl text object) ] props['style'].keys() -- [ 'alpha', (opacity of text) 'fontsize', (size in points of text) 'color', (hex color) 'halign', (horizontal alignment, 'left', 'center', or 'right') 'valign', (vertical alignment, 'baseline', 'center', or 'top') 'rotation', 'zorder', (precedence of text when stacked with other objs) ] """ self.msg += " Adding xlabel\n" axis_key = "xaxis{0}".format(self.axis_ct) self.plotly_fig["layout"][axis_key]["title"] = str(props["text"]) title_font = dict( size=props["style"]["fontsize"], color=_export_color(props["style"]["color"]), ) self.plotly_fig["layout"][axis_key]["title_font"] = title_font def draw_ylabel(self, **props): """Add a yaxis label to the current subplot in layout dictionary. props.keys() -- [ 'text', (actual content string, not the text obj) 'position', (an x, y pair, not an mpl Bbox) 'coordinates', ('data', 'axes', 'figure', 'display') 'text_type', ('title', 'xlabel', or 'ylabel') 'style', (style dict, see below) 'mplobj' (actual mpl text object) ] props['style'].keys() -- [ 'alpha', (opacity of text) 'fontsize', (size in points of text) 'color', (hex color) 'halign', (horizontal alignment, 'left', 'center', or 'right') 'valign', (vertical alignment, 'baseline', 'center', or 'top') 'rotation', 'zorder', (precedence of text when stacked with other objs) ] """ self.msg += " Adding ylabel\n" axis_key = "yaxis{0}".format(self.axis_ct) self.plotly_fig["layout"][axis_key]["title"] = props["text"] title_font = dict( size=props["style"]["fontsize"], color=_export_color(props["style"]["color"]), ) self.plotly_fig["layout"][axis_key]["title_font"] = title_font def resize(self): """Revert figure layout to allow plotly to resize. By default, PlotlyRenderer tries its hardest to precisely mimic an mpl figure. However, plotly is pretty good with aesthetics. By running PlotlyRenderer.resize(), layout parameters are deleted. This lets plotly choose them instead of mpl. """ self.msg += "Resizing figure, deleting keys from layout\n" for key in ["width", "height", "autosize", "margin"]: try: del self.plotly_fig["layout"][key] except (KeyError, AttributeError): pass def strip_style(self): self.msg += "Stripping mpl style is no longer supported\n"