Configure gsplot#

gsplot provides validated figure and plotting defaults through a JSON file. Loading is explicit, returns an immutable Config, and never changes Matplotlib global state.

Explicit loading#

Canonical gsplot never searches the current directory or a user directory. Supply the file explicitly:

import gsplot as gs

config = gs.load_config("path/to/gsplot.json")

Use config.section("figure"), config.get(...), or config.as_mapping() to inspect the immutable value.

Parameter precedence#

For a configurable function, explicit arguments take precedence over values in the configuration file, which take precedence over function defaults.

Schema 2 example#

{
  "schema_version": 2,
  "figure": {
    "size": [5, 5],
    "unit": "in",
    "squeeze": true,
    "layout": "tight"
  },
  "plotting": {
    "default_color": "#3b5bdb",
    "default_cmap": "viridis"
  }
}
fig, axes = gs.subplots("ABC", config=config)

The configured figure size is used, while an explicitly supplied function argument wins over the configured value. Schema-1 input is translated with a migration warning during the 1.x compatibility window; new files should use schema 2.

Backend selection#

Select a backend before importing matplotlib.pyplot or creating a managed Figure:

gs.use_backend("Agg")

For interactive use, setting MPLBACKEND before starting Python is often the simplest choice.

Complete example#

from pprint import pprint

import gsplot as gs

# Configuration loading is explicit and returns an immutable value object.
config = gs.load_config("./gsplot.json")

print("configuration:")
pprint(config.as_mapping())

# Direct arguments override values from the configuration file.
fig, axes = gs.subplots("A", config=config)
axis = axes["A"]
gs.line(axis, [0, 1, 2], [0, 1, 4], label="configured line")
gs.legend(axis)
gs.save(fig, "config", show=False)
{
  "schema_version": 2,
  "figure": {
    "size": [5, 5],
    "unit": "in",
    "squeeze": true,
    "layout": "tight"
  },
  "plotting": {
    "default_color": "#3b5bdb",
    "default_cmap": "viridis"
  }
}
Figure generated by the configuration example

Download the generated PDF.