clawpack / visclaw

Clawpack visualization tools
http://www.clawpack.org
BSD 3-Clause "New" or "Revised" License
29 stars 47 forks source link

Add ability to pass norm and alpha values to imshow #297

Closed kbarnhart closed 8 months ago

kbarnhart commented 10 months ago

I've found it useful to be able to specify a norm object to plotitem for 2d_imshow

e.g.,

plotitem.imshow_norm = mpl.colors.LogNorm(0.001, 10, clip=True)

Similarly, I've found it useful to provide an alpha value to make an imshow transparent (e.g., on top of a hillshade).

This PR adds these two features (I'll make an associated PR for the docs shortly).

kbarnhart commented 10 months ago

This is not quite ready to be considered because the norm is not being correctly reflected in the colorbar. Will note here when it is ready for further consideration.

mandli commented 10 months ago

There are some tools to do this but I am not sure that they match what you would like. The norms are always more difficult to work with so it also may be something we would put into an example or FAQ if the implementation is too difficult.

kbarnhart commented 10 months ago

thanks @mandli - If I get it to work I will make an example. I got it to work before... so 🤞 I can get it to work again.

point me towards the tools? If they are sufficient, I'd happily migrate.

rjleveque commented 10 months ago

@kbarnhart: It seems like adding it as you do here should work, and would be a good addition. I'm not sure offhand why it's not reflected in the colorbar.

In looking at the code I see that we allow setting plotitem.kwargs for pcolor plots and these are appended to the plot command, but not for imshow. I'm not sure why not, but that might also be nice to allow for users who want to pass in other arguments we don't explicitly support.

kbarnhart commented 8 months ago

After a bit of digging, I concluded that the reason I thought it was not working was because no colorbar ticks were being draw (thus it was not possible to evaluate if the colorbar norm was working as expected). The following example works as expected for the chile2010 case:

    from matplotlib.colors import SymLogNorm
    #.... many missing lines
    plotitem = plotaxes.new_plotitem(plot_type='2d_imshow')
    plotitem.plot_var = geoplot.surface_or_depth
    plotitem.imshow_cmap = geoplot.tsunami_colormap
    plotitem.imshow_norm = SymLogNorm(vmin=-2, vmax=2, linthresh=0.02)
    plotitem.imshow_alpha=1.0
    plotitem.add_colorbar = True
    plotitem.colorbar_ticks = [-2, -.2, -0.02,0,0.02, 0.2, 2]
    plotitem.colorbar_tick_labels = [-2, -.2, -0.02,0,0.02, 0.2, 2]
    plotitem.amr_celledges_show = [0,0,0]
    plotitem.patchedges_show = 1

@rjleveque and I just discussed whether additional kwargs might ever need to be passed to imshow (besides the two I've just added) and concluded that no other kwargs would make sense to pass. Therefore, I've not added additional kwargs.

I think that this pr and https://github.com/clawpack/doc/pull/227 are ready to be considered.