-
Notifications
You must be signed in to change notification settings - Fork 224
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
plot 2d ndarrays with grdimage #2846
Comments
Just want to make sure we're on the same page. Are you writing codes like this? import numpy as np
import xarray as xr
import pygmt
array = np.arange(200).reshape((10, 20))
data = xr.DataArray(array)
fig = pygmt.Figure()
fig.grdimage(data, frame=True)
fig.show() |
Not exactly A simple example is a calculation for parameters x and y, which can represent longitudes and latitudes, but they can also represent anything else. import numpy as np
import xarray as xr
import pygmt
x = np.linspace(-118, -117, 100) # longitudes
y = np.linspace(35, 36, 100) # latitudes
X, Y = np.meshgrid(x, y)
array = np.zeros_like(X)
for i in range(X.shape[0]):
for j in range(X.shape[1]):
array[i, j] = f(X[i, j], Y[i, j])# f calculate something based on x and y
data_array = xr.DataArray(array, coords={'x': x, 'y': y}, dims=('y', 'x'))
fig = pygmt.Figure()
fig.grdimage(data_array, frame=True, projection='M10')
fig.show()
|
I think the main point is that it will be nice to be able to plot 2D data without the need to go through raster-like data, whether it be writing it to a raster file or using xarray. |
We have a similar but simpler example (https://www.pygmt.org/dev/gallery/3d_plots/grdview_surface.html), so your script can be simplified to: x = np.linspace(-118, -117, 100) # longitudes
y = np.linspace(35, 36, 100) # latitudes
data = xr.DataArray(f(*np.meshgrid(x, y)), coords={'x': x, 'y': y}, dims=('y', 'x')) |
I think we have three options here:
For reference, GMT.jl also provides a function named I'm inclined to option 1 or 3. |
Actually, there is option 4, i.e., let
I think we should implement #2852. @GenericMappingTools/pygmt-maintainers If no objections, I'll close this feature request. Anyone interested in this feature should track progress in #2852 instead. |
Description of the desired feature
I find myself working with 2D numpy ndarrays often. Then, for visualization purposes, I need to input the data into an xarray DataArray and plot it using pyGMT. I think it would be very convenient if the grdimage function could accept 2D ndarrays along with x and y coordinates. Another option, if PyGMT's grdimage should not change significantly from GMT's grdimage, would be to provide a higher-level function on top of grdimage that does the same. Something that resembles matplotlib's pcolormesh.
Are you willing to help implement and maintain this feature?
Yes
The text was updated successfully, but these errors were encountered: