# Plot a hovmoller diagram

**URL:** <https://forum.generic-mapping-tools.org/t/plot-a-hovmoller-diagram/3443>\
**Category:** PyGMT Q&A\
**Created:** [November 14, 2022, 10:27am UTC](https://forum.generic-mapping-tools.org/t/plot-a-hovmoller-diagram/3443 "2022-11-14T10:27:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shereef](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@shereef](https://forum.generic-mapping-tools.org/u/shereef)\
**Post date:** [November 14, 2022, 10:27am UTC](https://forum.generic-mapping-tools.org/t/plot-a-hovmoller-diagram/3443/1 "2022-11-14T10:27:05Z")

</div>

"I want to plot a hovmoller diagram using a depth-time series netcdf file. In plot, time axis will be in X axis and depth axis will be in Y axis (I am giving an example plot here). Is it possible in pygmt..?

 ![Screenshot from 2022-11-14 15-16-27](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/e/e238f572a4a657552c51f9cec106403b88d7a195.png)

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [November 14, 2022, 2:55pm UTC](https://forum.generic-mapping-tools.org/t/plot-a-hovmoller-diagram/3443/2 "2022-11-14T14:55:38Z")

</div>

Yes it is.  
You want to use `grdimage` with a cartesian projection. You can specify « T » (for absolute time) to the x-axis.

---

<div class="post-metadata">

**Author:** ![shereef](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@shereef](https://forum.generic-mapping-tools.org/u/shereef)\
**Post date:** [November 15, 2022, 5:53am UTC](https://forum.generic-mapping-tools.org/t/plot-a-hovmoller-diagram/3443/3 "2022-11-15T05:53:13Z")

</div>

I appreciate the replay. I am new to pygmt. If you have any example code, it would be very appreciated if you could offer it.

---

<div class="post-metadata">

**Author:** ![PlanetGus](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/planetgus/32/303_2.png) [@PlanetGus](https://forum.generic-mapping-tools.org/u/PlanetGus)\
**Post date:** [November 15, 2022, 9:47am UTC](https://forum.generic-mapping-tools.org/t/plot-a-hovmoller-diagram/3443/4 "2022-11-15T09:47:14Z")

</div>

I don’t use the python wrapper myself,  
But you have the doc :

> **[pygmt.Figure.grdimage — PyGMT](https://www.pygmt.org/latest/api/generated/pygmt.Figure.grdimage.html?highlight=grdimage#pygmt.Figure.grdimage)**

And the examples from the gallery.

I believe the code looks somehow like this :  
ˋ`ˋ fig_ref.grdimage("myfile.nc", projection="X20T/10c", cmap="vik") ˋ`ˋ

---

<div class="post-metadata">

**Author:** ![shereef](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@shereef](https://forum.generic-mapping-tools.org/u/shereef)\
**Post date:** [November 15, 2022, 8:43pm UTC](https://forum.generic-mapping-tools.org/t/plot-a-hovmoller-diagram/3443/5 "2022-11-15T20:43:40Z")

</div>

Thank you; the code executed well; the only issue is that the time axis I plotted using the time axis as a absolute value . Can we use anything as an axis for time?Here, I’m Including the Sample Code.

```python
import xarray as xr
import numpy as np
import pandas as pd
import pygmt

# creating a sample data poits
ds = xr.Dataset(
    {"v": (("depth", "time"), np.random.rand(4, 25))},
    coords={
        "depth": [10, 20, 30, 40],
        "time": pd.date_range("2015-01-01", periods=25),
    },
)
ds=ds.v
t=ds.time

#converting time as absalute value
t = (t - t.min()) / np.timedelta64(1,'D')
t=t.data
ds['time']=t

#plot
fig = pygmt.Figure()
fig.grdimage(
    ds,
    region=[t.min(), t.max(), 0, 40],
    projection='X3i/1i',
    cmap='inferno',
    frame=True
    )
fig.show()

```

---

<div class="post-metadata">

**Author:** ![yvonnefroehlich](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/yvonnefroehlich/32/5064_2.png) [@yvonnefroehlich](https://forum.generic-mapping-tools.org/u/yvonnefroehlich)\
**Post date:** [November 15, 2022, 9:19pm UTC](https://forum.generic-mapping-tools.org/t/plot-a-hovmoller-diagram/3443/6 "2022-11-15T21:19:33Z")

</div>

Hello @shereef,

thanks for providing your script! Can you please format it as code using three backticks (```) in the lines before and after the block with the script:

````auto
```
your script formated as code
```

````

Then people can simply copy/past your code 😉.

---

<div class="post-metadata">

**Author:** ![shereef](https://avatars.discourse-cdn.com/v4/letter/s/0ea827/32.png) [@shereef](https://forum.generic-mapping-tools.org/u/shereef)\
**Post date:** [November 15, 2022, 9:39pm UTC](https://forum.generic-mapping-tools.org/t/plot-a-hovmoller-diagram/3443/7 "2022-11-15T21:39:10Z")

</div>

Thanks again. I’m encountering this error while running the code. “ufunc ‘add’ cannot use operands with types dtype(‘float64’) and dtype(’\<m8[ns]’)”.

```auto
import xarray as xr
import numpy as np
import pandas as pd
import pygmt
import datetime

# creating a sample data poits
ds = xr.Dataset(
    {"v": (("depth", "time"), np.random.rand(4, 25))},
    coords={
        "depth": [10, 20, 30, 40],
        "time": pd.date_range("2015-01-01", periods=25),
    },
)
fig = pygmt.Figure()
fig.grdimage(
    ds.v,
    region=[[datetime.date(2015,1,1), datetime.date(2015, 1, 25),10,40]],
    projection='X3i/1i',
    cmap='inferno',
    frame=True
    )
fig.show()

```
