1515import plotly .graph_objs as go
1616
1717
18-
1918# Warning format
20- def warning_on_one_line (message , category , filename , lineno , file = None , line = None ):
21- return '%s:%s: %s:\n \n %s\n \n ' % (filename , lineno , category .__name__ , message )
19+ def warning_on_one_line (msg , category , filename , lineno , file = None , line = None ):
20+ return '%s:%s: %s:\n \n %s\n \n ' % (filename , lineno , category .__name__ , msg )
2221warnings .formatwarning = warning_on_one_line
2322
23+
2424class PlotlyRenderer (Renderer ):
2525 """A renderer class inheriting from base for rendering mpl plots in plotly.
2626
@@ -77,16 +77,18 @@ def open_figure(self, fig, props):
7777 self .msg += "Opening figure\n "
7878 self .mpl_fig = fig
7979 self .plotly_fig ['layout' ] = go .Layout (
80- width = int (props ['figwidth' ]* props ['dpi' ]),
81- height = int (props ['figheight' ]* props ['dpi' ]),
80+ width = int (props ['figwidth' ] * props ['dpi' ]),
81+ height = int (props ['figheight' ] * props ['dpi' ]),
8282 autosize = False ,
8383 hovermode = 'closest' )
8484 self .mpl_x_bounds , self .mpl_y_bounds = mpltools .get_axes_bounds (fig )
8585 margin = go .Margin (
86- l = int (self .mpl_x_bounds [0 ]* self .plotly_fig ['layout' ]['width' ]),
87- r = int ((1 - self .mpl_x_bounds [1 ])* self .plotly_fig ['layout' ]['width' ]),
88- t = int ((1 - self .mpl_y_bounds [1 ])* self .plotly_fig ['layout' ]['height' ]),
89- b = int (self .mpl_y_bounds [0 ]* self .plotly_fig ['layout' ]['height' ]),
86+ l = int (self .mpl_x_bounds [0 ] * self .plotly_fig ['layout' ]['width' ]),
87+ r = int (
88+ (1 - self .mpl_x_bounds [1 ]) * self .plotly_fig ['layout' ]['width' ]),
89+ t = int ((1 - self .mpl_y_bounds [1 ]) * self .plotly_fig ['layout' ][
90+ 'height' ]),
91+ b = int (self .mpl_y_bounds [0 ] * self .plotly_fig ['layout' ]['height' ]),
9092 pad = 0 )
9193 self .plotly_fig ['layout' ]['margin' ] = margin
9294
@@ -154,10 +156,11 @@ def open_axes(self, ax, props):
154156 zeroline = False ,
155157 ticks = 'inside' )
156158 # update defaults with things set in mpl
157- mpl_xaxis , mpl_yaxis = mpltools .prep_xy_axis (ax = ax ,
158- props = props ,
159- x_bounds = self .mpl_x_bounds ,
160- y_bounds = self .mpl_y_bounds )
159+ mpl_xaxis , mpl_yaxis = mpltools .prep_xy_axis (
160+ ax = ax ,
161+ props = props ,
162+ x_bounds = self .mpl_x_bounds ,
163+ y_bounds = self .mpl_y_bounds )
161164 xaxis .update (mpl_xaxis )
162165 yaxis .update (mpl_yaxis )
163166 bottom_spine = mpltools .get_spine_visible (ax , 'bottom' )
@@ -169,7 +172,6 @@ def open_axes(self, ax, props):
169172 xaxis ['showline' ] = bottom_spine
170173 yaxis ['showline' ] = top_spine
171174
172-
173175 # put axes in our figure
174176 self .plotly_fig ['layout' ]['xaxis{0}' .format (self .axis_ct )] = xaxis
175177 self .plotly_fig ['layout' ]['yaxis{0}' .format (self .axis_ct )] = yaxis
@@ -216,15 +218,15 @@ def draw_bar(self, coll):
216218 widths = [bar_props ['x1' ] - bar_props ['x0' ] for bar_props in trace ]
217219 heights = [bar_props ['y1' ] - bar_props ['y0' ] for bar_props in trace ]
218220 vertical = abs (
219- sum (widths [0 ]- widths [iii ] for iii in range (len (widths )))
221+ sum (widths [0 ] - widths [iii ] for iii in range (len (widths )))
220222 ) < tol
221223 horizontal = abs (
222- sum (heights [0 ]- heights [iii ] for iii in range (len (heights )))
224+ sum (heights [0 ] - heights [iii ] for iii in range (len (heights )))
223225 ) < tol
224226 if vertical and horizontal :
225227 # Check for monotonic x. Can't both be true!
226228 x_zeros = [bar_props ['x0' ] for bar_props in trace ]
227- if all ((x_zeros [iii + 1 ] > x_zeros [iii ]
229+ if all ((x_zeros [iii + 1 ] > x_zeros [iii ]
228230 for iii in range (len (x_zeros [:- 1 ])))):
229231 orientation = 'v'
230232 else :
@@ -242,9 +244,9 @@ def draw_bar(self, coll):
242244 # check if we're stacked or not...
243245 for old , new in zip (old_heights , new_heights ):
244246 if abs (old - new ) > tol :
245- self .plotly_fig ['layout' ]['barmode' ]= 'stack'
246- self .plotly_fig ['layout' ]['hovermode' ]= 'x'
247- x = [bar ['x0' ]+ (bar ['x1' ]- bar ['x0' ])/ 2 for bar in trace ]
247+ self .plotly_fig ['layout' ]['barmode' ] = 'stack'
248+ self .plotly_fig ['layout' ]['hovermode' ] = 'x'
249+ x = [bar ['x0' ] + (bar ['x1' ] - bar ['x0' ]) / 2 for bar in trace ]
248250 y = [bar ['y1' ] for bar in trace ]
249251 bar_gap = mpltools .get_bar_gap ([bar ['x0' ] for bar in trace ],
250252 [bar ['x1' ] for bar in trace ])
@@ -257,21 +259,22 @@ def draw_bar(self, coll):
257259 # check if we're stacked or not...
258260 for old , new in zip (old_rights , new_rights ):
259261 if abs (old - new ) > tol :
260- self .plotly_fig ['layout' ]['barmode' ]= 'stack'
261- self .plotly_fig ['layout' ]['hovermode' ]= 'y'
262+ self .plotly_fig ['layout' ]['barmode' ] = 'stack'
263+ self .plotly_fig ['layout' ]['hovermode' ] = 'y'
262264 x = [bar ['x1' ] for bar in trace ]
263- y = [bar ['y0' ]+ (bar ['y1' ]- bar ['y0' ])/ 2 for bar in trace ]
265+ y = [bar ['y0' ] + (bar ['y1' ] - bar ['y0' ]) / 2 for bar in trace ]
264266 bar_gap = mpltools .get_bar_gap ([bar ['y0' ] for bar in trace ],
265267 [bar ['y1' ] for bar in trace ])
266- bar = go .Bar (orientation = orientation ,
267- x = x ,
268- y = y ,
269- xaxis = 'x{0}' .format (self .axis_ct ),
270- yaxis = 'y{0}' .format (self .axis_ct ),
271- opacity = trace [0 ]['alpha' ], # TODO: get all alphas if array?
272- marker = go .Marker (
273- color = trace [0 ]['facecolor' ], # TODO: get all
274- line = go .Line (width = trace [0 ]['edgewidth' ]))) # TODO ditto
268+ bar = go .Bar (
269+ orientation = orientation ,
270+ x = x ,
271+ y = y ,
272+ xaxis = 'x{0}' .format (self .axis_ct ),
273+ yaxis = 'y{0}' .format (self .axis_ct ),
274+ opacity = trace [0 ]['alpha' ], # TODO: get all alphas if array?
275+ marker = go .Marker (
276+ color = trace [0 ]['facecolor' ], # TODO: get all
277+ line = go .Line (width = trace [0 ]['edgewidth' ]))) # TODO ditto
275278 if len (bar ['x' ]) > 1 :
276279 self .msg += " Heck yeah, I drew that bar chart\n "
277280 self .plotly_fig ['data' ] += bar ,
@@ -282,7 +285,6 @@ def draw_bar(self, coll):
282285 warnings .warn ('found box chart data with length <= 1, '
283286 'assuming data redundancy, not plotting.' )
284287
285-
286288 def draw_marked_line (self , ** props ):
287289 """Create a data dict for a line obj.
288290
@@ -346,14 +348,15 @@ def draw_marked_line(self, **props):
346348 )
347349 )
348350 if props ['coordinates' ] == 'data' :
349- marked_line = go .Scatter (mode = mode ,
350- name = props ['label' ],
351- x = [xy_pair [0 ] for xy_pair in props ['data' ]],
352- y = [xy_pair [1 ] for xy_pair in props ['data' ]],
353- xaxis = 'x{0}' .format (self .axis_ct ),
354- yaxis = 'y{0}' .format (self .axis_ct ),
355- line = line ,
356- marker = marker )
351+ marked_line = go .Scatter (
352+ mode = mode ,
353+ name = props ['label' ],
354+ x = [xy_pair [0 ] for xy_pair in props ['data' ]],
355+ y = [xy_pair [1 ] for xy_pair in props ['data' ]],
356+ xaxis = 'x{0}' .format (self .axis_ct ),
357+ yaxis = 'y{0}' .format (self .axis_ct ),
358+ line = line ,
359+ marker = marker )
357360 self .plotly_fig ['data' ] += marked_line ,
358361 self .msg += " Heck yeah, I drew that line\n "
359362 else :
@@ -459,7 +462,6 @@ def draw_path(self, **props):
459462 warnings .warn ("I found a path object that I don't think is part "
460463 "of a bar chart. Ignoring." )
461464
462-
463465 def draw_text (self , ** props ):
464466 """Create an annotation dict for a text obj.
465467
@@ -491,16 +493,17 @@ def draw_text(self, **props):
491493 """
492494 self .msg += " Attempting to draw an mpl text object\n "
493495 if not mpltools .check_corners (props ['mplobj' ], self .mpl_fig ):
494- warnings .warn ("Looks like the annotation(s) you are trying \n "
495- "to draw lies/lay outside the given figure size.\n \n "
496- "Therefore, the resulting Plotly figure may not be \n "
497- "large enough to view the full text. To adjust \n "
498- "the size of the figure, use the 'width' and \n "
499- "'height' keys in the Layout object. Alternatively,\n "
500- "use the Margin object to adjust the figure's margins." )
496+ warnings .warn (
497+ "Looks like the annotation(s) you are trying \n "
498+ "to draw lies/lay outside the given figure size.\n \n "
499+ "Therefore, the resulting Plotly figure may not be \n "
500+ "large enough to view the full text. To adjust \n "
501+ "the size of the figure, use the 'width' and \n "
502+ "'height' keys in the Layout object. Alternatively,\n "
503+ "use the Margin object to adjust the figure's margins." )
501504 align = props ['mplobj' ]._multialignment
502505 if not align :
503- align = props ['style' ]['halign' ] # mpl default
506+ align = props ['style' ]['halign' ] # mpl default
504507 if 'annotations' not in self .plotly_fig ['layout' ]:
505508 self .plotly_fig ['layout' ]['annotations' ] = go .Annotations ()
506509 if props ['text_type' ] == 'xlabel' :
@@ -519,8 +522,9 @@ def draw_text(self, **props):
519522 "coordinates\n "
520523 x_px , y_px = props ['mplobj' ].get_transform ().transform (
521524 props ['position' ])
522- x , y = mpltools .display_to_paper (x_px , y_px ,
523- self .plotly_fig ['layout' ])
525+ x , y = mpltools .display_to_paper (
526+ x_px , y_px , self .plotly_fig ['layout' ]
527+ )
524528 xref = 'paper'
525529 yref = 'paper'
526530 xanchor = props ['style' ]['halign' ] # no difference here!
@@ -602,8 +606,9 @@ def draw_title(self, **props):
602606 self .plotly_fig ['layout' ])
603607 annotation = go .Annotation (
604608 text = props ['text' ],
605- font = go .Font (color = props ['style' ]['color' ],
606- size = props ['style' ]['fontsize' ]
609+ font = go .Font (
610+ color = props ['style' ]['color' ],
611+ size = props ['style' ]['fontsize' ]
607612 ),
608613 xref = 'paper' ,
609614 yref = 'paper' ,
@@ -618,8 +623,9 @@ def draw_title(self, **props):
618623 self .msg += " Only one subplot found, adding as a " \
619624 "plotly title\n "
620625 self .plotly_fig ['layout' ]['title' ] = props ['text' ]
621- titlefont = go .Font (size = props ['style' ]['fontsize' ],
622- color = props ['style' ]['color' ]
626+ titlefont = go .Font (
627+ size = props ['style' ]['fontsize' ],
628+ color = props ['style' ]['color' ]
623629 )
624630 self .plotly_fig ['layout' ]['titlefont' ] = titlefont
625631
@@ -649,8 +655,9 @@ def draw_xlabel(self, **props):
649655 self .msg += " Adding xlabel\n "
650656 axis_key = 'xaxis{0}' .format (self .axis_ct )
651657 self .plotly_fig ['layout' ][axis_key ]['title' ] = props ['text' ]
652- titlefont = go .Font (size = props ['style' ]['fontsize' ],
653- color = props ['style' ]['color' ])
658+ titlefont = go .Font (
659+ size = props ['style' ]['fontsize' ],
660+ color = props ['style' ]['color' ])
654661 self .plotly_fig ['layout' ][axis_key ]['titlefont' ] = titlefont
655662
656663 def draw_ylabel (self , ** props ):
@@ -679,8 +686,9 @@ def draw_ylabel(self, **props):
679686 self .msg += " Adding ylabel\n "
680687 axis_key = 'yaxis{0}' .format (self .axis_ct )
681688 self .plotly_fig ['layout' ][axis_key ]['title' ] = props ['text' ]
682- titlefont = go .Font (size = props ['style' ]['fontsize' ],
683- color = props ['style' ]['color' ])
689+ titlefont = go .Font (
690+ size = props ['style' ]['fontsize' ],
691+ color = props ['style' ]['color' ])
684692 self .plotly_fig ['layout' ][axis_key ]['titlefont' ] = titlefont
685693
686694 def resize (self ):
0 commit comments