kplot 1.1.31__tar.gz → 1.1.32__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: kplot
3
- Version: 1.1.31
3
+ Version: 1.1.32
4
4
  Summary: a matplotlib wrapper for keyan :-)
5
5
  Author: Keyan Gootkin
6
6
  Author-email: Keyan Gootkin <keyangootkin@gmail.com>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "kplot"
3
- version = "1.1.31"
3
+ version = "1.1.32"
4
4
  description = "a matplotlib wrapper for keyan :-)"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -4,10 +4,12 @@
4
4
  from kbasic.typing import Number, Array
5
5
  from collections.abc import Iterable
6
6
  from typing import Optional
7
- from matplotlib.pyplot import cm
7
+ from matplotlib.pyplot import cm, gca
8
8
  from matplotlib.colors import Colormap, LinearSegmentedColormap, ListedColormap, \
9
9
  hex2color, Normalize, LogNorm, FuncNorm, AsinhNorm, PowerNorm, SymLogNorm, \
10
- BoundaryNorm, CenteredNorm, TwoSlopeNorm
10
+ BoundaryNorm, CenteredNorm, TwoSlopeNorm, ScalarMappable
11
+ from matplotlib.axes_grid1 import make_axes_locatable
12
+ from matplotlib.axes import Axes
11
13
  from numpy import uint8, zeros, ndarray, inf, nanmin, nanmax, nanquantile, nanmean, \
12
14
  nanstd, absolute, log10, ones, linspace
13
15
  from numpy.typing import NDArray
@@ -33,7 +35,9 @@ blue = "#7350E6"
33
35
  lightblue = "#AE9FE3"
34
36
  shadow = "#B8B7B8"
35
37
  manoaskies = LinearSegmentedColormap.from_list("manoaskies", [pink, blue])
36
- manoaskies_centered = LinearSegmentedColormap.from_list("manoaskies_centered", [lightpink, pink, "#000000", blue, lightblue])
38
+ manoaskies_centered = LinearSegmentedColormap.from_list(
39
+ "manoaskies_centered", [lightpink, pink, "#000000", blue, lightblue]
40
+ )
37
41
  manoaskies_background_blue = "#0C0524"
38
42
  pink2grey = LinearSegmentedColormap.from_list("p2g", [pink, shadow])
39
43
  grey2black = LinearSegmentedColormap.from_list("g2b", [shadow, "#000000"])
@@ -47,15 +51,56 @@ default_cmap = cm.plasma
47
51
  # !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
48
52
  # >-|===|> Functions <|===|-<
49
53
  # !==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==!==
54
+ def colorbar(
55
+ z: Optional[Array] = None,
56
+ cmap: Cmap = default_cmap,
57
+ norm: Norm | str = 'linear',
58
+ ax: Optional[Axes] = None,
59
+ location: str = 'right',
60
+ size: str | Number = '7%',
61
+ pad: float = 0.05,
62
+ ticks: Optional[list] = None,
63
+ units: Optional[str] = None
64
+ ) -> None:
65
+ """docstring"""
66
+ n = auto_norm(norm, z)
67
+ ax = ax if ax else gca()
68
+ divider = make_axes_locatable(ax)
69
+ cax = divider.append_axes(location, size=size, pad=pad)
70
+ ax.get_figure().colorbar(
71
+ ScalarMappable(norm=n, cmap=cmap),
72
+ cax=cax, ax=ax, ticks=ticks, label=units,
73
+ orientation = 'vertical' if location in ('right', 'left') else 'horizontal'
74
+ )
75
+ if location=='top':
76
+ cax.xaxis.set_ticks_position('top')
77
+ cax.xaxis.set_label_position('top')
78
+
79
+ def parse_color(
80
+ color,
81
+ norm='linear',
82
+ cmap=default_cmap,
83
+ ):
84
+ """docstring"""
85
+ match color:
86
+ # given an rgb(a)
87
+ case ((r, g, b) | (r, g, b, a)) if all(type(x) in Number.types for x in color):
88
+ return color
89
+ # otherwise assume arrays of numbers want to be color-mapped
90
+ case x if type(color) in Array.types and type(color[0]) in Number.types:
91
+ color = [cmap(norm(c)) for c in color]
92
+ case _:
93
+ raise NotImplementedError(f"bad color: {color}")
94
+
50
95
  def auto_norm(
51
- norm: str,
96
+ norm: Norm | str,
52
97
  frames: Optional[NDArray] = None,
53
- vmin: Optional[Number] = None,
98
+ vmin: Optional[Number] = None,
54
99
  vcenter: Optional[Number] = None,
55
100
  vmax: Optional[Number] = None,
56
- linear_threshold: Optional[Number] = None,
101
+ linear_threshold: Optional[Number] = None,
57
102
  saturate: Optional[Number] = None
58
- ) -> Norm:
103
+ ) -> Norm:
59
104
  """A function to create a matplotlib normalization given a set images.
60
105
 
61
106
  Args:
@@ -69,7 +114,7 @@ def auto_norm(
69
114
  Returns:
70
115
  matplotlib normalization
71
116
  """
72
- if type(norm) in Cmap.types: return norm
117
+ if type(norm) in Norm.types: return norm
73
118
  frames = frames[(-inf < frames)&(frames < inf)]
74
119
  # set min/max
75
120
  match (frames, vmin, vmax, saturate):
@@ -98,7 +98,9 @@ def show(
98
98
  # colorbar
99
99
  if colorbar:
100
100
  divider = make_axes_locatable(ax)
101
- colorbar_location = colorbar_style.pop("location") if "location" in colorbar_style.keys() else "right"
101
+ colorbar_location = colorbar_style.pop("location")\
102
+ if "location" in colorbar_style.keys()\
103
+ else "right"
102
104
  cax = divider.append_axes(colorbar_location, **colorbar_style)
103
105
  fig.colorbar(img, cax=cax, ax=ax, ticks=cticks, label=units, orientation = 'vertical' if colorbar_location in ('right', 'left') else 'horizontal')
104
106
  if colorbar_location=='top':
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes