0

I have create an ellipse as follows

x=0
y=0
ells=Ellipse(xy=(x,y), #create ellipse
             width =1, 
             height =2,
             edgecolor='b',
             fc='g',
             alpha=0.3,
             zorder=0)

I can't find out how to get reference to those points so I can directly assign them to variable. I read the docs on path, but I didn't see anything there that helped. The reason I need access to the points is that I want to apply a specific tranform to them that is more complex than I can do with the transform attribute of paths. I also tried indexing them like

ells[0] #and got
TypeError: 'Path' object does not support indexing

How do I get the points as an array?

bart cubrich
  • 1,184
  • 1
  • 14
  • 41

1 Answers1

3

You should use Ellipse.get_path(), and work from that Path object. Below you can find a fully working example, based on this post.

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse
from matplotlib.path import Path
from matplotlib.patches import PathPatch

img = plt.imread("image.jpg")

fig, ax = plt.subplots(1)
ax.set_aspect('equal')

ax.imshow(img)

# Create the base ellipse
ellipse = Ellipse((300, 300), width=400, height=100,
                  edgecolor='white', facecolor='none', linewidth=2)

# Get the path
path = ellipse.get_path()
# Get the list of path codes
codes = path.codes
# Get the list of path vertices
vertices = path.vertices.copy()
# Transform the vertices so that they have the correct coordinates
vertices = ellipse.get_patch_transform().transform(vertices)

# Do your transforms here
vertices[0] = vertices[0] - 10

# Create a new Path
modified_path = Path(vertices, codes)
# Create a new PathPatch
modified_ellipse = PathPatch(modified_path, edgecolor='white',
                             facecolor='none', linewidth=2)

# Add the modified ellipse
ax.add_patch(modified_ellipse)

plt.show()

Results before and after modifying the ellipse:

Before

enter image description here

My example only moves one vertice, but you can apply any transform you want. If you change the number of vertices, however, make sure to update codes accordingly.

Renn Kane
  • 493
  • 2
  • 13
  • 2
    It would make much more sense to answer the [original question](https://stackoverflow.com/questions/48670760/draw-an-ellipse-on-a-figure-and-get-coordinates-python) to not have answers spread over different threads. – ImportanceOfBeingErnest Aug 14 '19 at 18:22
  • @ImportanceOfBeingErnest you are absolutely right. Should I post my answer on the original question, now that I have posted it there? – Renn Kane Aug 14 '19 at 18:23
  • My ellipses are rotated, so this didn't actually work. It moved the ellipse down and to the right instead of just down when I did subtraction. Plus, I would rather just have an array of points to work with, which this doesn't do. Good answer generally though! – bart cubrich Aug 14 '19 at 18:28
  • @bartcubrich `vertices` *is* an array of points, ie. an array of coordinates. What do you mean, by "array of points", if not this? – Renn Kane Aug 14 '19 at 18:30
  • 1
    In general, yes, you can always delete this one. Of course if it turns out that this question asks something entirely different (I don't understand the rotation problem either...) you can also edit this one to show the more specific solution. – ImportanceOfBeingErnest Aug 14 '19 at 18:33
  • 1
    I was being stupid. I didn't understand at first. This is actually perfect. I needed `vertices[:,0] = vertices[:,0] - 0.5` tot test what I wanted to test. – bart cubrich Aug 14 '19 at 18:44