One thing I find with the PyGMT/GMT documentation is that the explanations are often lacking.
Yes, you are right. Currently, the documentation regarding the frame
parameter of Figure.colorbar
is quite sparse. Thanks for your feedback
! Thus, I submitted a PR to expand the current gallery example “Colorbar” explaning the frame
parameter regarding annotation, ticks, and labels.
Just a follow up (not essential) - Is there a way to overwrite the annotations on the colorbars? Right now, I have converted my data from datetime into matplolib date numbers, so the colorbar shows the date number, rather than an actual date. If I wanted to change the datenum from 19340 to 14 Dec. 2022, is that possible?
Actually, it is possible to set up a colormap for dates directly based on datetime.datetime
objects. Then you can color-code your data based on the date and also the colorbar has the date as annotations.
import datetime
import pandas as pd
import pygmt
# -----------------------------------------------------------------------------
# General stuff
# -----------------------------------------------------------------------------
# Time window of one year
date_start = datetime.datetime(2016, 1, 1)
date_end = datetime.datetime(2016, 12, 31)
# Random Test data
dates = [
datetime.datetime(2016, 2, 1),
datetime.datetime(2016, 3, 1),
datetime.datetime(2016, 5, 1),
datetime.datetime(2016, 8, 1),
datetime.datetime(2016, 11, 1),
]
y_values = [1, 3, 5, 2, 4]
# Create pandas DataFrame
dict_dates = {"date": dates, "value": y_values, "color": dates}
df_dates = pd.DataFrame(dict_dates)
# -----------------------------------------------------------------------------
# Create time chart
# -----------------------------------------------------------------------------
fig = pygmt.Figure()
# Basic map
fig.basemap(
projection="X10c/5c",
region=[date_start, date_end, 0, 6],
# upper-case O (not zero) gives months
frame=["WSen", "xafg1O", "ya1g1f0.5"],
)
# Colormap for date
pygmt.makecpt(
cmap="batlow",
series=[date_start, date_end],
)
# Plot data with color-coding by date
fig.plot(
data=df_dates,
style="c0.4c",
pen="0.5p",
cmap=True, # Use colormap set up above
)
# Add colorbar
fig.colorbar(frame="af")
# Show and save figure
fig.show()
# fig.savefig(fname="date_cpt.png")
Output figure