GenericMappingTools / pygmt

A Python interface for the Generic Mapping Tools.
https://www.pygmt.org
BSD 3-Clause "New" or "Revised" License
730 stars 214 forks source link

changing map annotation font size #733

Open uliw opened 3 years ago

uliw commented 3 years ago

Description of the problem

Setting

pygmt.config(FONT_ANNOT="6p")

appears to have no effect and setting it directly like this

fig.basemap(
    region=region,
    projection=projection,
    frame=True,
    font_annot="6p")

results in a parsing error.

GMTCLibError: Module 'basemap' failed with status code 71:
basemap [ERROR]: Malformed -f argument [ont_annot6p]
basemap [ERROR]: Option -f parsing failure. Correct syntax:
basemap [ERROR]: Offending option -font_annot6p

I may not see the forest for trees here... so any pointers are much appreciated

PyGMT information:
  version: v0.2.1
System information:
  python: 3.8.6 | packaged by conda-forge | (default, Oct  7 2020, 19:08:05)  [GCC 7.5.0]
  executable: /home/uliw/anaconda3/bin/python
  machine: Linux-5.9.1-2-default-x86_64-with-glibc2.10
Dependency information:
  numpy: 1.18.5
  pandas: 1.1.5
  xarray: 0.16.2
  netCDF4: 1.5.5
  packaging: 20.8
  ghostscript: 9.53.3
  gmt: 6.1.1
GMT library information:
  binary dir: /home/uliw/anaconda3/bin
  cores: 8
  grid layout: rows
  library path: /home/uliw/anaconda3/lib/libgmt.so
  padding: 2
  plugin dir: /home/uliw/anaconda3/lib/gmt/plugins
  share dir: /home/uliw/anaconda3/share/gmt
  version: 6.1.1

This is

Full code that generated the error

PASTE CODE HERE

Full error message

PASTE ERROR MESSAGE HERE

System information

Please paste the output of python -c "import pygmt; pygmt.show_versions()":

PASTE THE OUTPUT HERE
seisman commented 3 years ago

Setting pygmt.config(FONT_ANNOT="6p") should work. Could you please provide your full codes?

uliw commented 3 years ago

Here is an example. Changing the value from 6 to e.g., does not affect the font size

import pygmt

pygmt.config(FONT_ANNOT="6p")

projection = "A-95/42/4i" 
region = "-119/22/-50/50r" 
fig = pygmt.Figure()
fig.basemap(
    region=region,
    projection=projection,
    frame=True,
)
fig.show()
seisman commented 3 years ago

OK. I can reproduce your issue. Will see why it doesn't work as expected.

Here is a workaround:

import pygmt

projection = "A-95/42/4i" 
region = "-119/22/-50/50r" 
fig = pygmt.Figure()

pygmt.config(FONT_ANNOT="6p")

fig.basemap(
    region=region,
    projection=projection,
    frame=True,
)
fig.show()
uliw commented 3 years ago

ah ok, so when issued after creating the figure instance it will work. Thanks!

On Mon, Dec 14, 2020 at 1:13 PM Dongdong Tian notifications@github.com wrote:

OK. I can reproduce your issue. Will see why it doesn't work as expected.

Here is a workaround:

import pygmt

projection = "A-95/42/4i" region = "-119/22/-50/50r" fig = pygmt.Figure()

pygmt.config(FONT_ANNOT="6p")

fig.basemap( region=region, projection=projection, frame=True, ) fig.show()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/GenericMappingTools/pygmt/issues/733#issuecomment-744617731, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABWSVAVQF7SSMHINX2NJ55LSUZIVBANCNFSM4U3B6IIQ .

-- Ulrich G. Wortmann https://www.es.utoronto.ca/people/faculty/wortmann-ulrich/ Dept. of Earth Sciences Fax : 416 978 3938 University of Toronto Phone: 416 978 7084 22 Ursula Franklin Street, Toronto, ON, Canada M5S 3B1

seisman commented 3 years ago

Here is the step to reproduce the issue and understand why it doesn't work as expected.

We first need to start a jupyter notebook, and run the following script.

import pygmt

pygmt.config(FONT_ANNOT="20p")

projection = "A-95/42/4i" 
region = "-119/22/-50/50r" 
fig = pygmt.Figure()
fig.basemap(
    region=region,
    projection=projection,
    frame=True,
)
fig.show()

import pygmt creates a new session, and pygmt.config() changes GMT's global settings. The script can produce a plot with a large annotation font size, as expected.

Then, in the same jupyter notebook, we can change the annotation font size 20p to 6p, and run the script again. It still produces a plot with a large annotation font size.

We can rewrite the above processes into a single script, like below:

# "import pygmt" start a new session
import pygmt

# it changes GMT's global settings
pygmt.config(FONT_ANNOT="20p")

projection = "A-95/42/4i" 
region = "-119/22/-50/50r" 
fig = pygmt.Figure()
# the basemap has an annotation font size of 20p, as determined by the global FONT_ANNOT setting
fig.basemap(
    region=region,
    projection=projection,
    frame=True,
)
fig.show()

# here, "import pygmt" doesn't start a new session
import pygmt

# here, "pygmt.config()" changes the **local** settings of the **previous** figure, so it doesn't affect the next figure
pygmt.config(FONT_ANNOT="6p")

projection = "A-95/42/4i" 
region = "-119/22/-50/50r" 
fig = pygmt.Figure()
# the basemap still has an annotation font size of 20p, as determined by the global FONT_ANNOT setting
fig.basemap(
    region=region,
    projection=projection,
    frame=True,
)
fig.show()
weiji14 commented 3 years ago

My reading of this issue is that it is a problem related to having one GMT session open all the time in PyGMT (c.f. #867). Either we:

  1. create a fig.destroy()/fig.clear() (or similar) method
  2. have a context manager like with pygmt.Figure() as fig: fig.* ... to keep the scope of things local.
  3. we enforce/strongly recommend the use of pygmt.config in a with statement so that things are kept to a local scope only.

This is needed to ensure that one figure session doesn't pollute the configurations of subsequent figures.

seisman commented 3 years ago

Or we could document the behavior of pygmt.config() better.