How to use the log scale on the polar axis in matplotlib
I am trying to create a polar plot with a log scale on the radial axis, but I keep getting the error. Below is a sample code and error. It seems to work fine in Cartesian coordinates, does anyone know what's going on?
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm
bazbins = np.linspace(0, 2*np.pi, 360)
fbins = np.logspace(np.log10(0.05), np.log10(0.5), 101)
theta, r = np.meshgrid(bazbins, fbins)
# Set up plot window
fig, ax = plt.subplots(figsize=(12,9))#, subplot_kw=dict(projection='polar'))
# polar
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_rscale('log')
# carthesia
#ax.set_yscale('log')
# Plot data
#ax.pcolormesh(theta, r, r)
plt.gca().invert_yaxis()
ax.contourf(theta, r, r)
ax.set_ylim((0.0, 0.5))
plt.show()
Exception in Tkinter callback:
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 236, in resize
self.show()
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 239, in draw
FigureCanvasAgg.draw(self)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 421, in draw
self.figure.draw(self.renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 898, in draw
func(*args)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 1997, in draw
a.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1045, in draw
tick.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 239, in draw
self.label1.draw(renderer)
File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/text.py", line 591, in draw
ismath=ismath)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 156, in draw_text
return self.draw_mathtext(gc, x, y, s, prop, angle)
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 145, in draw_mathtext
x = int(x) + ox
ValueError: cannot convert float NaN to integer
source to share
This looks like a bug in matplotlib. You don't need to call the method set_rlim
(or set_ylim
) before set_rscale
(or set_yscale
). Also, you must name set_rlim
either set_ylim
0 or 0.0 as the lower limit. Other values for the lower limit will also fail. The problem occurs with other backends as well (I confirmed the problem for gtkagg and pdf backends).
I've posted a bug report for this issue which can be found here . If this issue affects you, please go to the bug report page and leave a comment so the matplotlib developers know that this issue is important to users.
source to share
There are more problems with current matplotlib and log-polar plots.
For example, try adding a small value to the radius in the matplotlib example for polar plots, then use set_rlim(0)
and set_rscale('log')
to plot it (as suggested in the comments here). All values below 0.1 receive special treatment. This affects the ticks on the axes r
(note the completely unlocal 10e-2 and 10e-3) as well as the graphical data:
The behavior appears to be undocumented. I ended up doing the log-transform by hand (third graph in the above series). For others falling into this chain, here is my code:
import numpy as np
import matplotlib.pyplot as plt
def scatter_polar_mpl(ax, theta, r):
ax.scatter(theta, r)
ax.set_rlim(0)
ax.set_title('polar matplotlib')
def scatter_logpolar_mpl(ax, theta, r):
ax.scatter(theta, r)
ax.set_rlim(0)
ax.set_rscale('log')
ax.set_title('log-polar matplotlib')
def scatter_logpolar(ax, theta, r_, bullseye=0.3, **kwargs):
min10 = np.log10(np.min(r_))
max10 = np.log10(np.max(r_))
r = np.log10(r_) - min10 + bullseye
ax.scatter(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 = np.arange(0, 3.0, 0.01) + 0.001
theta = 2 * np.pi * r
ax = plt.subplots(1, 3, subplot_kw=dict(polar=True))[1].flatten()
scatter_polar_mpl(ax[0], theta, r)
scatter_logpolar_mpl(ax[1], theta, r)
scatter_logpolar(ax[2], theta, r)
plt.show()
source to share
When I take out the comment on the line that sets the plot window, it gets executed for me.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm
bazbins = np.linspace(0, 2*np.pi, 360)
fbins = np.logspace(np.log10(0.05), np.log10(0.5), 101)
theta, r = np.meshgrid(bazbins, fbins)
# Set up plot window
fig, ax = plt.subplots(figsize=(12,9), subplot_kw=dict(projection='polar'))
# polar
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_rscale('log')
plt.gca().invert_yaxis()
ax.contourf(theta, r, r)
ax.set_ylim((0.0, 0.5))
plt.show()
source to share