Hello everyone
I want to use pygmt to draw subplots containing two types of subgraphs, but the text of the subgraphs will overlap, this situation can be solved by increasing the spacing between subgraphs through the “margins” parameter, but too much spacing will affect the beauty of the graph. My code is as follows:
import pygmt
from osgeo import gdal
def create_histogram(fig, file, xmin, xmax, ymax, frame):
gdal.AllRegister()
dataset = gdal.Open(file)
im_data = dataset.ReadAsArray(0, 0, dataset.RasterXSize, dataset.RasterYSize)
pygmt.config(FONT="16p,4")
fig.histogram(
# Define the plot range as a list of xmin, xmax, ymin, ymax
# Let ymin and ymax determined automatically by setting both to the same
# value
region=[xmin, xmax, 0, ymax],
# projection="X7c", # Cartesian projection with a width of 10 centimeters
# Add frame, annotations (a), ticks (f), and y-axis label (+l) "Counts"
# The numbers give the steps of annotations and ticks
frame=frame,
data=im_data,
# Set the bin width via the "series" parameter
series=1,
# Fill the bars with color "red3"
fill="red3",
# Draw a 1-point thick solid outline in "darkgray" around the bars
pen="1p,darkgray,solid",
# Choose counts via the "histtype" parameter
histtype=1,
)
fig = pygmt.Figure()
with pygmt.config(FONT_TITLE="18p,5", MAP_TITLE_OFFSET="-12p", MAP_FRAME_TYPE="plain"):
with fig.subplot(ncols=2, nrows=4, subsize=("7c", "7c"), margins=["0.1c", "0.1c"]):
lowest = -15
highest = 15
interval = (highest - lowest) / 64
with fig.set_panel(panel=[0, 0]):
pygmt.makecpt(cmap="Purple_Orange.cpt", series=[lowest, highest, interval])
fig.grdimage(grid=fr'276_Method1.tif', frame=[f"lbtr+tMethod1"], cmap=True)
with fig.set_panel(panel=[0, 1]):
pygmt.makecpt(cmap="Purple_Orange.cpt", series=[lowest, highest, interval])
fig.grdimage(grid=fr'276_Method2.tif', frame=[f"lbtr+tMethod2"], cmap=True)
with fig.set_panel(panel=[2, 0]):
pygmt.makecpt(cmap="Purple_Orange.cpt", series=[lowest, highest, interval])
fig.grdimage(grid=fr'277_Method1.tif', frame=[f"lbtr+tMethod1"], cmap=True)
with fig.set_panel(panel=[2, 1]):
pygmt.makecpt(cmap="Purple_Orange.cpt", series=[lowest, highest, interval])
fig.grdimage(grid=fr'277_Method2.tif', frame=[f"lbtr+tMethod2"], cmap=True)
with fig.set_panel(panel=[1, 0]):
frame = ["lStr", "xa5f1+lBias (K)"]
create_histogram(fig, fr'276_Method1.tif', lowest, highest, 50, frame)
with fig.set_panel(panel=[1, 1]):
frame = ["lStr", "xa5f1+lBias (K)"]
create_histogram(fig, fr'276_Method2.tif', lowest, highest, 50, frame)
with fig.set_panel(panel=[3, 0]):
frame = ["lStr", "xa5f1+lBias (K)"]
create_histogram(fig, fr'277_Method1.tif', lowest, highest, 50, frame)
with fig.set_panel(panel=[3, 1]):
frame = ["lStr", "xa5f1+lBias (K)"]
create_histogram(fig, fr'277_Method2.tif', lowest, highest, 50, frame)
fig.show()
With margins set to [“1c”, “1c”], the spacing between subgraphs is too large and very unattractive:
We can also set the spacing of the subgraphs individually by setting the “clearance” parameter, but it will reduce the length of the subgraphs. The code is as follows:
with fig.set_panel(panel=[1, 0], clearance=["s1.2c", "n0.08c"]):
frame = ["lStr", "xa5f1+lBias (K)"]
create_histogram(fig, fr'276_Method1.tif', lowest, highest, 50, frame)
with fig.set_panel(panel=[1, 1], clearance=["s1.2c", "n0.08c"]):
frame = ["lStr", "xa5f1+lBias (K)"]
create_histogram(fig, fr'276_Method2.tif', lowest, highest, 50, frame)
Data and cpt files:
https://drive.google.com/drive/folders/1fwFN0yActYL8m86a6jBEMJYZw0Ypc83b?usp=sharing
Is there a good solution to this problem?