# Formatting the datetime string on the x-axis of the PyGMT plot

**URL:** https://forum.generic-mapping-tools.org/t/formatting-the-datetime-string-on-the-x-axis-of-the-pygmt-plot/4422
**Category:** PyGMT Q&A
**Created:** [November 10, 2023, 9:55pm UTC](https://forum.generic-mapping-tools.org/t/formatting-the-datetime-string-on-the-x-axis-of-the-pygmt-plot/4422 "2023-11-10T21:55:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Robson](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@Robson](https://forum.generic-mapping-tools.org/u/Robson)
#### Post date: [November 10, 2023, 9:55pm UTC](https://forum.generic-mapping-tools.org/t/formatting-the-datetime-string-on-the-x-axis-of-the-pygmt-plot/4422/1 "2023-11-10T21:55:12Z")

</div>

I would like to be able to format the datetime string on my x-axis to make it look like in the graph below (generated with Matplotlib):

 ![test_matplotlib](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/b/b8a62a20f91ba92b718cb30789a40ae6d5582fde.png)

However, I haven’t found a way to achieve this with PyGMT. Is it possible by any chance?  
Here is the code I have so far:

```
import pandas as pd
import numpy as np
import pygmt

# Creating pandas dataframe of fake data
start_date = '2022-06-12'
end_date = '2022-06-13 23:55:00'
frequency = '5min'
df = pd.DataFrame(index=pd.date_range(start=start_date, end=end_date, freq=frequency))
df['variable'] = np.random.randint(1, 10, size=576)

# Plot the data
fig = pygmt.Figure()

with pygmt.config(MAP_FRAME_PEN='0.6p',
                  MAP_GRID_PEN_PRIMARY='0.02p,grey,-',
                  MAP_TICK_LENGTH_PRIMARY='4p',
                  MAP_TICK_LENGTH_SECONDARY='4p',
                  FORMAT_CLOCK_MAP="hh:mm"):

    fig.plot(
        projection="X20c/5c",
        region=[df.index[0]-pd.Timedelta('1H'), df.index[-1]+pd.Timedelta('1H'), 0, 10],
        frame=["WS", "g", "sya2f1", "sxa6Hf3H", 'y+lVariable'],
        x=df.index.values,
        y=df['variable'].values,
        style="-10p",
        pen="red",
    )
    
    #fig.savefig('test_pygmt.png', dpi=300)
    fig.show()

```

And below is the generated figure:

 ![test_pygmt](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/f/fb518902bcfad3f527ecf7112e28e47cd83b3e1c.png)

I appreciate your help in advance!

---

<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 11, 2023, 1:06pm UTC](https://forum.generic-mapping-tools.org/t/formatting-the-datetime-string-on-the-x-axis-of-the-pygmt-plot/4422/2 "2023-11-11T13:06:33Z")

</div>

Hello @Robson,

here you can use setting primary ( **p** ) and secondary ( **s** ) axes (for details please see the GMT documentation at [gmt — GMT 6.6.0 documentation](https://docs.generic-mapping-tools.org/latest/gmt.html#axes-settings)). Furthermore there is a section regarding primary and secondary time axes in the PyGMT tutorial “Plotting datetime charts” at [Plotting datetime charts — PyGMT](https://www.pygmt.org/v0.10.0/tutorials/advanced/date_time_charts.html#setting-primary-and-secondary-time-axes).

I modifed your code example. The x-axis of the output figure looks not exactly like the one of your matplot figure, but maybe this is also OK?

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

# Creating pandas dataframe of fake data
start_date = '2022-06-12'
end_date = '2022-06-13 23:55:00'
frequency = '5min'
df = pd.DataFrame(index=pd.date_range(start=start_date, end=end_date, freq=frequency))
df['variable'] = np.random.randint(1, 10, size=576)

# Plot the data
fig = pygmt.Figure()

with pygmt.config(
        MAP_FRAME_PEN='0.6p',
        MAP_GRID_PEN_PRIMARY='0.02p,grey,-',
        MAP_TICK_LENGTH_PRIMARY='4p',
        MAP_TICK_LENGTH_SECONDARY='4p',
        FORMAT_CLOCK_MAP="hh:mm",
        # Set the font for the annotations of the
        # primary axis
        FONT_ANNOT_PRIMARY="10p",
        # secondary axis
        FONT_ANNOT_SECONDARY="10p",
        # OR both primary and secondary axes
        # FONT_ANNOT="10p",
):

    fig.plot(
        projection="X20c/5c",
        region=[df.index[0]-pd.Timedelta('1H'), df.index[-1]+pd.Timedelta('1H'), 0, 10],
        # frame=["WS", "g", "sya2f1", "sxa6Hf3H", 'y+lVariable'],
        # Use primary (p) x-axis for annotations (a) in steps of 1 day (1D)
        # Use secondary (s) x-axis for annotations (a) in steps of 6 hours (6H)
        frame=["WS", "g", "pxa1D", "sxa6H", "ya2f1", "y+lVariable"],
        x=df.index.values,
        y=df['variable'].values,
        style="-10p",
        pen="red",
    )
    
    # fig.savefig('test_pygmt_prim_seco_axes.png', dpi=300)
    fig.show()

```

_Output figure_

 ![test_pygmt_prim_seco_axes](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/e/e793d8a4f1e16b79da27481ecd47288c87531b5a.png)

---

<div class="post-metadata">

### Author: ![Robson](https://avatars.discourse-cdn.com/v4/letter/r/8797f3/32.png) [@Robson](https://forum.generic-mapping-tools.org/u/Robson)
#### Post date: [November 12, 2023, 12:35pm UTC](https://forum.generic-mapping-tools.org/t/formatting-the-datetime-string-on-the-x-axis-of-the-pygmt-plot/4422/3 "2023-11-12T12:35:33Z")

</div>

Thank you very much, @yvonnefroehlich , your response will serve me for now. Unfortunately, it seems that it’s not possible to format the complete date as yyyy-mm-dd hh:mm:ss.
