3

I'm creating an animation using Matplotlib and I'm using a Fill object. I'd like to be able to change the fill data from a function. For other plot types there's usually a set_data() function or set_offsets(). Fill doesn't seem to have one. I would expect to do something like the code below but this doesn't work. Any ideas?

import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(xlim=(-1, 1), ylim=(-1, 1),)
triangle, = ax.fill([0,1,1],[0,0,1])
# I want to change the datapoints later in my code, like this:
triangle.set_data([0,2,2],[0,0,2])
5
  • What type of object is triangle? Did you look up the documentation for that type? I can understand why you would want that code to work, but why do you think it should? Commented May 2, 2014 at 14:49
  • triangle is a fill object, which is a Polygon, which I would think would have a way to change it's data. Commented May 2, 2014 at 17:27
  • Did you look at matplotlib.org/api/artist_api.html#matplotlib.patches.Polygon ? Commented May 2, 2014 at 17:41
  • Yes. Not obvious if there's a way to change the data. Hence my question. I'd be open to another suggestion for manipulating the data to a closed,filled, object. Commented May 2, 2014 at 18:38
  • set_xy? matplotlib.org/api/… Commented May 2, 2014 at 19:21

1 Answer 1

2

This is not particularly well documented, but Polygon objects have a pair of methods get_xy and set_xy. The method get_xy() returns an ndarray with shape (N + 1, 2). The extra point is a repeat of the first point (so that the polygon is closed). Putting the desired data into the expected format (which is what is specified in the documentation to construct a Polygon object):

triangle.set_xy(np.array([[0, 2, 2, 0],[0, 0, 2, 0]]).T)
ax = plt.gca()
ax.set_xlim([0, 3])
ax.set_ylim([0, 3])
plt.draw()

This even lets you change the number of vertices in your patch.

Added issue to clarify the documentation https://github.com/matplotlib/matplotlib/issues/3035.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.