rafaqz / DimensionalData.jl

Named dimensions and indexing for julia arrays and other data
https://rafaqz.github.io/DimensionalData.jl/stable/
MIT License
281 stars 41 forks source link

Specifying which dims go in which axes when plotting #432

Closed tomchor closed 1 year ago

tomchor commented 1 year ago

I've been exploring this great package and finding out about its features, but one simple thing I could not figure out how to do is to specify which dimensions go in which axes when plotting. For example when I plot this data:

julia> w
28×1×9×315 Raster{Union{Missing, Float32},4} w with dimensions: 
  Dim{:xC} Sampled Float32[-385.7143, -357.14285, …, 357.14285, 385.7143] ForwardOrdered Regular Points,
  Dim{:yC} Sampled Float32[-385.7143] ForwardOrdered Regular Points,
  Dim{:zF} Sampled Float32[-80.0, -56.168503, …, -7.40753, 0.0] ForwardOrdered Irregular Points,
  Ti Sampled Float64[0.0, 322.02037844088295, …, 96606.11353226482, 96665.74792264563] ForwardOrdered Irregular Points
with missingval: missing

from file:
./data/xz1.PPN-NDTEST.nc

julia> heatmap(w[Ti=end, yC=1])

I get this plot

image

It isn't a huge deal but I'd like the dimension zF to be in the vertical axis, and xC to be in the horizontal. How can I achieve this?

rafaqz commented 1 year ago

I guess we could have a dimension order keyword?

How it currectly works is Ti, X, Y and Z dims are organised as you usually expect of dependent/independent dimensions (which varies by plot type). Use these dims and things just work.

If you use custom named dimensions there is no way to know the order.

Maybe we could specify something like plot(A; dependent=:somedim) or plot(A; independent=:someotherdim)?

The reason I haven't added this so far is you can just use permutedims to change the plotted dimensions as you have to with a regular array.

tomchor commented 1 year ago

Ah, I see. For me, as a user, it would be helpful to have a way of controlling that, but I also don't know how much work that'd be. If you were to implement this, in terms of API I'd suggest something like

plot(A; xdim=:somedim)

since I since "dependent" versus "independent" may be a bit less general.

or, in order to keep the current interface for Plots.heatmap:

plot(x=:xF, y=:zF, A) # Or I guess it'd have to be something like the following, no?
plot(x=Dim{:xF}, y=Dim{:zF}, A) 
rafaqz commented 1 year ago

It's trivial to implement. It's just one more bit of interface to document and test.

tomchor commented 1 year ago

It's trivial to implement. It's just one more bit of interface to document and test.

As a user I think it's a useful feature. Unfortunately at this point I don't have the skills to implement it by myself and create a PR, but lmk if I can help you out at least with the documentation part.

tomchor commented 1 year ago

So, I tried to figure out how to implement it, but came up short.

I keep coming back to this feature because most of the time when I'm plotting, I'm doing it once in a script and I don't know a priori the order of the dimensions from the file I'm reading. So using permutedims() would incur some trial-and-error that generally isn't easy for me to do.

Since you said it's trivial to implement, can you implement or at least point me how to do it? I can then document it if you want.

Thanks in advance

rafaqz commented 1 year ago

The usual way in plots is with permutedims or PermutedDimsArray?

permutedims(A, (:xC,:yC))) or whatever you have to the order you want? You can use the dims names or just numbers in a vector.

Did you try that. I don't get how it's any different thats what the plot recipe will do anyway.

(There shouldn't be trial any error using dim names?)

tomchor commented 1 year ago

The usual way in plots is with permutedims or PermutedDimsArray?

permutedims(A, (:xC,:yC))) or whatever you have to the order you want? You can use the dims names or just numbers in a vector.

Did you try that. I don't get how it's any different thats what the plot recipe will do anyway.

(There shouldn't be trial any error using dim names?)

@rafaqz thanks for the reply. I had tried permutedims(A, (:xC,:yC))) and had gotten an error so I assumed it was only implemented with dims as numbers. It turns out I was using an older version of DimensionalData (sorry!). It seems like this indeed works at the moment with the latest version.

Thanks!