Skip to content

Instantly share code, notes, and snippets.

@temporaer
Last active April 16, 2021 12:21
Show Gist options
  • Save temporaer/6755266 to your computer and use it in GitHub Desktop.
Save temporaer/6755266 to your computer and use it in GitHub Desktop.
matplotlib log-polar plots seem to be quite buggy at the time of writing. They crash if rlim is not set before rscale, and they seem to have a magic 0.1 in there somewhere, where all values less than 0.1 are interpreted as 'negative' (whatever that means in a log-polar plot). This gist creates some spiral data, a polar and a log-polar plot using…
import numpy as np
import matplotlib.pyplot as plt
def plot_polar_mpl(ax, theta, r):
ax.plot(theta, r)
ax.set_rlim(0)
ax.set_title('polar matplotlib')
def plot_logpolar_mpl(ax, theta, r):
ax.plot(theta, r)
ax.set_rlim(0)
ax.set_rscale('log')
ax.set_title('log-polar matplotlib')
def plot_logpolar(ax, theta, r_, bullseye=None, **kwargs):
min10 = np.log10(np.min(r_))
max10 = np.log10(np.max(r_))
if bullseye is None:
bullseye = min10 - np.log10(0.5 * np.min(r_))
r = np.log10(r_) - min10 + bullseye
ax.plot(theta, r, **kwargs)
l = np.arange(np.floor(min10), max10)
ax.set_rticks(l - min10 + bullseye)
ax.set_yticklabels(["1e%d" % x for x in l])
ax.set_rlim(0, max10 - min10 + bullseye)
ax.set_title('log-polar manual')
return ax
r = 10 ** np.arange(-3, 1.0, 0.0001)
theta = 2 * np.pi * np.log10(r)
ax = plt.subplots(1, 3, subplot_kw=dict(polar=True))[1].flatten()
plot_polar_mpl(ax[0], theta, r)
plot_logpolar_mpl(ax[1], theta, r)
plot_logpolar(ax[2], theta, r)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment