@@ -459,69 +459,55 @@ def _get_label_rotation_and_horizontal_alignment(self, obj, data, x_or_y):
459459 if tick_label_text_width_identifier in self .axis_options :
460460 self .axis_options .remove (tick_label_text_width_identifier )
461461
462- tick_labels_rotation = [label .get_rotation () for label in major_tick_labels ]
463- tick_labels_rotation_same_value = len (set (tick_labels_rotation )) == 1
464-
465- tick_labels_horizontal_alignment = [
466- label .get_horizontalalignment () for label in major_tick_labels
467- ]
468- tick_labels_horizontal_alignment_same_value = (
469- len (set (tick_labels_horizontal_alignment )) == 1
470- )
462+ values = []
471463
472- if (
473- tick_labels_rotation_same_value
474- and tick_labels_horizontal_alignment_same_value
475- ):
476- values = []
477-
478- if any (tick_labels_rotation ) != 0 :
464+ tick_labels_rotation = [label .get_rotation () for label in major_tick_labels ]
465+ if len (set (tick_labels_rotation )) == 1 :
466+ if tick_labels_rotation [0 ] != 0 :
479467 values .append (f"rotate={ tick_labels_rotation [0 ]} " )
480-
481- # Horizontal alignment will be ignored if no 'x/y tick label text width' has
482- # been passed in the 'extra' parameter
483- # tick_label_text_width = None
484- # if tick_label_text_width:
485- # values.append(f"align={tick_labels_horizontal_alignment[0]}")
486- # values.append(f"text width={tick_label_text_width}")
487-
488- if values :
489- label_style = "{}ticklabel style = {{{}}}" .format (
490- x_or_y , "," .join (values )
491- )
492468 else :
493- values = []
494-
495- if tick_labels_rotation_same_value :
496- values .append ("rotate={tick_labels_rotation[0]}" )
497- else :
498- values .append (
499- "rotate={{{},0}}[\\ ticknum]" .format (
500- "," .join (str (x ) for x in tick_labels_rotation )
501- )
469+ values .append (
470+ "rotate={{{},0}}[\\ ticknum]" .format (
471+ "," .join (str (x ) for x in tick_labels_rotation )
502472 )
503-
504- # Ignore horizontal alignment if no '{x,y} tick label text width' has been
505- # passed in the 'extra' parameter
506- # if tick_label_text_width:
507- # if tick_labels_horizontal_alignment_same_value:
508- # values.append(f"align={tick_labels_horizontal_alignment[0]}")
509- # values.append(f"text width={tick_label_text_width}")
510- # else:
511- # for idx, x in enumerate(tick_labels_horizontal_alignment):
512- # label_style += f"{x_or_y}_tick_label_ha_{idx}/.initial = {x}"
513-
514- # values.append(
515- # f"align=\\pgfkeysvalueof{{/pgfplots/{x_or_y}_tick_label_ha_\\ticknum}}"
516- # )
517- # values.append(f"text width={tick_label_text_width}")
518-
519- label_style = (
520- "every {} tick label/.style = {{\n "
521- "{}\n "
522- "}}" .format (x_or_y , ",\n " .join (values ))
523473 )
524474
475+ tick_labels_horizontal_alignment = [
476+ label .get_horizontalalignment () for label in major_tick_labels
477+ ]
478+ if len (set (tick_labels_horizontal_alignment )) == 1 :
479+ anchor = {"right" : "east" , "left" : "west" , "center" : "center" }[
480+ tick_labels_horizontal_alignment [0 ]
481+ ]
482+ if not (x_or_y == "x" and anchor == "center" ) and not (
483+ x_or_y == "y" and anchor == "east"
484+ ):
485+ values .append (f"anchor={ anchor } " )
486+
487+ if values :
488+ label_style = "{}ticklabel style={{{}}}" .format (x_or_y , "," .join (values ))
489+
490+ # Ignore horizontal alignment if no '{x,y} tick label text width' has been
491+ # passed in the 'extra' parameter
492+ # if tick_label_text_width:
493+ # if is_tick_label_alignment_identical:
494+ # values.append(f"align={tick_labels_horizontal_alignment[0]}")
495+ # values.append(f"text width={tick_label_text_width}")
496+ # else:
497+ # for idx, x in enumerate(tick_labels_horizontal_alignment):
498+ # label_style += f"{x_or_y}_tick_label_ha_{idx}/.initial = {x}"
499+
500+ # values.append(
501+ # f"align=\\pgfkeysvalueof{{/pgfplots/{x_or_y}_tick_label_ha_\\ticknum}}"
502+ # )
503+ # values.append(f"text width={tick_label_text_width}")
504+
505+ # label_style = (
506+ # "every {} tick label/.style = {{\n"
507+ # "{}\n"
508+ # "}}".format(x_or_y, ",\n".join(values))
509+ # )
510+
525511 return label_style
526512
527513
@@ -606,9 +592,10 @@ def _get_ticks(data, xy, ticks, ticklabels):
606592 axis_options .append (f"{ xy } tick={ val } " )
607593
608594 if is_label_required :
609- axis_options .append (
610- "{}ticklabels={{{}}}" .format (xy , "," .join (pgfplots_ticklabels ))
611- )
595+ length = sum (len (label ) for label in pgfplots_ticklabels )
596+ sep = ("" , "," , "" ) if length < 75 else ("\n " , ",\n " , "\n " )
597+ string = sep [1 ].join (pgfplots_ticklabels )
598+ axis_options .append (f"{ xy } ticklabels={{{ sep [0 ]} { string } { sep [2 ]} }}" )
612599 return axis_options
613600
614601
0 commit comments