Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All plot_ functions in StationPlot should return the collection #3472

Open
guidocioni opened this issue Apr 10, 2024 · 3 comments
Open

All plot_ functions in StationPlot should return the collection #3472

guidocioni opened this issue Apr 10, 2024 · 3 comments
Labels
Type: Feature New functionality

Comments

@guidocioni
Copy link

guidocioni commented Apr 10, 2024

What should we add?

Among the plot functions of the StationPlot module, only some of them return the object containing the collection drawn on the plot. For example, plot_text returns

return text_collection

This is useful, because in case one wants to reuse the same axis/figure for an animation, it is sufficient to just remove the collection and draw again on the same axis. However, some functions (like plot_barb) do not return these objects. Still, this is easy to implement by just adding, e.g.

return self.barbs

after this line

self.barbs = self.ax.barbs(self.x, self.y, u, v, **defaults)

For sure there is a cleaner way to do this, but still this is how I just implemented it on my side (and it works). Having all the functions return the collection drawn on the plot would be also more consistent than having just some of them doing so.

Just for reference here is the code that is handling the animation

_ = plt.figure(1, figsize=(15, 15))
proj = ccrs.Orthographic(central_latitude=40, central_longitude=-100)
ax = plt.axes(projection=proj)

for time in tqdm(out.valid.unique()):
    s = []
    stationplot = StationPlot(ax,
                            subset_filtered['lon'].values,
                            subset_filtered['lat'].values,
                            clip_on=True,
                            transform=ccrs.PlateCarree(),
                            fontsize=8)
    s = stationplot.plot_parameter(
                    'NW', subset_filtered['tmpc'], color='red', zorder=5)
    s2 = stationplot.plot_barb(subset_filtered['u'],
                                      subset_filtered['v'],
                                      length=4.5,
                                      sizes=dict(emptybarb=0.15,
                                                 spacing=0.2,
                                                 height=0.5),
                                      linewidth=0.75,
                                      pivot='middle',
                                      zorder=5)
    plt.savefig(time.strftime('images/stations_%Y%m%d%H%M.png'), bbox_inches='tight', format='png')
    remove_collections([s, oval, s2])

For this to work, stationplot.plot_barb needs to return the barbs collection.

Reference

No response

@guidocioni guidocioni added the Type: Feature New functionality label Apr 10, 2024
@dopplershift
Copy link
Member

As far as I can tell, this is merely an oversight on our part, because we usually intend for plots to do that (as long as it's sensible). Would be happy to merge a PR that adds an appropriate return to plot_barb() and plot_arrow().

@guidocioni
Copy link
Author

As far as I can tell, this is merely an oversight on our part, because we usually intend for plots to do that (as long as it's sensible). Would be happy to merge a PR that adds an appropriate return to plot_barb() and plot_arrow().

Ok, I think I can do that.
Would it be "sensible" to return directly e.g. self.barbs or do you need to copy the object before?

@dopplershift
Copy link
Member

I think just returning directly the object created (and happens to be stored as an attribute on the object) is sensible and consistent with the rest of the class, as well as Matplotlib in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New functionality
Projects
None yet
Development

No branches or pull requests

2 participants