MCPcopy Create free account
hub / github.com/spotify/chartify / set_legend_location

Method set_legend_location

chartify/_core/chart.py:291–345  ·  view source on GitHub ↗

Set the legend location. Args: location (str or tuple): Legend location. One of: - Outside of the chart: 'outside_top', 'outside_bottom', 'outside_right' - Within the chart area: 'top_left', 'top_center', 'top_right', '

(self, location, orientation="horizontal")

Source from the content-addressed store, hash-verified

289 return self.figure.legend[0].location
290
291 def set_legend_location(self, location, orientation="horizontal"):
292 """Set the legend location.
293
294 Args:
295 location (str or tuple): Legend location. One of:
296 - Outside of the chart: 'outside_top', 'outside_bottom',
297 'outside_right'
298 - Within the chart area: 'top_left', 'top_center',
299 'top_right', 'center_left', 'center', 'center_right',
300 'bottom_left', 'bottom_center', 'bottom_right'
301 - Coordinates: Tuple(Float, Float)
302 - None: Removes the legend.
303 orientation (str): 'horizontal' or 'vertical'
304
305 Returns:
306 Current chart object
307 """
308
309 def add_outside_legend(legend_location, layout_location):
310 self.figure.legend.location = legend_location
311 if not self.figure.legend:
312 warnings.warn(
313 """
314 Legend location will not apply.
315 Set the legend after plotting data.
316 """,
317 UserWarning,
318 )
319 return self
320 new_legend = self.figure.legend[0]
321 new_legend.orientation = orientation
322 self.figure.add_layout(new_legend, layout_location)
323
324 if location == "outside_top":
325 add_outside_legend("top_left", "above")
326 # Re-render the subtitle so that it appears over the legend.
327 subtitle_index = self.figure.renderers.index(self._subtitle_glyph)
328 self.figure.renderers.pop(subtitle_index)
329 self._subtitle_glyph = self._add_subtitle_to_figure(self._subtitle_glyph.text)
330 elif location == "outside_bottom":
331 add_outside_legend("bottom_center", "below")
332 elif location == "outside_right":
333 add_outside_legend("top_left", "right")
334 elif location is None:
335 self.figure.legend.visible = False
336 else:
337 self.figure.legend.location = location
338 self.figure.legend.orientation = orientation
339
340 vertical = self.axes._vertical
341 # Reverse the legend order
342 if self._reverse_vertical_legend:
343 if orientation == "vertical" and vertical:
344 self.figure.legend[0].items = list(reversed(self.figure.legend[0].items))
345 return self
346
347 def show(self, format="html"):
348 """Show the chart.

Calls 1