Autoscaling after adding patches (and annotations)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Autoscaling after adding patches (and annotations)

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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
jni
Reply | Threaded
Open this post in threaded view
|

Re: Autoscaling after adding patches (and annotations)

jni

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

Have you tried `ax.autoscale_view`?


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Autoscaling after adding patches (and annotations)

Neal Becker
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 <[hidden email]> wrote:
What's an 
easy way to get the axes to scale correctly in this example?


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Autoscaling after adding patches (and annotations)

Elan Ernest
In reply to this post by Neal Becker
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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
>
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Autoscaling after adding patches (and annotations)

Neal Becker
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 <[hidden email]> 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
> [hidden email]
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
>
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users