Autoscaling after adding patches (and annotations)

In this code, I add polygons and text annotations. The result is plotted on
axes scaled [0..1], not autoscaled to match the data. If I add e.g., a
scatterplot of the polygon centers, the result in scaled fine. What's an
easy way to get the axes to scale correctly in this example?

for i, center in enumerate(centers):
    circle = mpl.patches.RegularPolygon ((center.real, center.imag), 6, 1,
fill=False, clip_on=False, linestyle='-', orientation=30*pi/180)
    ax.add_artist (circle)
    ax.annotate (beam_colors[i], xy=(center.real, center.imag),
textcoords='offset points', xytext=(-3,3))
plt.show()

What's an
easy way to get the axes to scale correctly in this example?

Have you tried `ax.autoscale_view`?

https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.autoscale_view.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190322/a6f109cd/attachment-0001.html>

Yes, that doesn't work. It just autoscales to a tiny portion of the data.

In my example, I generate the xy coordinates for the centers of hexagons
that tile a square area. The code adds the hexagons using add_artist.
Also, the centers of the hexagons are annotated. Neither of these
operations causes the axes to scale correctly. If I add a step to plot the
centers of the hexagons, using e.g., scatter, then the axes are scaled
correctly.
For now, I'm just adding the scatter plot with alpha=0 to get the scaling
correct.
But I still don't know a less klugey way to do it.

···

On Fri, Mar 22, 2019 at 12:43 AM Juan Nunez-Iglesias <jni.soma at gmail.com> wrote:

What's an
easy way to get the axes to scale correctly in this example?

Have you tried `ax.autoscale_view`?

https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.autoscale_view.html

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190322/4fb235ae/attachment.html>

To add a patch to an axes use `ax.add_patch()`.

If you have several patches, consider using a collection, as shown in
https://matplotlib.org/gallery/shapes_and_collections/collections.html
or
https://matplotlib.org/gallery/shapes_and_collections/patch_collection.html

···

Am 21.03.2019 um 15:44 schrieb Neal Becker:

In this code, I add polygons and text annotations. The result is plotted on
axes scaled [0..1], not autoscaled to match the data. If I add e.g., a
scatterplot of the polygon centers, the result in scaled fine. What's an
easy way to get the axes to scale correctly in this example?

for i, center in enumerate(centers):
     circle = mpl.patches.RegularPolygon ((center.real, center.imag), 6, 1,
fill=False, clip_on=False, linestyle='-', orientation=30*pi/180)
     ax.add_artist (circle)
     ax.annotate (beam_colors[i], xy=(center.real, center.imag),
textcoords='offset points', xytext=(-3,3))
plt.show()

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
https://mail.python.org/mailman/listinfo/matplotlib-users

OK, using ax.add_patch() together with ax.autoscale_view() (after adding
all patches) gives the desired result.
Thanks!

···

On Fri, Mar 22, 2019 at 10:04 AM Elan Ernest <elch.rz at ruetz-online.de> wrote:

To add a patch to an axes use `ax.add_patch()`.

If you have several patches, consider using a collection, as shown in
https://matplotlib.org/gallery/shapes_and_collections/collections.html
or
https://matplotlib.org/gallery/shapes_and_collections/patch_collection.html

Am 21.03.2019 um 15:44 schrieb Neal Becker:
> In this code, I add polygons and text annotations. The result is
plotted on
> axes scaled [0..1], not autoscaled to match the data. If I add e.g., a
> scatterplot of the polygon centers, the result in scaled fine. What's an
> easy way to get the axes to scale correctly in this example?
>
> for i, center in enumerate(centers):
> circle = mpl.patches.RegularPolygon ((center.real, center.imag), 6,
1,
> fill=False, clip_on=False, linestyle='-', orientation=30*pi/180)
> ax.add_artist (circle)
> ax.annotate (beam_colors[i], xy=(center.real, center.imag),
> textcoords='offset points', xytext=(-3,3))
> plt.show()
>
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
>
_______________________________________________
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/20190322/926ea26e/attachment.html>