-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Convert axhline, axvline, and axline to layout shapes #5719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
|
|
||
| import warnings | ||
|
|
||
| from matplotlib import transforms | ||
| import plotly.graph_objs as go | ||
| from plotly.matplotlylib.mplexporter import Renderer | ||
| from plotly.matplotlylib import mpltools | ||
|
|
@@ -459,8 +460,18 @@ def draw_marked_line(self, **props): | |
| self.plotly_fig.add_trace(marked_line) | ||
| self.msg += " Heck yeah, I drew that line\n" | ||
| elif props["coordinates"] == "axes": | ||
| # dealing with legend graphical elements | ||
| self.msg += " Using native legend\n" | ||
| if self._processing_legend: | ||
| # dealing with legend graphical elements | ||
| self.msg += " Using native legend\n" | ||
| else: | ||
| # horizontal/vertical reference lines (axhline/axvline) | ||
| self._draw_axes_line(props) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sending lines to |
||
| elif props["coordinates"] == "display" and isinstance( | ||
| props["mplobj"].get_transform(), transforms.BlendedGenericTransform | ||
| ): | ||
| # axhline/axvline: blended axes/data transforms are reported as | ||
| # display coordinates by the exporter | ||
| self._draw_axes_line(props) | ||
| else: | ||
| self.msg += " Line didn't have 'data' coordinates, not drawing\n" | ||
| warnings.warn( | ||
|
|
@@ -469,6 +480,37 @@ def draw_marked_line(self, **props): | |
| "coordinates!" | ||
| ) | ||
|
|
||
| def _draw_axes_line(self, props): | ||
| """Draw an axes-coordinate reference line (axhline/axvline) as a | ||
| layout shape spanning the line's endpoints in data coordinates.""" | ||
| ax = self.current_mpl_ax | ||
| trans = props["mplobj"].get_transform() | ||
| if props["coordinates"] == "display": | ||
| px_points = 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] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll need to add a check to see if these are date values. mpl uses a different date format that plotly, so you'll need to convert the values if they're dates. You could do something like this: if self.x_is_mpl_date:
x0, x1 = self._convert_x_dates([x0, x1]) |
||
| color = mpltools.merge_color_and_opacity( | ||
| props["linestyle"]["color"], props["linestyle"]["alpha"] | ||
| ) | ||
| shape = go.layout.Shape( | ||
| type="line", | ||
| x0=x0, | ||
| y0=y0, | ||
| x1=x1, | ||
| y1=y1, | ||
| xref="x{0}".format(self.axis_ct), | ||
| yref="y{0}".format(self.axis_ct), | ||
|
Comment on lines
+502
to
+503
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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" | ||
|
|
||
| def draw_image(self, **props): | ||
| """Draw image. | ||
|
|
||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your changes have surfaced a bug! Dotted lines can now throw an error because of an issue here.
"circle"isn't a valid dash option. Could you please make the following update?It also looks like
convert_dashcan return invalid values. While you're fixing this issue, you might want to address that one. Valid values are the following:'solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot'