How to draw a circular heatmap within a rectangle in Python
<p>Say I have an image and I have a bounding box on a part of the image. How can I draw a circular heatmap within this rectangle?</p>

<p>You need to create a new <code>Axes</code> in the desired position, and use a polar <code>pcolor</code> plot to construct a "heatmap":</p>

<pre><code>import matplotlib.pyplot as plt
import numpy as np

fig,ax1 = plt.subplots()
# plot dummy image
ax1.imshow(np.random.rand(200,200),cmap='viridis')

# create new Axes, position is in figure relative coordinates!
relpos = [0.6, 0.6, 0.2, 0.2]
ax2 = fig.add_axes(relpos, polar=True)
ax2.axis('off')
phi = np.linspace(0,2*np.pi,50)
r = np.linspace(0,1,50)
gradient = np.tile(np.linspace(0,1,r.size)[:,None],phi.size)
ax2.pcolor(gradient,cmap='hot_r')
</code></pre>

<p>The result:</p>

<p><a href="http://i.stack.imgur.com/EIv0E.png" rel="nofollow"><img src="http://i.stack.imgur.com/EIv0E.png" alt="result"></a></p>

<p>The color gradient samples linearly from the colormap, in the above example named <code>hot_r</code>. You can play around with both the colormap and with the transition of the <code>gradient</code> variable, the result will always be radially dependent.</p>

<p>The only thing you need to take care of is to transform your rectangle (given in units which only you can tell) to relative figure units (where (0,0) is the bottom left corner of the figure, and (1,1) is the top left). The axis positioning works in the way which is usual for box-shaped objects: <code>[left,bottom,width,height]</code>.</p>

python matplotlib heatmap