コード例 #1
0
ファイル: renderer.py プロジェクト: tatamora/Kmeadias
    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'] = go.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"
            if props['coordinates'] is not '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.Annotation(
                text=(str(props['text']) if
                      isinstance(props['text'], six.string_types) 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=props['style']['color'],
                    size=props['style']['fontsize']
                )
            )
            self.plotly_fig['layout']['annotations'] += annotation,
            self.msg += "    Heck, yeah I drew that annotation\n"
コード例 #2
0
    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'] = go.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"
            if props['coordinates'] is not '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.Annotation(
                text=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.Font(
                    color=props['style']['color'],
                    size=props['style']['fontsize']
                )
            )
            self.plotly_fig['layout']['annotations'] += annotation,
            self.msg += "    Heck, yeah I drew that annotation\n"