Michael
Michael

Reputation: 65

Positioning of log-polar plot axis labels in matplotlib

I have trouble positioning the axis labels in this plot. I like to position the top label so that the pipe aligns with the grid, and the right and left labels so that they do not touch the plot.

I tried

    ax.tick_params(axis='both', which='both', pad=15)

but it has no effect. Also,

rcParams

seems to conflict with the log-scale of the plot. As a hack, I tried whitespaces but these are stripped of LaTeX at the beginning and end of each word. Finally, I tried invisible Unicode signs but only got matplotlib to crash.

Your help is greatly appreciated!

log-polar plot enter image description here

import numpy
from matplotlib import pyplot as plt
ax = plt.subplot(111, polar=True)
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
ax.set_xticks(numpy.linspace(0, 2 * 3.14, 4, endpoint=False))
ax.set_theta_direction(-1)
ax.set_theta_offset(3.14 / 2.0)
plt.ylim(0, 2.5)
ax.set_xticklabels([
    r'$100,000\,{\rm yr}|10\,{\rm yr}$',
    r'$100\,{\rm yr}$',
    r'$1000\,{\rm yr}$',
    r'$10,000\,{\rm yr}$'],
    fontsize=16)
plt.show()

Upvotes: 3

Views: 895

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339120

The trick can be to align the textlabels differently according to their position. That is, the left label should be aligned right, the right label left.

For the top label, it makes sense to split it up, such that the first label is left aligned and the last label is right aligned.

import numpy as np
from matplotlib import pyplot as plt

ax = plt.subplot(111, polar=True)
ticks = np.linspace(0, 2 * np.pi, 5, endpoint=True)
ax.set_xticks(ticks)
ax.set_theta_direction(-1)
ax.set_theta_offset(3.14 / 2.0)
plt.ylim(0, 2.5)
ax.set_xticklabels([
    r'$10\,{\rm yr}$',
    r'$100\,{\rm yr}$',
    r'$1000\,{\rm yr}$',
    r'$10,000\,{\rm yr}$', r"$100,000\,{\rm yr}|$"],
    fontsize=16)

aligns = ["left", "left", "center", "right", "right"]
for tick, align in zip(ax.get_xticklabels(), aligns):
    tick.set_ha(align)

plt.show()

enter image description here

Upvotes: 1

Related Questions