Skip to content

Commit 3b69d6d

Browse files
authored
Merge branch 'develop' into pssg-look-and-feel
2 parents bbafd3f + 38c5dba commit 3b69d6d

File tree

4 files changed

+118
-8
lines changed

4 files changed

+118
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# python
22
.venv
33
*.pyc
4-
__pycache__/
4+
__pycache__/
5+
*.egg-info/

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ A lightweight Python plotting library for creating plots in the PSSG style.
66

77
## Simple Bar Chart Demo
88

9-
Aquick demo of plotting a simple bar chart:
9+
A quick demo of plotting a simple bar chart:
1010

1111
```python
1212
from pssgplot import PlotEnvironment, BarPlot
1313
import pandas as pd
1414

15-
with PlotEnvironment(font_path='fonts/gillsans.ttf'):
15+
with PlotEnvironment():
1616
df = pd.DataFrame({
1717
"x": ["A", "B", "C", "D"],
1818
"y": [1, 2, 3, 4],
@@ -27,6 +27,21 @@ with PlotEnvironment(font_path='fonts/gillsans.ttf'):
2727
chart.save('output/simple-bar-chart.png')
2828
```
2929

30+
## Installing and Setup
31+
32+
Setting up with _uv_ is straightforward:
33+
34+
```bash
35+
uv venv
36+
source .venv/bin/activate
37+
38+
uv pip install -e pssg-plots/
39+
40+
uv run plotting-script.py
41+
```
42+
43+
You can also just use vanilla pip and virtualenvs if you'd like.
44+
3045
## Examples
3146

3247
For more details and demos, check out the example files:

pssgplot/pssgplot.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"""
33
# std imports
44
from os import PathLike
5+
from pathlib import Path
56
from typing import List, Optional, Union
7+
import warnings
68

79
# tpl imports
810
from matplotlib.font_manager import FontProperties, fontManager
@@ -50,12 +52,8 @@ def __init__(
5052
"""
5153
if interactive and backend is not None:
5254
raise ValueError("interactive and backend cannot both be specified.")
53-
54-
if font_name is not None and font_path is not None:
55-
raise ValueError("Only one of font_name and font_path may be specified.")
5655

57-
if font_path is not None:
58-
font_name = self._load_font(font_path)
56+
font_name = self._resolve_font(font_name, font_path)
5957

6058
if font_scale <= 0:
6159
raise ValueError("font_scale must be positive.")
@@ -104,3 +102,39 @@ def _load_font(self, font_path: PathLike) -> str:
104102
""" load a font into matplotlib and return its name """
105103
fontManager.addfont(font_path)
106104
return FontProperties(fname=font_path).get_name()
105+
106+
def _resolve_font(self, font_name: Optional[str], font_path: Optional[str]) -> str:
107+
""" Resolves the font to use for plotting.
108+
If neither font_name nor font_path is specified, look for the Gill Sans MT or Gill Sans font packaged with this library.
109+
If it is not found, look for a systems Gill Sans or Gill Sans MT font. If it is still not found, print a warning and use the default font.
110+
If font_name is specified, use it.
111+
If font_path is specified, load the font from the path and use it.
112+
If both are specified, raise an error.
113+
"""
114+
if font_name is not None and font_path is not None:
115+
raise ValueError("Only one of font_name and font_path may be specified.")
116+
117+
if font_path is not None:
118+
return self._load_font(font_path)
119+
120+
if font_name is not None:
121+
return font_name
122+
123+
# Use bundled Gill Sans font if available
124+
bundled_font_path = Path(__file__).parent.parent / "fonts" / "gillsans.ttf"
125+
if bundled_font_path.exists():
126+
return self._load_font(bundled_font_path)
127+
128+
# Look for system Gill Sans or Gill Sans MT fonts
129+
available_fonts = [f.name for f in fontManager.ttflist]
130+
for font_candidate in ['Gill Sans MT', 'Gill Sans', 'GillSans']:
131+
if font_candidate in available_fonts:
132+
return font_candidate
133+
134+
# Fall back to default font with warning
135+
warnings.warn(
136+
"Gill Sans font not found. Using default sans-serif font. "
137+
"For best results, install Gill Sans or specify a custom font.",
138+
UserWarning
139+
)
140+
return "sans-serif"

pyproject.toml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[build-system]
2+
requires = ["setuptools>=61.0", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "pssgplot"
7+
version = "0.1.0"
8+
description = "A lightweight Python plotting library for creating plots in the PSSG style"
9+
readme = "README.md"
10+
requires-python = ">=3.8"
11+
license = {text = "MIT"}
12+
authors = [
13+
{name = "PSSG Team"}
14+
]
15+
keywords = ["plotting", "visualization", "charts", "matplotlib"]
16+
classifiers = [
17+
"Development Status :: 3 - Alpha",
18+
"Intended Audience :: Science/Research",
19+
"License :: OSI Approved :: MIT License",
20+
"Programming Language :: Python :: 3",
21+
"Programming Language :: Python :: 3.8",
22+
"Programming Language :: Python :: 3.9",
23+
"Programming Language :: Python :: 3.10",
24+
"Programming Language :: Python :: 3.11",
25+
"Programming Language :: Python :: 3.12",
26+
"Topic :: Scientific/Engineering :: Visualization",
27+
]
28+
29+
dependencies = [
30+
"pandas>=2.0.0",
31+
"matplotlib>=3.7.0",
32+
"seaborn>=0.13.0",
33+
]
34+
35+
[project.optional-dependencies]
36+
dev = [
37+
"pytest>=7.0",
38+
"black>=23.0",
39+
"flake8>=6.0",
40+
]
41+
42+
[project.urls]
43+
Homepage = "https://github.com/hpcgroup/pssg-plots"
44+
Repository = "https://github.com/hpcgroup/pssg-plots"
45+
46+
[tool.setuptools]
47+
packages = ["pssgplot"]
48+
49+
[tool.setuptools.package-data]
50+
pssgplot = ["*.ttf", "fonts/*.ttf"]
51+
52+
[tool.black]
53+
line-length = 88
54+
target-version = ["py38", "py39", "py310", "py311"]
55+
56+
[tool.pytest.ini_options]
57+
testpaths = ["tests"]
58+
python_files = ["test_*.py"]
59+
python_classes = ["Test*"]
60+
python_functions = ["test_*"]

0 commit comments

Comments
 (0)