Plotting functions
Module containing plotting functions for summarising and visualising data and dataframe objects.
dod2k_utilities.ut_plot
@author: Lucie Luecke
Plotting functions for displaying data(frames).
Last updated 19/12/2025 for publication of dod2k v2.0
df_colours_markers(db_name='dod2k_v2.0')
Generate archive colours and proxy markers for plotting functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_name
|
str
|
Name of the database CSV file to load. Default is 'dod2k_dupfree_dupfree'. |
'dod2k_v2.0'
|
Returns:
| Name | Type | Description |
|---|---|---|
archive_colour |
dict
|
Dictionary mapping archive types to color codes. |
archives_sorted |
ndarray
|
Sorted list of archive types based on record count. |
proxy_marker |
dict
|
Dictionary mapping each archive type and proxy to a specific marker. |
Source code in dod2k_utilities/ut_plot.py
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 | |
geo_EOF_plot(df, pca_rec, EOFs, keys, fs=(13, 8), dpi=350, barlabel='EOF', which_EOF=0)
Plot geographic distribution of records colored by EOF loadings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame with paleo-proxy records. Must include columns 'geo_meanLat', 'geo_meanLon', 'datasetId'. |
required |
pca_rec
|
dict
|
Dictionary mapping keys to lists of dataset IDs included in PCA. |
required |
EOFs
|
dict
|
Dictionary mapping keys to EOF arrays. |
required |
keys
|
list
|
List of keys (record types) to plot. |
required |
fs
|
tuple
|
Figure size in inches. Default is (13, 8). |
(13, 8)
|
dpi
|
int
|
Figure resolution in dots per inch. Default is 350. |
350
|
barlabel
|
str
|
Label for the colorbar. Default is 'EOF'. |
'EOF'
|
which_EOF
|
int
|
Index of the EOF to plot. Default is 0 (first EOF). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure
|
Matplotlib figure object containing the EOF-colored map. |
Source code in dod2k_utilities/ut_plot.py
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 | |
geo_plot(df, fs=(9, 4.5), dpi=350, return_col=False, **kwargs)
Plot the spatial distribution of paleo-proxy records on a global map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing the records. Must include columns: 'geo_meanLat', 'geo_meanLon', 'archiveType', 'paleoData_proxy', 'datasetId'. |
required |
fs
|
tuple
|
Figure size (width, height) in inches. Default is (9, 4.5). |
(9, 4.5)
|
dpi
|
int
|
Figure resolution in dots per inch. Default is 350. |
350
|
**kwargs
|
dict
|
Optional keyword arguments. Supported keys: - 'mark_records': dict, to highlight specific datasets on the map. - 'mark_archives': list of archive keys to mark. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
fig |
Figure
|
Matplotlib figure object containing the map. |
Source code in dod2k_utilities/ut_plot.py
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 | |
get_archive_colours(archives_sorted, archive_count, cols=['#4477AA', '#EE6677', '#228833', '#CCBB44', '#66CCEE', '#AA3377', '#BBBBBB', '#44AA99', '#332288'])
Assign colors to archive types based on record abundance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
archives_sorted
|
list of str
|
Archive types sorted in descending or preferred order, typically by record count. |
required |
archive_count
|
dict
|
Dictionary mapping archive type to total number of records. |
required |
cols
|
list of str
|
List of color hex codes used to assign colors to major archives.
The last color in the list is reserved for minor archives and
the aggregated |
['#4477AA', '#EE6677', '#228833', '#CCBB44', '#66CCEE', '#AA3377', '#BBBBBB', '#44AA99', '#332288']
|
Returns:
| Name | Type | Description |
|---|---|---|
archive_colour |
dict
|
Mapping from archive type to assigned color. Includes an
|
major_archives |
list of str
|
Archive types with more than 10 records. |
other_archives |
list of str
|
Archive types with 10 or fewer records. |
Notes
Archive types with more than 10 records are treated as major archives
and assigned unique colors. All remaining archive types are grouped
under 'other' and share a common color.
Source code in dod2k_utilities/ut_plot.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
get_colours(data, colormap='brewer_RdBu_11', minval=False, maxval=False, return_mappable=False)
Generate colors from a colormap based on data values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array - like
|
Array or list of numerical values to map to colors. |
required |
colormap
|
str
|
Matplotlib colormap name. Default is 'brewer_RdBu_11'. |
'brewer_RdBu_11'
|
minval
|
float or False
|
Minimum value for color normalization. If False, uses min(data). Default is False. |
False
|
maxval
|
float or False
|
Maximum value for color normalization. If False, uses max(data). Default is False. |
False
|
return_mappable
|
bool
|
If True, also return ScalarMappable and Normalize objects for colorbar. Default is False. |
False
|
Returns:
| Type | Description |
|---|---|
list of tuple
|
List of RGBA color tuples, one for each data value, in same order as data. |
Examples:
>>> temps = [15, 20, 25, 30, 35]
>>> colors = get_colours(temps, colormap='coolwarm')
>>> # Use colors for scatter plot
>>> plt.scatter(x, y, c=colors)
Source code in dod2k_utilities/ut_plot.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | |
get_colours2(data, colormap='brewer_RdBu_11', minval=False, maxval=False)
generates colours from a colormap based on the data values (array or list) returns cols: list of colours, in same order as data
Source code in dod2k_utilities/ut_plot.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
plot_PCs(years_hom, eigenvectors, paleoData_zscores_hom, title='', name='', col='tab:blue')
Plot principal components and reconstructed time series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
years_hom
|
ndarray
|
Homogenised time axis. |
required |
eigenvectors
|
ndarray
|
Eigenvectors from PCA. |
required |
paleoData_zscores_hom
|
MaskedArray
|
Homogenised z-score data array of shape (n_records, n_years). |
required |
title
|
str
|
Title for plots. |
''
|
name
|
str
|
Name suffix for saving figures. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
PCs |
ndarray
|
Principal component time series. |
eigenvectors |
ndarray
|
Eigenvectors (EOF loadings) corresponding to PCs. |
Source code in dod2k_utilities/ut_plot.py
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | |
plot_count_proxy_by_archive_all(df, archive_proxy_count, archive_proxy_ticks, archive_colour)
Plot proxy counts by archive for all proxy types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing proxy and archive metadata (not directly used for plotting but retained for consistency). |
required |
archive_proxy_count
|
dict
|
Dictionary mapping proxy identifiers (e.g., |
required |
archive_proxy_ticks
|
list of str
|
Ordered list of proxy identifiers used for tick labels. |
required |
archive_colour
|
dict
|
Mapping from archive type to color. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
Figure containing the bar chart. |
Notes
All proxy types are included regardless of count. Bars are sorted in descending order of record count. Archive colors are derived from the archive prefix of each proxy identifier.
Source code in dod2k_utilities/ut_plot.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 | |
plot_count_proxy_by_archive_short(df, archive_proxy_count, archive_proxy_ticks, archive_colour)
Plot proxy counts by archive for proxy types exceeding a count threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing proxy and archive metadata (not directly used for plotting but retained for consistency). |
required |
archive_proxy_count
|
dict
|
Dictionary mapping proxy identifiers (e.g., |
required |
archive_proxy_ticks
|
list of str
|
Ordered list of proxy identifiers used for tick labels. |
required |
archive_colour
|
dict
|
Mapping from archive type to color. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
Figure containing the bar chart. |
Notes
Only proxy types with more than 10 records are included. Bars are sorted in descending order of count. Archive type is inferred from the prefix of each proxy identifier and used for color coding and legend construction.
Source code in dod2k_utilities/ut_plot.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
plot_coverage(df, archives_sorted, major_archives, other_archives, archive_colour, all=False, ysc='linear', return_data=False)
Plot temporal coverage of proxy records, optionally separated by archive type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing a |
required |
archives_sorted
|
list of str
|
Ordered list of archive types present in the dataset. |
required |
major_archives
|
list of str
|
Archive types treated as major and plotted individually. |
required |
other_archives
|
list of str
|
Archive types grouped under the |
required |
archive_colour
|
dict
|
Mapping from archive type (and |
required |
all
|
bool
|
If True, plot total coverage across all archives. |
False
|
ysc
|
(linear, log)
|
Y-axis scale. |
'linear'
|
return_data
|
bool
|
If True, return coverage arrays in addition to the figure. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Figure
|
Coverage plot figure. |
|
years |
(ndarray, optional)
|
Array of years spanning the full temporal range. |
coverage |
(ndarray, optional)
|
Total number of records available for each year. |
coverage_by_archive |
(dict, optional)
|
Dictionary mapping archive type to yearly coverage arrays. |
Notes
Coverage is defined as the number of records overlapping each year.
Archive types not classified as major are aggregated into an
'other' category.
Source code in dod2k_utilities/ut_plot.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
plot_coverage2(df, years, title='')
Plot the coverage of records over a range of years.
This function counts how many records in the DataFrame overlap with each year in the given range and produces a step plot showing total coverage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing 'year' data for each record. Each row should have 'miny' and 'maxy' indicating the start and end year of the record. |
required |
years
|
array - like
|
Array of years over which to compute coverage. |
required |
title
|
str
|
Title of the plot. Default is an empty string. |
''
|
Returns:
| Type | Description |
|---|---|
Figure
|
The matplotlib Figure object containing the plot. |
Source code in dod2k_utilities/ut_plot.py
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 | |
plot_coverage_analysis(df, years, key, col, title='')
Plot the coverage of records over a range of years.
This function counts how many records in the DataFrame overlap with each year in the given range and produces a step plot showing total coverage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing 'year' data for each record. Each row should have 'miny' and 'maxy' indicating the start and end year of the record. |
required |
years
|
array - like
|
Array of years over which to compute coverage. |
required |
title
|
str
|
Title of the plot. Default is an empty string. |
''
|
Returns:
| Type | Description |
|---|---|
Figure
|
The matplotlib Figure object containing the plot. |
Source code in dod2k_utilities/ut_plot.py
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 | |
plot_geo_archive_proxy(df, archive_colour, highlight_archives=[], marker='default', size='default', figsize='default')
Plot global distribution of proxy records grouped by archive and proxy type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing geographic coordinates and proxy metadata.
Must include |
required |
archive_colour
|
dict
|
Mapping from archive type to color. |
required |
highlight_archives
|
list of str
|
Archive types to emphasize using archive-specific marker cycling. |
[]
|
marker
|
str or sequence
|
Marker specification. If |
'default'
|
size
|
int or float
|
Marker size. If |
'default'
|
figsize
|
tuple or str
|
Figure size. If |
'default'
|
Returns:
| Type | Description |
|---|---|
Figure
|
Figure containing the global map. |
Notes
Marker shape distinguishes proxy types, while color denotes archive type. Highlighted archives reuse marker cycling per archive, whereas non-highlighted archives use a global marker index.
Source code in dod2k_utilities/ut_plot.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 | |
plot_geo_archive_proxy_short(df, archives_sorted, archive_proxy_count_short, archive_colour)
Plot geographical distribution of proxy records for major archive–proxy combinations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing geographic coordinates and proxy metadata.
Must include |
required |
archives_sorted
|
list of str
|
Ordered list of archive types to control plotting and legend order. |
required |
archive_proxy_count_short
|
dict
|
Nested dictionary mapping archive types to proxy counts, including
grouped |
required |
archive_colour
|
dict
|
Mapping from archive type to color. |
required |
Returns:
| Type | Description |
|---|---|
Figure
|
Figure containing the global map. |
Notes
Each archive–proxy combination is plotted with a distinct marker,
while colors indicate archive type. Proxies classified as 'other'
are plotted using masks that exclude explicitly listed proxy types.
Source code in dod2k_utilities/ut_plot.py
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | |
plot_length(df, title='', mincount=0, col='tab:blue')
Plot a histogram of lengths from a DataFrame.
This function bins the 'length' values in the DataFrame into predefined ranges,
optionally filters bins with counts below mincount, and displays a bar plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing a column named 'length' with numeric values. |
required |
title
|
str
|
Title of the plot. |
''
|
mincount
|
int
|
Minimum count threshold for bins. Bins with fewer counts than |
0
|
Returns:
| Type | Description |
|---|---|
None
|
The function displays a matplotlib bar plot and does not return any value. |
Source code in dod2k_utilities/ut_plot.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
plot_resolution(df, title='', mincount=0, col='tab:blue')
Plot a histogram of resolutions from a DataFrame.
This function counts the occurrences of each "resolution" in the DataFrame,
optionally merges bins with counts below mincount, and displays a bar plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame containing a column named 'resolution', where each entry is a list of integers representing resolution values. |
required |
title
|
str
|
Title of the plot. |
''
|
mincount
|
int
|
Minimum count threshold for individual resolution bins. Bins with fewer
counts than |
0
|
Returns:
| Type | Description |
|---|---|
None
|
The function displays a matplotlib bar plot and does not return any value. |
Source code in dod2k_utilities/ut_plot.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
shade_percentiles(x, y, color, ax, alpha=1, lu=False, zorder=None, lw=1, ups=[60, 70, 80, 90, 95], label=None)
Shade percentile ranges of an ensemble on a matplotlib axis.
Creates overlapping shaded regions showing different percentile ranges of an ensemble, useful for visualizing uncertainty in climate data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
Time or x-axis values (1D array of length n). |
required |
y
|
array - like
|
Ensemble data as m×n array where m is ensemble dimension and n is time dimension. |
required |
color
|
str or tuple
|
Color for shading (matplotlib color specification). |
required |
ax
|
Axes
|
Axes object to plot on. |
required |
alpha
|
float
|
Overall transparency multiplier (0-1). Default is 1. |
1
|
lu
|
bool
|
If True, plot dotted lines at 5th and 95th percentiles. Default is False. |
False
|
zorder
|
float
|
Drawing order for the shaded regions. Default is None. |
None
|
lw
|
float
|
Line width for percentile boundary lines if lu=True. Default is 1. |
1
|
ups
|
list of float
|
Upper percentiles to shade. Default is [60, 70, 80, 90, 95]. |
[60, 70, 80, 90, 95]
|
label
|
str
|
Label for legend (applied to outermost shading). Default is None. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
|
Notes
Shades symmetric percentile ranges with decreasing opacity: - Innermost: 40th-60th percentile (darkest) - Outermost: 5th-95th percentile (lightest)
Examples:
>>> fig, ax = plt.subplots()
>>> x = np.arange(100)
>>> y = np.random.randn(50, 100) # 50 ensemble members, 100 time steps
>>> shade_percentiles(x, y, 'blue', ax, lu=True, label='Ensemble')
Source code in dod2k_utilities/ut_plot.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |