[Matplotlib-users] How to draw a collection of oriented triangles ?

Joshua Klein mobiusklein at gmail.com
Sat Nov 26 09:53:57 EST 2016


I’ve had great success using PathPatch and AffineTransform for this purpose:

from matplotlib import pyplot as pltfrom matplotlib.path import
Pathfrom matplotlib.transforms import Affine2Dfrom matplotlib.patches
import PathPatch

unit_triangle = Path.unit_regular_polygon(3)
def make_triangle(x, y, rot_deg, color, scale):
    path = Path(unit_triangle.vertices * scale, unit_triangle.codes)
    trans = Affine2D().translate(x, y).rotate_deg_around(x, y, rot_deg)
    t_path = path.transformed(trans)
    patch = PathPatch(t_path, facecolor=color)
    return patch

ax = plt.gca()
for x, y, rot_deg in [(1, 1, 90), (2, 2, 45), (-1, 1, 128)]:
    patch = make_triangle(x, y, rot_deg, "blue", 1)
    ax.add_patch(patch)
# Patches don't automatically change the visible range of the axes
like scatter does.
ax.autoscale()

​

On Sat, Nov 26, 2016 at 9:21 AM, Nicolas P. Rougier <
Nicolas.Rougier at inria.fr> wrote:

>
> I would like to draw several triangles (same size, same color) with
> different individual orientations.
> Scatter plot does not allow to specify individual orientations and I've a
> hard time with PatchCollection.
>
> What would be the easiest way ? Is there an example somewhere around by
> any chance ?
>
>
> Nicolas
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20161126/6bffc926/attachment.html>


More information about the Matplotlib-users mailing list