Skip to content

Commit ecfd7a6

Browse files
committed
Improved warning messages
- for newline character convertions - out-of-bounds annotations - warnings have now a custom format
1 parent e50863d commit ecfd7a6

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

plotly/matplotlylib/renderer.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
from . import mpltools
1212
from .. graph_objs import *
1313

14+
# Warning format
15+
def warning_on_one_line(message, category, filename, lineno, file=None, line=None):
16+
return ' %s:%s: %s:\n%s\n' % (filename, lineno, category.__name__, message)
17+
warnings.formatwarning = warning_on_one_line
1418

1519
class PlotlyRenderer(Renderer):
1620
"""A renderer class inheriting from base for rendering mpl plots in plotly.
@@ -499,11 +503,14 @@ def draw_text(self, **props):
499503
"""
500504
self.msg += " Attempting to draw an mpl text object\n"
501505
if not mpltools.check_corners(props['mplobj'], self.mpl_fig):
502-
warnings.warn("\n"
503-
"The annotation you're trying to draw lies outside "
504-
"the given figure size. Therefore, the resulting "
505-
"Plotly figure may not be large enough to view the "
506-
"full text.")
506+
warnings.warn("Looks like the annotation(s) you are trying "
507+
"to draw lies/lay outside the given figure size.\n"
508+
"Therefore, the resulting Plotly figure may not be "
509+
"large enough to view the full text.\n"
510+
"To adjust the size of the figure, use the "
511+
"'width' and 'height' keys in the Layout object. "
512+
"Alternatively, use the Margin object to adjust the "
513+
"figure's margins.")
507514
align = props['mplobj']._multialignment
508515
if not align:
509516
align = props['style']['halign'] # mpl default

plotly/tools.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
from . import utils
1515
from . import exceptions
1616

17+
# Warning format
18+
def warning_on_one_line(message, category, filename, lineno, file=None, line=None):
19+
return ' %s:%s: %s:\n%s\n' % (filename, lineno, category.__name__, message)
20+
warnings.formatwarning = warning_on_one_line
21+
1722
try:
1823
from . import matplotlylib
1924
_matplotlylib_imported = True
@@ -530,7 +535,8 @@ def _replace_newline(obj):
530535
"Plotly uses a subset of HTML escape characters "
531536
"to do things like newline, bold, italics, etc.\n"
532537
"Your newline characters have been converted to "
533-
"'<br>' so they'll show up right!")
538+
"'<br>' so they will show up right on your Plotly "
539+
"figure!")
534540
return s
535541
else:
536542
return obj # we return the actual reference... but DON'T mutate.

0 commit comments

Comments
 (0)