@@ -230,13 +230,13 @@ def _ticks(self, data, obj):
230230 self .axis_options .append ("tick align=center" )
231231
232232 # Set each rotation for every label
233- x_tick_rotation_and_horizontal_alignment = _get_label_rotation_and_horizontal_alignment (
233+ x_tick_rotation_and_horizontal_alignment = self . _get_label_rotation_and_horizontal_alignment (
234234 obj , data , "x"
235235 )
236236 if x_tick_rotation_and_horizontal_alignment :
237237 self .axis_options .append (x_tick_rotation_and_horizontal_alignment )
238238
239- y_tick_rotation_and_horizontal_alignment = _get_label_rotation_and_horizontal_alignment (
239+ y_tick_rotation_and_horizontal_alignment = self . _get_label_rotation_and_horizontal_alignment (
240240 obj , data , "y"
241241 )
242242 if y_tick_rotation_and_horizontal_alignment :
@@ -397,86 +397,92 @@ def _subplot(self, obj, data):
397397
398398 return
399399
400+ def _get_label_rotation_and_horizontal_alignment (self , obj , data , x_or_y ):
401+ tick_label_text_width = None
402+ tick_label_text_width_identifier = "{} tick label text width" .format (x_or_y )
403+ if tick_label_text_width_identifier in self .axis_options :
404+ self .axis_options .remove (tick_label_text_width_identifier )
400405
401- def _get_label_rotation_and_horizontal_alignment (obj , data , x_or_y ):
402- tick_label_text_width = None
403- tick_label_text_width_identifier = "{} tick label text width" .format (x_or_y )
404- if tick_label_text_width_identifier in data ["extra axis options" ]:
405- data ["extra axis options" ].remove (tick_label_text_width_identifier )
406+ label_style = ""
406407
407- label_style = ""
408-
409- major_tick_labels = (
410- obj .xaxis .get_majorticklabels ()
411- if x_or_y == "x"
412- else obj .yaxis .get_majorticklabels ()
413- )
414-
415- if not major_tick_labels :
416- return None
408+ major_tick_labels = (
409+ obj .xaxis .get_majorticklabels ()
410+ if x_or_y == "x"
411+ else obj .yaxis .get_majorticklabels ()
412+ )
417413
418- tick_labels_rotation = [ label . get_rotation () for label in major_tick_labels ]
419- tick_labels_rotation_same_value = len ( set ( tick_labels_rotation )) == 1
414+ if not major_tick_labels :
415+ return None
420416
421- tick_labels_horizontal_alignment = [
422- label .get_horizontalalignment () for label in major_tick_labels
423- ]
424- tick_labels_horizontal_alignment_same_value = (
425- len (set (tick_labels_horizontal_alignment )) == 1
426- )
417+ tick_labels_rotation = [label .get_rotation () for label in major_tick_labels ]
418+ tick_labels_rotation_same_value = len (set (tick_labels_rotation )) == 1
427419
428- if tick_labels_rotation_same_value and tick_labels_horizontal_alignment_same_value :
429- values = []
420+ tick_labels_horizontal_alignment = [
421+ label .get_horizontalalignment () for label in major_tick_labels
422+ ]
423+ tick_labels_horizontal_alignment_same_value = (
424+ len (set (tick_labels_horizontal_alignment )) == 1
425+ )
430426
431- if any (tick_labels_rotation ) != 0 :
432- values .append ("rotate={}" .format (tick_labels_rotation [0 ]))
427+ if (
428+ tick_labels_rotation_same_value
429+ and tick_labels_horizontal_alignment_same_value
430+ ):
431+ values = []
433432
434- # Horizontal alignment will be ignored if no 'x/y tick label text width' has
435- # been passed in the 'extra' parameter
436- if tick_label_text_width :
437- values .append ("align={}" .format (tick_labels_horizontal_alignment [0 ]))
438- values .append ("text width={}" .format (tick_label_text_width ))
433+ if any (tick_labels_rotation ) != 0 :
434+ values .append ("rotate={}" .format (tick_labels_rotation [0 ]))
439435
440- if values :
441- label_style = "{}ticklabel style = {{{}}}" .format (x_or_y , "," .join (values ))
442- else :
443- values = []
436+ # Horizontal alignment will be ignored if no 'x/y tick label text width' has
437+ # been passed in the 'extra' parameter
438+ if tick_label_text_width :
439+ values .append ("align={}" .format (tick_labels_horizontal_alignment [0 ]))
440+ values .append ("text width={}" .format (tick_label_text_width ))
444441
445- if tick_labels_rotation_same_value :
446- values .append ("rotate={}" .format (tick_labels_rotation [0 ]))
447- else :
448- values .append (
449- "rotate={{{},0}}[\\ ticknum]" .format (
450- "," .join (str (x ) for x in tick_labels_rotation )
442+ if values :
443+ label_style = "{}ticklabel style = {{{}}}" .format (
444+ x_or_y , "," .join (values )
451445 )
452- )
446+ else :
447+ values = []
453448
454- # Ignore horizontal alignment if no '{x,y} tick label text width' has
455- # been passed in the 'extra' parameter
456- if tick_label_text_width :
457- if tick_labels_horizontal_alignment_same_value :
458- values .append ("align={}" .format (tick_labels_horizontal_alignment [0 ]))
459- values .append ("text width={}" .format (tick_label_text_width ))
449+ if tick_labels_rotation_same_value :
450+ values .append ("rotate={}" .format (tick_labels_rotation [0 ]))
460451 else :
461- for idx , x in enumerate (tick_labels_horizontal_alignment ):
462- label_style += "{}_tick_label_ha_{}/.initial = {}" .format (
463- x_or_y , idx , x
464- )
465-
466452 values .append (
467- "align= \\ pgfkeysvalueof{{/pgfplots/{}_tick_label_ha_ \\ ticknum}} " .format (
468- x_or_y
453+ "rotate={{{},0}}[ \\ ticknum] " .format (
454+ "," . join ( str ( x ) for x in tick_labels_rotation )
469455 )
470456 )
471- values .append ("text width={}" .format (tick_label_text_width ))
472457
473- label_style = (
474- "every {} tick label/.style = {{\n "
475- "{}\n "
476- "}}" .format (x_or_y , ",\n " .join (values ))
477- )
458+ # Ignore horizontal alignment if no '{x,y} tick label text width' has
459+ # been passed in the 'extra' parameter
460+ if tick_label_text_width :
461+ if tick_labels_horizontal_alignment_same_value :
462+ values .append (
463+ "align={}" .format (tick_labels_horizontal_alignment [0 ])
464+ )
465+ values .append ("text width={}" .format (tick_label_text_width ))
466+ else :
467+ for idx , x in enumerate (tick_labels_horizontal_alignment ):
468+ label_style += "{}_tick_label_ha_{}/.initial = {}" .format (
469+ x_or_y , idx , x
470+ )
471+
472+ values .append (
473+ "align=\\ pgfkeysvalueof{{/pgfplots/{}_tick_label_ha_\\ ticknum}}" .format (
474+ x_or_y
475+ )
476+ )
477+ values .append ("text width={}" .format (tick_label_text_width ))
478+
479+ label_style = (
480+ "every {} tick label/.style = {{\n "
481+ "{}\n "
482+ "}}" .format (x_or_y , ",\n " .join (values ))
483+ )
478484
479- return label_style
485+ return label_style
480486
481487
482488def _get_tick_position (obj , axes_obj ):
0 commit comments