Related
I am trying to automatically give each Patch in a PatchCollection a color from a color map like tab20.
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(5,5))
coords = [
(0, 0),
(1, 2),
(1, 3),
(2, 2),
]
patches = [plt.Circle(coords[i], 0.1) for i in range(len(coords))]
patch_collection = PatchCollection(patches, cmap='tab20', match_original=True)
ax.add_collection(patch_collection)
ax.set_xlim(-1, 3)
ax.set_ylim(-1, 4)
plt.axis('equal')
But the above code is drawing each circle using the same color. How can the colors be cycled?
Here I've sampled the tab20 colormap, so that the RGBA array cmap.colors has exactly 20 different entries, then I've assigned this RGBA array to the keyword argument facecolors that every collection accepts.
Not just for cosmetics, I've added a colormap, so that it's possible to recognize the order in which the circles were drawn.
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from numpy.random import rand, seed
seed(20230104)
N = 20
coords = rand(N,2)*[2,1.2]
cmap = plt.get_cmap('tab20', N)
fig, ax = plt.subplots()
patches = [plt.Circle(coord, 0.06) for coord in coords]
# use facecolors=...
collection = PatchCollection(patches, facecolors=cmap.colors[:N-1])
ax.add_collection(collection)
cb = plt.colorbar(plt.cm.ScalarMappable(plt.Normalize(-0.5, N-0.5), cmap))
cb.set_ticks(range(N), labels=('%02d'%(n+1) for n in range(N)))
ax.autoscale(collection)
ax.set_aspect(1)
Overdone Version
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from numpy.random import rand, seed
seed(20230104)
N = 20
coords = rand(N, 2) * [2, 1.2]
cmap = plt.get_cmap("tab20", N)
patches = (plt.Circle(coord, 0.06) for coord in coords)
fig, ax = plt.subplots()
im = ax.add_collection(
PatchCollection(
patches,
facecolors=cmap.colors,
edgecolors="w",
linewidth=2,
cmap=cmap,
norm=plt.Normalize(-0.50, N - 0.50),
)
)
cb = plt.colorbar(
im,
location="bottom",
fraction=0.05,
aspect=50,
drawedges=True,
)
cb.set_ticks(range(N), labels=("%02d" % (n + 1) for n in range(N)))
cb.dividers.set_color(ax._facecolor)
cb.dividers.set_linewidth(3)
ax.autoscale()
ax.set_aspect(1)
This gives each patch its color from a fixed subset of colors in the selected colormap, repeating as necessary:
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
num_col = 3
cmap = plt.cm.tab20
fig, ax = plt.subplots(figsize=(5,5))
coords = [
(0, 0),
(1, 2),
(1, 3),
(2, 2),
]
patches = [plt.Circle(coords[i], 0.1) for i in range(len(coords))]
patch_collection = PatchCollection(patches, facecolor=cmap.colors[0:num_col])
ax.add_collection(patch_collection)
ax.set_xlim(-1, 3)
ax.set_ylim(-1, 4)
plt.axis('equal')
Output:
This gives a random color from the selected colormap by using numpy to generate a list of random numbers, then using the patch objects set_array method:
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(figsize=(5,5))
coords = [
(0, 0),
(1, 2),
(1, 3),
(2, 2),
]
patches = [plt.Circle(coords[i], 0.1) for i in range(len(coords))]
color_vals = np.random.rand(len(patches))
patch_collection = PatchCollection(patches, cmap='tab20', match_original=True)
patch_collection.set_array(color_vals)
ax.add_collection(patch_collection)
ax.set_xlim(-1, 3)
ax.set_ylim(-1, 4)
plt.axis('equal')
Output:
I don't think match_original=True is necessary as you want to change the default color of the original patches. I'm sure there other ways of doing this as well. This SO post was helpful: setting color range in matplotlib patchcollection
I created a sequence of points that I would like to convert into a Patch.
The goal is then to draw the patch on the left side of the y-label (see in Red in the figure), or draw it in any other part of the figure.
Although it can be accomplished with Gridspec, I would like to do it with a Patch.
import matplotlib.pyplot as plt
import numpy as np
plt.figure()
npoints = 100
td = np.linspace(np.pi*3/4, np.pi*5/4, npoints)
xd = np.cos(td)
yd = np.sin(td)
plt.plot(xd,yd)
EDIT1:
I am now able to make a Patch (just need to move it outside the axis):
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.path as mpath
import matplotlib.patches as mpatches
npoints = 100
td = np.linspace(np.pi*3/4, np.pi*5/4, npoints)
xd = np.cos(td)
yd = np.sin(td)
fig, ax = plt.subplots()
ax.axis([-2, 0, -1, 1])
verts=np.c_[xd,yd]
codes = np.ones(len(xd))*2 # Path.LINETO for all points except the first
codes[0] = 1 #Path.MOVETO only for the first point
path1 = mpath.Path(verts, codes)
patch = mpatches.PathPatch(path1, facecolor='none')
ax.add_patch(patch)
The result:
Now, I only need to move it outside the axis, maybe using a translation or scale.
I'm sure the key to do it is somewhere in this Matplotlib Transforms tutorial, more specifically, I am pretty sure the solution is using fig.transFigure.
EDIT 2: Almost there!
In order to use Figure coordinates (that are between [0,1]) I normalized the points that define the path. And instead of using ax.add_patch() that adds a patch to the axis, I use fig.add_artist() that adds the patch to the figure, over the axis.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.path as mpath
import matplotlib.patches as mpatches
#Normalized Data
def normalize(x):
return (x - min(x)) / (max(x) - min(x))
#plt.figure()
npoints = 100
td = np.linspace(np.pi*3/4, np.pi*5/4, npoints)
xd = np.cos(td)
yd = np.sin(td)
#plt.plot(xd,yd)
xd = normalize(xd)
yd = normalize(yd)
fig, ax = plt.subplots()
ax.axis([-2, 2, -1, 1])
verts=np.c_[xd,yd]
codes = np.ones(len(xd))*2 # Path.LINETO for all points except the first
codes[0] = 1 #Path.MOVETO only for the first point
path1 = mpath.Path(verts, codes)
patch1 = mpatches.PathPatch(path1, facecolor='none')
ax.add_patch(patch1)
patch2 = mpatches.PathPatch(path1, fc='none', ec='red', transform=fig.transFigure)
fig.add_artist(patch2)
And the result so far:
Doing this, I just need to scale and translate the patch, maybe using Affine2D.
EDIT 3: Done!
Finally I was able to do it! I used Try and Error in the scale() and translate() parameters as I did not get what coordinate system they were using. However, it would be great to get the exact y center (0.5 in Figure coordinates).
Here is the complete code:
import numpy as np
import matplotlib.path as mpath
import matplotlib.patches as mpatches
#Normalized Data
def normalize(x):
return (x - min(x)) / (max(x) - min(x))
npoints = 100
td = np.linspace(np.pi*3/4, np.pi*5/4, npoints)
xd = np.cos(td)
yd = np.sin(td)
xd = normalize(xd)
yd = normalize(yd)
fig, ax = plt.subplots()
ax.axis([-2, 2, -1, 1])
verts=np.c_[xd,yd]
codes = np.ones(len(xd))*2 # Path.LINETO for all points except the first
codes[0] = 1 #Path.MOVETO only for the first point
path1 = mpath.Path(verts, codes)
patch1 = mpatches.PathPatch(path1, fc='none', ec='green')
ax.add_patch(patch1) #draw inside axis
patch2 = mpatches.PathPatch(path1, fc='none', ec='C0', transform=fig.transFigure)
fig.add_artist(patch2) #this works! Draw on figure
import matplotlib.transforms as mtransforms
tt = fig.transFigure + mtransforms.Affine2D().scale(0.02, 0.8).translate(10,45)
patch3 = mpatches.PathPatch(path1, fc='none', ec='red', transform=tt)
fig.add_artist(patch3)
And the resulting figure:
As #Pedro pointed out, most of this can be found in the tutorial that he linked. However, here is a short answer.
Basically, it's almost as if you're creating a line plot. Just specify the points you want to pass through, add them to a list and that's it.
In this example I want to pass through some points on the plot, then "lift the pen off of the paper" and continue from another point. So we create two lists - one containing the points I want to use and the second list which describes what I want to do with those points. Path.MOVETO will move your "pen" to the given point without drawing a line, so we use this to set our initial startpoint. Path.LINETO creates a straight line starting from your current pen position towards the next line in the list.
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches
# Points you want to "pass through"
pts = [
(0, 0),
(0.2, 0.2),
(0.4, 0.2),
(0.4, 0.4),
(0.4, 0.6),
(0.6, 0.6),
(0.8, 0.8)
]
# What you want to "do" with each point
codes = [
Path.MOVETO, # inital point
Path.LINETO,
Path.LINETO,
Path.LINETO,
Path.MOVETO, # pick up the pen
Path.LINETO,
Path.LINETO
]
# Create path object
# https://matplotlib.org/stable/tutorials/advanced/path_tutorial.html
path = Path(pts, codes)
patch = patches.PathPatch(path, lw='2', color='r', fill=False)
# patch the path to the figure
fig, ax = plt.subplots()
ax.add_patch(patch)
plt.show()
Result of code execution:
I'm trying to clip a cloud of points by several polygons, but I don't know if this is possible with plt.axis.set_clip_path().
Since set_clip_path() requires a Path or a Patch as arguments, how could you create a geometry formed by several Polygons? It would be something like a plt.MultiPolygon(), but that doesn't exist. I've tried to create a matplotlib.PatchCollection with all the Polygons, but that does not work.
Here is the desired goal (from upper to lower figure):
Here is how I'd like the code to look like:
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
import numpy as np
fig, ax = plt.subplots()
points = np.array([np.random.random(100)*400,
np.random.random(100)*100]).T
A = plt.Polygon( np.array([( 0, 0),(50,100),(100, 0)]), color='w', ec='k' )
B = plt.Polygon( np.array([(120 , 0),(170 , 100), (220, 0)]), color='w', ec='k' )
C = plt.Polygon( np.array([(240 , 0),(290 , 100), (340, 0)]), color='w', ec='k' )
[ax.add_patch(i) for i in (A,B,C)]
ax.scatter(points[:,0], points[:,1], zorder=3).set_clip_path([A,B,C])
You can concatenate the vertices and the codes of all polygons, and use them to create a "compound path". Matplotlib's path tutorial contains an example creating a histogram from just one compound path.
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import PathPatch
import numpy as np
points = np.array([np.random.random(100) * 400,
np.random.random(100) * 100]).T
A = plt.Polygon(np.array([(0, 0), (50, 100), (100, 0)]), color='w', ec='k')
B = plt.Polygon(np.array([(120, 0), (170, 100), (220, 0)]), color='w', ec='k')
C = plt.Polygon(np.array([(240, 0), (290, 100), (340, 0)]), color='w', ec='k')
fig, ax = plt.subplots()
all_polys = [A, B, C]
[ax.add_patch(i) for i in all_polys]
vertices = np.concatenate([i.get_path().vertices for i in all_polys])
codes = np.concatenate([i.get_path().codes for i in all_polys])
dots = ax.scatter(points[:, 0], points[:, 1], zorder=3)
dots.set_clip_path(PathPatch(Path(vertices, codes), transform=ax.transData))
plt.show()
If you run the code or check out example plot, you'll see discontinuities between x = (3,4) and (7,8).
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
ranges = [(0, 3), (4, 7), (8, 10)]
block_nums = np.arange(1, 11)
times = np.random.rand(10)
_seg_vals = [(block_nums[start:end + 1], times[start:end + 1]) for start, end in ranges]
line_colors = 'blue', 'orange'
line_segments = [np.column_stack([x, y]) for x, y in _seg_vals]
plt.figure()
ax = plt.axes()
ax.add_collection(LineCollection(line_segments, colors=('blue','orange')))
ax.set_xlim(0, 10)
ax.set_ylim(0, 1)
plt.show()
I could hack together something to define segments between the discontinuities, but I'd prefer a more elegant solution. Is there some way to tell matplotlib to connect the segments at the integer boundaries?
It's doubtful matplotlib supports any way of drawing discontinuities but you can implement both of your LineCollections in a shorter way. assuming capacity of each discontinuity is 1, no domain intervals are required and the only thing you need is a list of discontinuity points:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection
marker_idx = np.array([3, 7])
xy_vals = np.transpose([np.arange(1, 11), np.random.rand(10)])
line_segments = np.split(xy_vals, marker_idx)
discontinuity_idx = np.transpose([marker_idx-1, marker_idx]).flatten()
discontinuity_line_segments = np.split(xy_vals[discontinuity_idx], 2)
plt.figure()
ax = plt.axes()
ax.add_collection(LineCollection(line_segments, colors=('blue','orange')))
ax.add_collection(LineCollection(discontinuity_line_segments, colors=('lightgreen', 'lightgreen')))
ax.set_xlim(0, 10)
ax.set_ylim(0, 1)
plt.show()
In this scenario I am plotting matplotlib.patches.Wedge objects and also buffered shapely.geometry.LineString objects. I need to compute the overlapping areas of these two objects. However, the Wedge is a matplotlib.wedges object and cannot be used with Shapely's .intersection() method.
How can I do this?
Here is some code:
from shapely.geometry import LineString
from matplotlib.patches import Wedge
from matplotlib import pyplot as plt
from descartes.patch import PolygonPatch
width = 5
radius = 1
rich = 1
circle_patch = Wedge((0, 0), radius+3,
0, 360, 3)
fig, ax = plt.subplots()
ax.add_patch(circle_patch)
ax.plot(0, 0, 'xr')
plt.autoscale()
coords = [
[0, 0],
[0, 1],
[0, 2],
[1, 2],
[2, 2]
]
stick = LineString(coords)
stick_patch = PolygonPatch(stick.buffer(0.5))
ax.add_patch(stick_patch)
x, y = stick.xy
ax.plot(x, y, 'r-', zorder=1)
plt.show()
area = stick.buffer(0.5).intersection(circle_patch).area
P.S. It has to be a ring shape, not a circle
Figured it out. There is a ._path.vertices member of the matplotlib.patches class which gives you the array of coordinates of the wedge object which you can then use with Shapely's LinearRing class to create a Shapely object like so:
from shapely.geometry import LineString, LinearRing
from matplotlib.patches import Wedge
width = 5
radius = 1
rich = 1
circle_patch = Wedge((0, 0), radius,
0, 360,)
ring_coords = circle_patch._path.vertices
ring_coords = ring_coords[(ring_coords[:, 0] != 0) & (ring_coords[:, 1] != 0)]
ring = LinearRing(ring_coords)
It does however need manipulation of the coordinate array which I don't think is the most robust method but it will do for me. Also the ring is not entirely smooth but I am sure one could do some smoothing of the coordinate array with some or other Numpy or Scipy function.
EDIT: To create the single wedge line one must remove the width member of the wedge. This can however be re-incorporated later using Shapely's buffer() function.
The simplest solution would be not to work with Matplotlib patches and construct the wedge-polygon with Shapely in the first place:
import matplotlib.pyplot as plt
from descartes.patch import PolygonPatch
from shapely.geometry import LineString, Point
outer_circle = Point(0, 0).buffer(4)
inner_circle = Point(0, 0).buffer(1)
wedge = outer_circle.difference(inner_circle)
stick = LineString([(0, 0), (0, 2), (2, 2)])
buffered_stick = stick.buffer(0.5)
intersection = buffered_stick.intersection(wedge)
wedge_patch = PolygonPatch(wedge)
stick_patch = PolygonPatch(buffered_stick, alpha=0.5, hatch='/')
intersection_patch = PolygonPatch(intersection, alpha=0.5, hatch='.')
fig, ax = plt.subplots()
ax.add_patch(wedge_patch)
ax.add_patch(stick_patch)
ax.add_patch(intersection_patch)
plt.autoscale()
If, for some reason, this is not possible, and you have to work with the Matplotlib's Wedge, then I can think of two ways to get its intersection area with Shapely's polygon. In both of them, I convert the patches to Shapely polygons first. You probably can't get intersection area using only Matplotlib.
1) Using .get_path() method on the Matplotlib's patch from which you can extract vertices as a NumPy array and convert it to a Shapely polygon using asPolygon:
import matplotlib.pyplot as plt
from descartes.patch import PolygonPatch
from matplotlib.patches import Wedge
from shapely.geometry import asPolygon, LineString
wedge_patch = Wedge(center=(0, 0),
r=4,
theta1=0,
theta2=360,
width=3)
stick = LineString([(0, 0), (0, 2), (2, 2)])
buffered_stick = stick.buffer(0.5)
wedge_path = wedge_patch.get_path()
wedge_polygon = asPolygon(wedge_path.vertices).buffer(0)
intersection = buffered_stick.intersection(wedge_polygon)
stick_patch = PolygonPatch(buffered_stick, alpha=0.5, hatch='/')
intersection_patch = PolygonPatch(intersection, alpha=0.5, hatch='.')
fig, ax = plt.subplots()
ax.add_patch(wedge_patch)
ax.add_patch(stick_patch)
ax.add_patch(intersection_patch)
plt.autoscale()
Note the buffer(0) which I apply to the wedge polygon. This is a common trick in Shapely to make a valid polygon out of an invalid. In your answer you do something similar when removing zeros from ring_coords.
2) By accessing Wedge attributes: center, r and width, and using them to recreate a polygon:
import matplotlib.pyplot as plt
from descartes.patch import PolygonPatch
from matplotlib.patches import Wedge
from shapely.geometry import LineString, Point
wedge_patch = Wedge(center=(0, 0),
r=4,
theta1=0,
theta2=360,
width=3)
stick = LineString([(0, 0), (0, 2), (2, 2)])
buffered_stick = stick.buffer(0.5)
outer_circle = Point(wedge_patch.center).buffer(wedge_patch.r)
inner_circle = Point(wedge_patch.center).buffer(wedge_patch.r - wedge_patch.width)
wedge_polygon = outer_circle.difference(inner_circle)
intersection = buffered_stick.intersection(wedge_polygon)
stick_patch = PolygonPatch(buffered_stick, alpha=0.5, hatch='/')
intersection_patch = PolygonPatch(intersection, alpha=0.5, hatch='.')
fig, ax = plt.subplots()
ax.add_patch(wedge_patch)
ax.add_patch(stick_patch)
ax.add_patch(intersection_patch)
plt.autoscale()
All solutions give the same visual output.
And all methods give roughly the same area:
>>> intersection.area
3.3774012986988513 # 1st case
3.3823210603713694 # 2nd case and the original without Matplotlib