ggsci 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ggsci-0.1.0/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ # Python-generated files
2
+ __pycache__/
3
+ *.py[oc]
4
+ build/
5
+ dist/
6
+ wheels/
7
+ *.egg-info
8
+
9
+ # Virtual environments
10
+ .venv
@@ -0,0 +1 @@
1
+ 3.13.7
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ ## py-ggsci 0.1.0
4
+
5
+ ### New features
6
+
7
+ - Port four experimental color scales for plotnine from the R package ggsci.
8
+ - Add palette functions for direct color access.
9
+ - Support alpha transparency for all scales.
10
+ - Reverse parameter for continuous scales.
11
+ - British spelling aliases.
ggsci-0.1.0/CLAUDE.md ADDED
@@ -0,0 +1,120 @@
1
+ # py-ggsci package design notes
2
+
3
+ ## Overview
4
+
5
+ Python port of the R package ggsci providing color palettes for plotnine.
6
+
7
+ ## Package structure
8
+
9
+ ```
10
+ src/ggsci/
11
+ ├── __init__.py # Main exports
12
+ ├── data.py # Color palette data (hex strings)
13
+ ├── palettes.py # Palette generation functions
14
+ ├── scales.py # Plotnine scale implementations
15
+ └── utils.py # Color utilities (alpha, interpolation)
16
+ ```
17
+
18
+ ## Key design decisions
19
+
20
+ ### 1. Pure Python data storage
21
+
22
+ - Color data stored directly in Python dict (not TOML)
23
+ - No runtime parsing overhead
24
+ - Better IDE support and type hints
25
+ - Follows mizani pattern
26
+
27
+ ### 2. Two scale patterns
28
+
29
+ **Discrete scales**: Use `@dataclass` with `InitVar` and `__post_init__`
30
+
31
+ ```python
32
+ @dataclass
33
+ class scale_color_npg(scale_discrete):
34
+ palette: InitVar[str] = "nrc"
35
+ alpha: InitVar[float] = 1.0
36
+
37
+ def __post_init__(self, palette, alpha):
38
+ super().__post_init__()
39
+ self.palette = npg_pal(palette, alpha)
40
+ ```
41
+
42
+ **Continuous scales**: Use functions that return `scale_*_gradientn`
43
+
44
+ ```python
45
+ def scale_color_gsea(palette="default", alpha=1.0, reverse=False, **kwargs):
46
+ colors = gsea_pal(palette, n=512, alpha=alpha, reverse=reverse)
47
+ return scale_color_gradientn(colors=colors, **kwargs)
48
+ ```
49
+
50
+ ### 3. Palette function types
51
+
52
+ - **Discrete**: Return callable `(n: int) -> List[str]`
53
+ - **Continuous**: Return `List[str]` directly
54
+
55
+ ## Implemented scales
56
+
57
+ | Scale | Type | Variants | Notes |
58
+ |-------|------|----------|-------|
59
+ | NPG | Discrete | nrc | Nature Publishing Group |
60
+ | FlatUI | Discrete | default, flattastic, aussie | 3 variations |
61
+ | GSEA | Continuous | default | Diverging heatmap colors |
62
+ | BS5 | Continuous | 11 colors | Sequential Bootstrap 5 |
63
+
64
+ ## Critical implementation details
65
+
66
+ ### Plotnine imports
67
+
68
+ ```python
69
+ from plotnine.scales.scale_discrete import scale_discrete
70
+ from plotnine.scales import scale_color_gradientn, scale_fill_gradientn
71
+ ```
72
+
73
+ ### Alpha handling
74
+
75
+ - Discrete: Applied in palette function, returns RGBA hex
76
+ - Continuous: Applied during interpolation
77
+
78
+ ### Aliases
79
+
80
+ ```python
81
+ scale_colour_npg = scale_color_npg # etc.
82
+ ```
83
+
84
+ ## Testing & demo
85
+
86
+ - `tests/`: Unit tests using pytest
87
+ - `examples/demo.py`: Visual demos with plot generation
88
+
89
+ ## Commands for development
90
+
91
+ ```bash
92
+ # Run tests
93
+ uv run python test.py
94
+
95
+ # Generate demo plots
96
+ uv run python examples/demo.py
97
+
98
+ # Check structure
99
+ find src/ -name "*.py"
100
+ ```
101
+
102
+ ## Import usage
103
+
104
+ ```python
105
+ from ggsci import (
106
+ scale_color_npg, scale_fill_npg, # Discrete NPG
107
+ scale_color_flatui, # Discrete FlatUI
108
+ scale_color_gsea, scale_fill_gsea, # Continuous diverging
109
+ scale_color_bs5, scale_fill_bs5, # Continuous sequential
110
+ npg_pal, flatui_pal, gsea_pal, bs5_pal # Palette functions
111
+ )
112
+ ```
113
+
114
+ ## Architecture benefits
115
+
116
+ - **Clean**: Flattened structure, no nested subdirs
117
+ - **Pythonic**: Follows plotnine/dataclass patterns
118
+ - **Performant**: No file parsing, direct data access
119
+ - **Extensible**: Clear patterns for adding new scales
120
+ - **Compatible**: Seamless plotnine integration
ggsci-0.1.0/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2025, py-ggsci authors
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ggsci-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.4
2
+ Name: ggsci
3
+ Version: 0.1.0
4
+ Summary: Plotnine color palettes inspired by plots in scientific journals, data visualization libraries, science fiction movies, and TV shows
5
+ Project-URL: Homepage, https://nanx.me/py-ggsci/
6
+ Project-URL: Documentation, https://nanx.me/py-ggsci/
7
+ Project-URL: Repository, https://github.com/nanxstats/py-ggsci
8
+ Project-URL: Issues, https://github.com/nanxstats/py-ggsci/issues
9
+ Project-URL: Changelog, https://github.com/nanxstats/py-ggsci/blob/main/CHANGELOG.md
10
+ Author-email: Nan Xiao <me@nanx.me>
11
+ License-File: LICENSE
12
+ Requires-Python: >=3.10
13
+ Requires-Dist: matplotlib>=3.10.6
14
+ Requires-Dist: numpy>=2.2.6
15
+ Requires-Dist: plotnine>=0.15.0
16
+ Description-Content-Type: text/markdown
17
+
18
+ # ggsci for Python
19
+
20
+ [![PyPI version](https://img.shields.io/pypi/v/ggsci)](https://pypi.org/project/ggsci/)
21
+ ![Python versions](https://img.shields.io/pypi/pyversions/ggsci)
22
+ [![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)
23
+ [![CI Tests](https://github.com/nanxstats/py-ggsci/actions/workflows/ci-tests.yml/badge.svg)](https://github.com/nanxstats/py-ggsci/actions/workflows/ci-tests.yml)
24
+ [![mkdocs](https://github.com/nanxstats/py-ggsci/actions/workflows/mkdocs.yml/badge.svg)](https://nanx.me/py-ggsci/)
25
+ ![License](https://img.shields.io/pypi/l/ggsci)
26
+
27
+ ggsci for Python provides color palettes for plotnine.
28
+
29
+ ## Installation
30
+
31
+ You can install py-ggsci from PyPI:
32
+
33
+ ```bash
34
+ pip install ggsci
35
+ ```
36
+
37
+ Or install the development version from GitHub:
38
+
39
+ ```bash
40
+ git clone https://github.com/nanxstats/py-ggsci.git
41
+ cd py-ggsci
42
+ python3 -m pip install -e .
43
+ ```
ggsci-0.1.0/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # ggsci for Python
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/ggsci)](https://pypi.org/project/ggsci/)
4
+ ![Python versions](https://img.shields.io/pypi/pyversions/ggsci)
5
+ [![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)
6
+ [![CI Tests](https://github.com/nanxstats/py-ggsci/actions/workflows/ci-tests.yml/badge.svg)](https://github.com/nanxstats/py-ggsci/actions/workflows/ci-tests.yml)
7
+ [![mkdocs](https://github.com/nanxstats/py-ggsci/actions/workflows/mkdocs.yml/badge.svg)](https://nanx.me/py-ggsci/)
8
+ ![License](https://img.shields.io/pypi/l/ggsci)
9
+
10
+ ggsci for Python provides color palettes for plotnine.
11
+
12
+ ## Installation
13
+
14
+ You can install py-ggsci from PyPI:
15
+
16
+ ```bash
17
+ pip install ggsci
18
+ ```
19
+
20
+ Or install the development version from GitHub:
21
+
22
+ ```bash
23
+ git clone https://github.com/nanxstats/py-ggsci.git
24
+ cd py-ggsci
25
+ python3 -m pip install -e .
26
+ ```
@@ -0,0 +1,176 @@
1
+ """
2
+ Demo of py-ggsci scales with plotnine
3
+
4
+ This script demonstrates how to use the py-ggsci color scales
5
+ with plotnine for creating publication-ready plots.
6
+ """
7
+
8
+ import numpy as np
9
+ import pandas as pd
10
+ from plotnine import (
11
+ aes,
12
+ facet_wrap,
13
+ geom_bar,
14
+ geom_point,
15
+ geom_tile,
16
+ ggplot,
17
+ labs,
18
+ theme_minimal,
19
+ )
20
+
21
+ from ggsci import scale_color_npg, scale_fill_bs5, scale_fill_flatui, scale_fill_gsea
22
+
23
+
24
+ def demo_npg_discrete():
25
+ """Demo NPG discrete scale with scatter plot."""
26
+ # Create sample data
27
+ np.random.seed(42)
28
+ data = pd.DataFrame(
29
+ {
30
+ "x": np.random.randn(100),
31
+ "y": np.random.randn(100),
32
+ "category": np.random.choice(["A", "B", "C", "D", "E"], 100),
33
+ }
34
+ )
35
+
36
+ # Create plot
37
+ plot = (
38
+ ggplot(data, aes(x="x", y="y", color="category"))
39
+ + geom_point(size=3, alpha=0.7)
40
+ + scale_color_npg()
41
+ + theme_minimal()
42
+ + labs(
43
+ title="NPG Color Scale Demo",
44
+ subtitle="Nature Publishing Group inspired colors",
45
+ x="X Value",
46
+ y="Y Value",
47
+ )
48
+ )
49
+
50
+ return plot
51
+
52
+
53
+ def demo_flatui_variations():
54
+ """Demo FlatUI scale with its three variations."""
55
+ # Create sample data
56
+ np.random.seed(42)
57
+ categories = ["A", "B", "C", "D", "E", "F"]
58
+ values = np.random.randint(10, 100, len(categories))
59
+ data = pd.DataFrame({"category": categories, "value": values})
60
+
61
+ # Create and save three separate plots
62
+ palettes = ["default", "flattastic", "aussie"]
63
+ for palette in palettes:
64
+ plot = (
65
+ ggplot(data, aes(x="category", y="value", fill="category"))
66
+ + geom_bar(stat="identity")
67
+ + scale_fill_flatui(palette=palette)
68
+ + theme_minimal()
69
+ + labs(
70
+ title=f"Flat UI Color Scale - {palette.title()}",
71
+ x="Category",
72
+ y="Value",
73
+ )
74
+ )
75
+ plot.save(f"flatui_{palette}.png", dpi=150, width=8, height=6)
76
+ print(f"Saved flatui_{palette}.png")
77
+
78
+ # Return None since we've already saved the individual files
79
+ return None
80
+
81
+
82
+ def demo_gsea_continuous():
83
+ """Demo GSEA continuous diverging scale with heatmap."""
84
+ # Create correlation matrix data
85
+ np.random.seed(42)
86
+ n_vars = 10
87
+ corr_matrix = np.random.randn(n_vars, n_vars)
88
+ corr_matrix = (corr_matrix + corr_matrix.T) / 2 # Make symmetric
89
+ np.fill_diagonal(corr_matrix, 1)
90
+
91
+ # Convert to long format for plotnine
92
+ data = []
93
+ for i in range(n_vars):
94
+ for j in range(n_vars):
95
+ data.append(
96
+ {
97
+ "var1": f"V{i + 1}",
98
+ "var2": f"V{j + 1}",
99
+ "correlation": corr_matrix[i, j],
100
+ }
101
+ )
102
+
103
+ data = pd.DataFrame(data)
104
+
105
+ # Create heatmap
106
+ plot = (
107
+ ggplot(data, aes(x="var1", y="var2", fill="correlation"))
108
+ + geom_tile()
109
+ + scale_fill_gsea()
110
+ + theme_minimal()
111
+ + labs(
112
+ title="GSEA Diverging Color Scale",
113
+ subtitle="GSEA GenePattern inspired heatmap colors",
114
+ x="Variable 1",
115
+ y="Variable 2",
116
+ )
117
+ )
118
+
119
+ return plot
120
+
121
+
122
+ def demo_bs5_sequential():
123
+ """Demo BS5 continuous sequential scale."""
124
+ # Create gradient data
125
+ x = np.linspace(0, 10, 100)
126
+ y = np.linspace(0, 10, 100)
127
+ X, Y = np.meshgrid(x, y)
128
+ Z = np.sin(np.sqrt(X**2 + Y**2))
129
+
130
+ # Convert to dataframe
131
+ data = pd.DataFrame({"x": X.flatten(), "y": Y.flatten(), "z": Z.flatten()})
132
+
133
+ # Create plot with different BS5 palettes
134
+ plot = (
135
+ ggplot(data.sample(5000), aes(x="x", y="y", fill="z"))
136
+ + geom_tile()
137
+ + scale_fill_bs5(palette="teal")
138
+ + theme_minimal()
139
+ + labs(
140
+ title="Bootstrap 5 Sequential Color Scale",
141
+ subtitle='Using the "teal" palette',
142
+ x="X",
143
+ y="Y",
144
+ fill="Value",
145
+ )
146
+ )
147
+
148
+ return plot
149
+
150
+
151
+ if __name__ == "__main__":
152
+ print("Generating demo plots...")
153
+
154
+ # Demo each scale type
155
+ plots = {
156
+ "npg_discrete": demo_npg_discrete(),
157
+ "flatui_variations": demo_flatui_variations(),
158
+ "gsea_continuous": demo_gsea_continuous(),
159
+ "bs5_sequential": demo_bs5_sequential(),
160
+ }
161
+
162
+ # Save plots (requires plotnine to be properly configured)
163
+ for name, plot in plots.items():
164
+ if plot is not None: # Skip None plots (like flatui_variations)
165
+ try:
166
+ plot.save(f"{name}.png", dpi=150, width=10, height=8)
167
+ print(f"Saved {name}.png")
168
+ except Exception as e:
169
+ print(f"Could not save {name}.png: {e}")
170
+
171
+ print("\nDemo complete!")
172
+ print("\nYou can use these scales in your plotnine plots:")
173
+ print("- scale_color_npg() / scale_fill_npg()")
174
+ print("- scale_color_flatui() / scale_fill_flatui()")
175
+ print("- scale_color_gsea() / scale_fill_gsea()")
176
+ print("- scale_color_bs5() / scale_fill_bs5()")
@@ -0,0 +1,38 @@
1
+ [project]
2
+ name = "ggsci"
3
+ version = "0.1.0"
4
+ description = "Plotnine color palettes inspired by plots in scientific journals, data visualization libraries, science fiction movies, and TV shows"
5
+ authors = [
6
+ { name = "Nan Xiao", email = "me@nanx.me" }
7
+ ]
8
+ dependencies = [
9
+ "matplotlib>=3.10.6",
10
+ "numpy>=2.2.6",
11
+ "plotnine>=0.15.0",
12
+ ]
13
+ readme = "README.md"
14
+
15
+ requires-python = ">= 3.10"
16
+
17
+ [project.urls]
18
+ Homepage = "https://nanx.me/py-ggsci/"
19
+ Documentation = "https://nanx.me/py-ggsci/"
20
+ Repository = "https://github.com/nanxstats/py-ggsci"
21
+ Issues = "https://github.com/nanxstats/py-ggsci/issues"
22
+ Changelog = "https://github.com/nanxstats/py-ggsci/blob/main/CHANGELOG.md"
23
+
24
+ [build-system]
25
+ requires = ["hatchling"]
26
+ build-backend = "hatchling.build"
27
+
28
+ [dependency-groups]
29
+ dev = [
30
+ "isort>=6.0.1",
31
+ "mkdocs>=1.6.1",
32
+ "mkdocs-material>=9.6.19",
33
+ "mkdocstrings-python>=1.18.2",
34
+ "mypy>=1.17.1",
35
+ "pytest>=8.4.2",
36
+ "pytest-cov>=7.0.0",
37
+ "ruff>=0.12.12",
38
+ ]
@@ -0,0 +1,43 @@
1
+ """
2
+ py-ggsci: Scientific journal color palettes for Python
3
+
4
+ A Python implementation of the R ggsci package, providing color palettes
5
+ inspired by scientific journals and sci-fi themes.
6
+ """
7
+
8
+ from .palettes import bs5_pal, flatui_pal, gsea_pal, npg_pal
9
+ from .scales import (
10
+ scale_color_bs5,
11
+ scale_color_flatui,
12
+ scale_color_gsea,
13
+ scale_color_npg,
14
+ scale_colour_bs5,
15
+ scale_colour_flatui,
16
+ scale_colour_gsea,
17
+ scale_colour_npg,
18
+ scale_fill_bs5,
19
+ scale_fill_flatui,
20
+ scale_fill_gsea,
21
+ scale_fill_npg,
22
+ )
23
+
24
+ __version__ = "0.1.0"
25
+
26
+ __all__ = [
27
+ "scale_color_npg",
28
+ "scale_fill_npg",
29
+ "scale_colour_npg",
30
+ "scale_color_flatui",
31
+ "scale_fill_flatui",
32
+ "scale_colour_flatui",
33
+ "scale_color_gsea",
34
+ "scale_fill_gsea",
35
+ "scale_colour_gsea",
36
+ "scale_color_bs5",
37
+ "scale_fill_bs5",
38
+ "scale_colour_bs5",
39
+ "npg_pal",
40
+ "flatui_pal",
41
+ "gsea_pal",
42
+ "bs5_pal",
43
+ ]