Plotting PatchCollection according to a function
<p>Completely new to the site and to rather new to Python as well, so help and hints appreciated.</p>

<p>I've got some data of (x,y) forming several nearly circle-shaped curves around a center. But for the sake of the example, I just created some (x,y) forming circles.</p>

<p>Now, I want to plot those and fill the space between those Polygons with color according to, let's say some (z) values obtained by a function so that every "ring" has its own shade.</p>

<p>Here is, what I've figured out by now.</p>

<pre><code>import matplotlib.pyplot as plt
import numpy as np
from math import sin, cos
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

r = np.array([0.1, 0.2, 0.3, 0.4, 0.5 ,0.6, 0.7, 0.8, 0.9, 1.0])

fig, ax = plt.subplots(1)
ax.set_xlim([-1.1, 1.1])
ax.set_ylim([-1.1, 1.1])

x=[]
y=[]
patches = []
colors=np.array([0.9,0.8, 0.1, 0.1, 0.1, 0.4, 0.2,0.8,0.1, 0.9])




for radius in r:
    for phi in np.linspace(0, 360, 200, endpoint=True):
        x.append(radius*cos(np.deg2rad(phi)))
        y.append(radius*sin(np.deg2rad(phi)))
    points = np.vstack([x,y]).T
    polygon = Polygon(points,False)
    patches.append(polygon)

p = PatchCollection(patches, cmap="Blues" )
p.set_array(colors)

ax.add_collection(p)

plt.show()
</code></pre>

<p>Giving me: <a href="http://i.stack.imgur.com/S4Hhn.png" rel="nofollow">rings</a></p>

<ol>
<li>I wonder why there is this horizontal line on the right side, this makes me believe I dont understand what my code does.</li>
<li>It has not done the trick as all of the ring-segments have the same color instead of having different shades.</li>
</ol>

<p>I thought the
    p.set_array(colors)
would do the trick as I have found it in the <a href="http://matplotlib.org/api/collections_api.html" rel="nofollow">example</a>
even though I have no idea what <strong>set_array()</strong> does as the documentation
does not give away a lot.</p>

<p>If there is a completely different approach, feel free to tell me anyway.</p>

<p>You need to add the circles from the biggest to the smallest, so they won't run over each other.</p>

<p>I used <a href="http://stackoverflow.com/questions/9215658/plot-a-circle-with-pyplot">plot a circle with pyplot</a>
and <a href="http://matplotlib.org/users/colormaps.html" rel="nofollow">http://matplotlib.org/users/colormaps.html</a></p>

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

r = np.arange(1, 0, -0.1)

fig, ax = plt.subplots(1)
ax.set_xlim([-1.1, 1.1])
ax.set_ylim([-1.1, 1.1])

color_vec = np.array([0.9, 0.8, 0.1, 0.1, 0.1, 0.4, 0.2, 0.8, 0.1, 0.9])
colors = cm.get_cmap("Blues")(color_vec)

for i, radius in enumerate(r):
    circle = plt.Circle((0, 0), radius, color=colors[i])
    ax.add_artist(circle)

plt.show()
</code></pre>

<p>if you need the patches:</p>

<pre><code>import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection

r = np.arange(1, 0, -0.1)

fig, ax = plt.subplots(1)
ax.set_xlim([-1.1, 1.1])
ax.set_ylim([-1.1, 1.1])
patches = []

colors = np.array([0.9, 0.8, 0.1, 0.1, 0.1, 0.4, 0.2, 0.8, 0.1, 0.9])

phi = np.linspace(0, 2*np.pi, 200)
for radius in r:
    x = radius * np.cos(phi)
    y = radius * np.sin(phi)
    points = np.vstack([x, y]).T
    polygon = Polygon(points, False)
    patches.append(polygon)

p = PatchCollection(patches, cmap="Blues")
p.set_array(colors)

ax.add_collection(p)

plt.show()
</code></pre>

python matplotlib plot