Completely new to the site and to rather new to Python as well, so help and hints appreciated.
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.
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.
Here is, what I've figured out by now.
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()
Giving me: rings
- I wonder why there is this horizontal line on the right side, this makes me believe I dont understand what my code does.
- It has not done the trick as all of the ring-segments have the same color instead of having different shades.
I thought the p.set_array(colors) would do the trick as I have found it in the example even though I have no idea what set_array() does as the documentation does not give away a lot.
If there is a completely different approach, feel free to tell me anyway.