Skip to content

Commit 5ba5ac9

Browse files
committed
properly handle horizontal alignment
1 parent 11d5ac4 commit 5ba5ac9

File tree

1 file changed

+45
-60
lines changed

1 file changed

+45
-60
lines changed

tikzplotlib/_axes.py

Lines changed: 45 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -459,69 +459,53 @@ 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 anchor != "center":
483+
values.append(f"anchor={anchor}")
484+
485+
if values:
486+
label_style = "{}ticklabel style={{{}}}".format(x_or_y, ",".join(values))
487+
488+
# Ignore horizontal alignment if no '{x,y} tick label text width' has been
489+
# passed in the 'extra' parameter
490+
# if tick_label_text_width:
491+
# if is_tick_label_alignment_identical:
492+
# values.append(f"align={tick_labels_horizontal_alignment[0]}")
493+
# values.append(f"text width={tick_label_text_width}")
494+
# else:
495+
# for idx, x in enumerate(tick_labels_horizontal_alignment):
496+
# label_style += f"{x_or_y}_tick_label_ha_{idx}/.initial = {x}"
497+
498+
# values.append(
499+
# f"align=\\pgfkeysvalueof{{/pgfplots/{x_or_y}_tick_label_ha_\\ticknum}}"
500+
# )
501+
# values.append(f"text width={tick_label_text_width}")
502+
503+
# label_style = (
504+
# "every {} tick label/.style = {{\n"
505+
# "{}\n"
506+
# "}}".format(x_or_y, ",\n".join(values))
507+
# )
508+
525509
return label_style
526510

527511

@@ -606,9 +590,10 @@ def _get_ticks(data, xy, ticks, ticklabels):
606590
axis_options.append(f"{xy}tick={val}")
607591

608592
if is_label_required:
609-
axis_options.append(
610-
"{}ticklabels={{{}}}".format(xy, ",".join(pgfplots_ticklabels))
611-
)
593+
length = sum(len(label) for label in pgfplots_ticklabels)
594+
sep = ("", ",", "") if length < 75 else ("\n ", ",\n ", "\n")
595+
string = sep[1].join(pgfplots_ticklabels)
596+
axis_options.append(f"{xy}ticklabels={{{sep[0]}{string}{sep[2]}}}")
612597
return axis_options
613598

614599

0 commit comments

Comments
 (0)