Plot polar scatter plot with images as markers, matplotlib: cannot convert NaN to integer
<p>Given a pandas dataframe I have some code that will create a scatter plot and place a specified png at each point:</p>

<pre><code>import matplotlib.pyplot as plt
from matplotlib.image import BboxImage
from matplotlib.transforms import Bbox, TransformedBbox

threshold = 0.05
fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot(111)
bound = 0.2
ax.set_xlim([-bound,bound])
ax.set_ylim([-bound,0.25])
ax.set_axis_bgcolor('white')

for index, row in high_low.iterrows():
    x = row['row1']
    y = row['row2']
    if abs(x) &gt; threshold or abs(y) &gt; threshold:
        im = plt.imread('/path/to/my/image_' + str(index) + '.png')
        bb = Bbox.from_bounds(x,y,.01,.01)  
        bb2 = TransformedBbox(bb,ax.transData)
        bbox_image = BboxImage(bb2,
                            norm = None,
                            origin=None,
                            clip_on=False,
                          alpha=0.75)

        bbox_image.set_data(im)
        ax.add_artist(bbox_image)

ax.grid(True, color="black", linestyle="--")
plt.savefig("myfig.svg", dpi=300, format='svg')
</code></pre>

<p>I want to change this plot to polar coordinates, so I added the following function</p>

<pre><code>def cart2pol(x, y):
    rho = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    return(rho, phi)
</code></pre>

<p>and changed my code to:</p>

<pre><code>ax = fig.add_subplot(111, polar=True) # &lt;- CHANGE HERE
ax.set_axis_bgcolor('white')
for index, row in high_low.iterrows():
    x = row['row1']
    y = row['row2']
    if abs(x) &gt; threshold or abs(y) &gt; threshold:
        rho, phi = cart2pol(x, y) # &lt;- CHANGE HERE
        im = plt.imread('/path/to/my/image_' + str(index) + '.png')
        bb = Bbox.from_bounds(rho,phi,.01,.01)   # &lt;- CHANGE HERE
        bb2 = TransformedBbox(bb,ax.transData)
        bbox_image = BboxImage(bb2,
                            norm = None,
                            origin=None,
                            clip_on=False,
                          alpha=0.75)

        bbox_image.set_data(im)
        ax.add_artist(bbox_image)
ax.grid(True, color="black", linestyle="--")
plt.savefig("myfigPolar.pdf", dpi=300, format='pdf')
</code></pre>

<p>The loop runs:</p>

<pre><code>...
&lt;matplotlib.image.BboxImage object at 0x7f6b805fb390&gt;
&lt;matplotlib.image.BboxImage object at 0x7f6b805fb6d0&gt;
&lt;matplotlib.image.BboxImage object at 0x7f6b805fba10&gt;
...
</code></pre>

<p>but I get the following error when plotting: </p>

<pre><code>&gt;&gt;&gt; plt.savefig("myfigPolar.pdf", dpi=300, format='pdf')
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 561, in savefig
    return fig.savefig(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 2220, in print_figure
    **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1952, in print_pdf
    return pdf.print_pdf(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_pdf.py", line 2352, in print_pdf
    self.figure.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/figure.py", line 1034, 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 2086, 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/image.py", line 1182, in draw
    im = self.make_image(renderer, image_mag)
  File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 1172, in make_image
    im.resize(int(widthDisplay), int(heightDisplay),
ValueError: cannot convert float NaN to integer
</code></pre>

<p>Not sure what I'm doing wrong. Neither <code>rho</code> nor <code>phi</code> are <code>Nan</code>, nor are they super small floats (which I've seen causing this error in other Stackoverflow answers). </p>

<p><strong>Edit</strong>: In response to the comment asking for <code>plt.show()</code></p>

<pre><code>Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 276, in resize
    self.show()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 451, 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 1034, 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 2086, 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/image.py", line 1182, in draw
    im = self.make_image(renderer, image_mag)
  File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 1172, in make_image
    im.resize(int(widthDisplay), int(heightDisplay),
ValueError: cannot convert float NaN to integer
</code></pre>

<p>Min and max values of <code>rho</code> and <code>phi</code></p>

<pre><code>&gt;&gt;&gt; min(rho), max(rho)
(0.10336371748137782, 0.22005286472537541)
&gt;&gt;&gt; min(phi), max(phi)
(-2.9088677009635697, 1.3618299263576035)
</code></pre>

<p>You need to pass positive angles to Bbox, apparently. This should work:</p>

<pre><code>def cart2pol(x, y):
    rho = np.sqrt(x**2 + y**2)
    phi = np.arctan2(y, x)
    if phi &lt; 0:
        phi += 2 * np.pi
    return(rho, phi)
</code></pre>

python matplotlib