1414 data_kind ,
1515 deprecate_parameter ,
1616 fmt_docstring ,
17+ is_given ,
1718 is_nonstr_iter ,
1819 kwargs_to_strings ,
1920 use_alias ,
@@ -206,7 +207,7 @@ def histogram(
206207 bar_offset : float | str | None = None ,
207208 cmap : str | bool = False ,
208209 pen : str | None = None ,
209- fill : str | Pattern | None = None ,
210+ fill : str | Pattern | Literal [ "position" , "value" ] | None = None ,
210211 annot : bool = False ,
211212 annot_position : Literal ["start" , "end" ] = "end" ,
212213 annot_font : str | None = None ,
@@ -240,7 +241,7 @@ def histogram(
240241 $aliases
241242 - A = horizontal
242243 - B = frame
243- - C = cmap
244+ - C = cmap, **+b**: fill
244245 - D = annot, **+b**: annot_position, **+f**: annot_font, **+o**: annot_offset,
245246 **+r**: annot_orientation
246247 - E = bar_width, **+o**: bar_offset
@@ -271,12 +272,22 @@ def histogram(
271272 is a file name or a 2-D sequence.
272273 - A 1-D array of weights, one per data point, requiring that ``data`` is a 1-D
273274 sequence of values.
274- $cmap
275275 pen
276276 Draw bar outline (or stair-case curve) using the specified pen thickness
277277 [Default is no outline].
278278 fill
279- Set color or pattern for filling bars [Default is no fill].
279+ Set color or pattern for filling bars [Default is no fill]. Set it to one of the
280+ two special values to fill bars by looking up the color from a CPT instead of a
281+ constant color:
282+
283+ - ``"position"``: look up the color using the mid-coordinate of the bin. This is
284+ the default when ``cmap`` is set.
285+ - ``"value"``: look up the color using the bin value, i.e., the bar count or
286+ frequency.
287+
288+ The special values require a CPT (either the current CPT or explicitly set by
289+ ``cmap``), and can't be used with ``fill``.
290+ $cmap
280291 annot
281292 If ``True``, annotate each bar with the value it represents. The remaining
282293 ``annot_*`` parameters control how the annotations look and are ignored if
@@ -384,6 +395,20 @@ def histogram(
384395 required = "bar_width" , reason = "Required when 'bar_offset' is set."
385396 )
386397
398+ # "position" and "value" are special values that fill bars by values and cmap.
399+ match fill :
400+ case "position" :
401+ _fill_color , _fill_lookup = None , ""
402+ case "value" :
403+ _fill_color , _fill_lookup = None , "+b"
404+ case _:
405+ _fill_color , _fill_lookup = fill , None # type: ignore[assignment]
406+ if _fill_color is not None and is_given (cmap ):
407+ raise GMTParameterError (
408+ at_most_one = ["cmap" , "fill" ],
409+ reason = "Cannot use 'cmap' when 'fill' is a constant color or pattern." ,
410+ )
411+
387412 # weights can be given as a 1-D array, or as a boolean to indicate that the second
388413 # column of data contains weights. If weights is an array, then data must be a 1-D
389414 # sequence of values.
@@ -396,7 +421,10 @@ def histogram(
396421
397422 aliasdict = AliasSystem (
398423 A = Alias (horizontal , name = "horizontal" ),
399- C = Alias (cmap , name = "cmap" ),
424+ C = [
425+ Alias (cmap , name = "cmap" ),
426+ Alias (_fill_lookup , name = "fill" ),
427+ ],
400428 D = _alias_option_D (
401429 annot = annot ,
402430 annot_position = annot_position ,
@@ -409,7 +437,7 @@ def histogram(
409437 Alias (bar_offset , name = "bar_offset" , prefix = "+o" ),
410438 ],
411439 F = Alias (center , name = "center" ),
412- G = Alias (fill , name = "fill" ),
440+ G = Alias (_fill_color , name = "fill" ),
413441 L = Alias (
414442 out_range ,
415443 name = "out_range" ,
0 commit comments