Here is an example (adapted the code from here) import matplotlib If rotation_mode="anchor", the text is directly rotated about the anchor point ( ha="right"). In this mode, the bounding box is not exactly the top right corner of the text even if ha="right" is used. So if rotation_mode is not specified, the text is first rotated and then aligned. If "anchor", then alignment occurs before rotation. Then aligned according to their horizontal and vertical If None or "default", the text will be first rotated, I looked into the documentation of the _rotation_mode ( link): set_rotation_mode(self, m) Here is a good resource that provides several options. Or loop over them to set the respective properties, for label in ax.get_xticklabels():Īn example would be import numpy as np np.ed(42) In those cases either use plt.setp to set the rotation and alignment of existing labels, plt.setp(ax.get_xticklabels(), ha="right", rotation=45) This will in general let the labels and tick positions become out of sync, because set_xticklabels sets the formatter of the axes to a FixedFormatter, while the locator stays the automatic AutoLocator, or any other automatic locator. If instead the labels are shown automatically, one should not use set_xticklabels. via plt.xticks or via ax.set_xticks) or if a categorical plot is used. The above can be used if the ticks are specified manually like in the question (e.g. ax.set_xticklabels(xticklabels, rotation = 45, va="center", position=(0,-0.28)) This leads to an equal spacing but required to further adjust their vertical position with respect to the axis. Use the horizontalalignment or ha argument for that and set it to "right": ax.set_xticklabels(xticklabels, rotation = 45, ha="right")Īn alternative can be to keep the ticklabels horizontally centered, but also center them vertically. However you might want to align them such the the upper right corner is the reference for their positioning below the tick. Since you'd always want the ticklabels to link to their tickmarks, changing the spacing is not really an option. Their bounding boxes are unequal in width and might even overlap, which makes them look unequally spaced. The labels are centered at the tickmark position.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |