API¶
single_bar
¶
Functions:
| Name | Description |
|---|---|
plot_one_group_bar_figure |
绘制单组柱状图,包含散点、误差条和统计显著性标记。 |
plot_one_group_violin_figure |
绘制单组小提琴图,可选散点叠加、渐变填色和统计显著性标注。 |
plot_one_group_bar_figure
¶
plot_one_group_bar_figure(data: Sequence[Sequence[Num] | ndarray], ax: Axes | None = None, labels_name: list[str] | None = None, colors: list[str] | None = None, edgecolor: str | None = None, gradient_color: bool = False, colors_start: list[str] | None = None, colors_end: list[str] | None = None, show_dots: bool = True, dots_color: list[list[str]] | None = None, width: Num = 0.5, color_alpha: Num = 1, dots_size: Num = 35, errorbar_type: str = 'sd', title_name: str = '', title_fontsize: Num = 12, title_pad: Num = 10, x_label_name: str = '', x_label_ha: str = 'center', x_label_fontsize: Num = 12, x_tick_fontsize: Num = 12, x_tick_rotation: Num = 0, y_label_name: str = '', y_label_fontsize: Num = 12, y_tick_fontsize: Num = 8, y_tick_rotation: Num = 0, y_lim: tuple[float, float] | None = None, statistic: bool = False, test_method: list[str] = ['ttest_ind'], p_list: list[float] | None = None, popmean: Num = 0, statistical_line_color: str = '0.5', asterisk_fontsize: Num = 10, asterisk_color: str = 'k', y_base: float | None = None, interval: float | None = None, ax_bottom_is_0: bool = False, y_max_tick_is_1: bool = False, math_text: bool = True, one_decimal_place: bool = False, percentage: bool = False) -> Axes | None
绘制单组柱状图,包含散点、误差条和统计显著性标记。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray | Sequence[Sequence[Num] | ndarray]
|
输入数据,可以是二维numpy数组或嵌套序列,每个子序列代表一个柱状图的数据点 |
required |
ax
|
Axes | None
|
matplotlib的坐标轴对象,如果为None则使用当前坐标轴. Defaults to None. |
None
|
labels_name
|
list[str] | None
|
柱状图的标签名称列表. Defaults to None. |
None
|
colors
|
list[str] | None
|
柱状图的颜色列表. Defaults to None. |
None
|
edgecolor
|
str | None
|
柱状图边缘颜色. Defaults to None. |
None
|
gradient_color
|
bool
|
是否使用渐变颜色填充柱状图. Defaults to False. |
False
|
colors_start
|
list[str] | None
|
渐变色的起始颜色列表. Defaults to None. |
None
|
colors_end
|
list[str] | None
|
渐变色的结束颜色列表. Defaults to None. |
None
|
show_dots
|
bool
|
是否显示散点. Defaults to True. |
True
|
dots_color
|
list[list[str]] | None
|
散点的颜色列表. Defaults to None. |
None
|
width
|
Num
|
柱状图的宽度. Defaults to 0.5. |
0.5
|
color_alpha
|
Num
|
柱状图颜色的透明度. Defaults to 1. |
1
|
dots_size
|
Num
|
散点的大小. Defaults to 35. |
35
|
errorbar_type
|
str
|
误差条类型,可选 "sd"(标准差) 或 "se"(标准误). Defaults to "sd". |
'sd'
|
title_name
|
str
|
图表标题. Defaults to "". |
''
|
title_fontsize
|
Num
|
标题字体大小. Defaults to 12. |
12
|
title_pad
|
Num
|
标题与图表的间距. Defaults to 10. |
10
|
x_label_name
|
str
|
X轴标签名称. Defaults to "". |
''
|
x_label_ha
|
str
|
X轴标签的水平对齐方式. Defaults to "center". |
'center'
|
x_label_fontsize
|
Num
|
X轴标签字体大小. Defaults to 12. |
12
|
x_tick_fontsize
|
Num
|
X轴刻度字体大小. Defaults to 12. |
12
|
x_tick_rotation
|
Num
|
X轴刻度旋转角度. Defaults to 0. |
0
|
y_label_name
|
str
|
Y轴标签名称. Defaults to "". |
''
|
y_label_fontsize
|
Num
|
Y轴标签字体大小. Defaults to 12. |
12
|
y_tick_fontsize
|
Num
|
Y轴刻度字体大小. Defaults to 8. |
8
|
y_tick_rotation
|
Num
|
Y轴刻度旋转角度. Defaults to 0. |
0
|
y_lim
|
tuple[Num, Num] | None
|
Y轴的范围限制. Defaults to None. |
None
|
statistic
|
bool
|
是否进行统计显著性分析. Defaults to False. |
False
|
test_method
|
list[str]
|
统计检验方法列表,包括
1. |
['ttest_ind']
|
p_list
|
list[float] | None
|
预计算的p值列表,用于显著性标记. Defaults to None. |
None
|
popmean
|
Num
|
单样本t检验的假设均值. Defaults to 0. |
0
|
statistical_line_color
|
str
|
显著性标记线的颜色. Defaults to "0.5". |
'0.5'
|
asterisk_fontsize
|
Num
|
显著性星号的字体大小. Defaults to 10. |
10
|
asterisk_color
|
str
|
显著性星号的颜色. Defaults to "k". |
'k'
|
y_base
|
float | None
|
显著性连线的起始Y轴位置(高度)。如果为None,则使用内部算法自动计算一个合适的位置。Defaults to None. |
None
|
interval
|
float | None
|
相邻显著性连线之间的垂直距离(Y轴增量)。如果为None,则使用内部算法根据图表范围和比较对数自动计算。Defaults to None. |
None
|
ax_bottom_is_0
|
bool
|
Y轴是否从0开始. Defaults to False. |
False
|
y_max_tick_is_1
|
bool
|
Y轴最大刻度是否限制为1. Defaults to False. |
False
|
math_text
|
bool
|
是否将Y轴显示为科学计数法格式. Defaults to True. |
True
|
one_decimal_place
|
bool
|
Y轴刻度是否只保留一位小数. Defaults to False. |
False
|
percentage
|
bool
|
是否将Y轴显示为百分比格式. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
当data数据格式无效时抛出 |
ValueError
|
当errorbar_type不是"sd"或"se"时抛出 |
Returns:
| Type | Description |
|---|---|
Axes | None
|
Axes | None: 返回matplotlib的坐标轴对象或None |
Source code in src/plotfig/single_bar.py
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 234 235 236 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 290 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 337 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 398 399 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 459 460 461 462 463 464 465 466 | |
plot_one_group_violin_figure
¶
plot_one_group_violin_figure(data: Sequence[list[float] | NDArray[float64]], ax: Axes | None = None, labels_name: list[str] | None = None, width: Num = 0.8, colors: list[str] | None = None, color_alpha: Num = 1, gradient_color: bool = False, colors_start: list[str] | None = None, colors_end: list[str] | None = None, show_dots: bool = False, dots_size: Num = 35, title_name: str = '', title_fontsize: Num = 12, title_pad: Num = 10, x_label_name: str = '', x_label_ha: str = 'center', x_label_fontsize: Num = 10, x_tick_fontsize: Num = 8, x_tick_rotation: Num = 0, y_label_name: str = '', y_label_fontsize: Num = 10, y_tick_fontsize: Num = 8, y_tick_rotation: Num = 0, y_lim: tuple[float, float] | None = None, statistic: bool = False, test_method: list[str] = ['ttest_ind'], popmean: Num = 0, p_list: list[float] | None = None, statistical_line_color: str = '0.5', asterisk_fontsize: Num = 10, asterisk_color: str = 'k', y_base: float | None = None, interval: float | None = None, ax_bottom_is_0: bool = False, y_max_tick_is_1: bool = False, math_text: bool = True, one_decimal_place: bool = False, percentage: bool = False) -> Axes | None
绘制单组小提琴图,可选散点叠加、渐变填色和统计显著性标注。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[list[float] | NDArray[float64]]
|
输入数据,可以是二维numpy数组或嵌套序列,每个子序列代表一个小提琴的数据点 |
required |
ax
|
Axes | None
|
matplotlib的坐标轴对象,如果为None则使用当前坐标轴. Defaults to None. |
None
|
labels_name
|
list[str] | None
|
小提琴图的标签名称列表. Defaults to None. |
None
|
width
|
Num
|
小提琴图的宽度. Defaults to 0.8. |
0.8
|
colors
|
list[str] | None
|
小提琴图的颜色列表. Defaults to None. |
None
|
color_alpha
|
Num
|
小提琴图颜色的透明度. Defaults to 1. |
1
|
gradient_color
|
bool
|
是否使用渐变颜色填充小提琴图. Defaults to False. |
False
|
colors_start
|
list[str] | None
|
渐变色的起始颜色列表. Defaults to None. |
None
|
colors_end
|
list[str] | None
|
渐变色的结束颜色列表. Defaults to None. |
None
|
show_dots
|
bool
|
是否显示散点. Defaults to False. |
False
|
dots_size
|
Num
|
散点的大小. Defaults to 35. |
35
|
title_name
|
str
|
图表标题. Defaults to "". |
''
|
title_fontsize
|
Num
|
标题字体大小. Defaults to 12. |
12
|
title_pad
|
Num
|
标题与图表的间距. Defaults to 10. |
10
|
x_label_name
|
str
|
X轴标签名称. Defaults to "". |
''
|
x_label_ha
|
str
|
X轴标签的水平对齐方式. Defaults to "center". |
'center'
|
x_label_fontsize
|
Num
|
X轴标签字体大小. Defaults to 10. |
10
|
x_tick_fontsize
|
Num
|
X轴刻度字体大小. Defaults to 8. |
8
|
x_tick_rotation
|
Num
|
X轴刻度旋转角度. Defaults to 0. |
0
|
y_label_name
|
str
|
Y轴标签名称. Defaults to "". |
''
|
y_label_fontsize
|
Num
|
Y轴标签字体大小. Defaults to 10. |
10
|
y_tick_fontsize
|
Num
|
Y轴刻度字体大小. Defaults to 8. |
8
|
y_tick_rotation
|
Num
|
Y轴刻度旋转角度. Defaults to 0. |
0
|
y_lim
|
tuple[Num, Num] | None
|
Y轴的范围限制. Defaults to None. |
None
|
statistic
|
bool
|
是否进行统计显著性分析. Defaults to False. |
False
|
test_method
|
list[str]
|
统计检验方法列表. Defaults to ["ttest_ind"]. |
['ttest_ind']
|
popmean
|
Num
|
单样本t检验的假设均值. Defaults to 0. |
0
|
p_list
|
list[float] | None
|
预计算的p值列表,用于显著性标记. Defaults to None. |
None
|
statistical_line_color
|
str
|
显著性标记线的颜色. Defaults to "0.5". |
'0.5'
|
asterisk_fontsize
|
Num
|
显著性星号的字体大小. Defaults to 10. |
10
|
asterisk_color
|
str
|
显著性星号的颜色. Defaults to "k". |
'k'
|
y_base
|
float | None
|
显著性连线的起始Y轴位置(高度)。如果为None,则使用内部算法自动计算一个合适的位置。Defaults to None. |
None
|
interval
|
float | None
|
相邻显著性连线之间的垂直距离(Y轴增量)。如果为None,则使用内部算法根据图表范围和比较对数自动计算。Defaults to None. |
None
|
ax_bottom_is_0
|
bool
|
Y轴是否从0开始. Defaults to False. |
False
|
y_max_tick_is_1
|
bool
|
Y轴最大刻度是否限制为1. Defaults to False. |
False
|
math_text
|
bool
|
是否将Y轴显示为科学计数法格式. Defaults to True. |
True
|
one_decimal_place
|
bool
|
Y轴刻度是否只保留一位小数. Defaults to False. |
False
|
percentage
|
bool
|
是否将Y轴显示为百分比格式. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
当data数据格式无效时抛出 |
Returns:
| Type | Description |
|---|---|
Axes | None
|
Axes | None: 返回matplotlib的坐标轴对象或None |
Source code in src/plotfig/single_bar.py
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 540 541 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 626 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 713 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 756 | |
multi_bars
¶
Functions:
| Name | Description |
|---|---|
plot_multi_group_bar_figure |
绘制多组分组条形图,支持误差线、散点叠加和统计显著性标注。 |
plot_multi_group_bar_figure
¶
plot_multi_group_bar_figure(data: Sequence[Sequence[Sequence[float]]], ax: Axes | None = None, group_labels: list[str] | None = None, bar_labels: list[str] | None = None, bar_width: Num = 0.2, bar_gap: Num = 0.1, bar_color: list[str] | None = None, errorbar_type: str = 'sd', dots_color: str = 'gray', dots_size: int = 35, legend: bool = True, legend_position: tuple[Num, Num] = (1.2, 1), title_name: str = '', title_fontsize=12, title_pad=10, x_label_name: str = '', x_label_ha='center', x_label_fontsize=10, x_tick_fontsize=8, x_tick_rotation=0, y_label_name: str = '', y_label_fontsize=10, y_tick_fontsize=8, y_tick_rotation=0, y_lim: tuple[float, float] | None = None, statistic: bool = False, test_method: str = 'external', p_list: list[list[Num]] | None = None, line_color='0.5', asterisk_fontsize=10, asterisk_color='k', y_base: float | None = None, interval: float | None = None, ax_bottom_is_0: bool = False, y_max_tick_is_1: bool = False, math_text: bool = True, one_decimal_place: bool = False, percentage: bool = False) -> Axes
绘制多组分组条形图,支持误差线、散点叠加和统计显著性标注。
该函数用于可视化多组数据的比较,每组包含多个柱子,每个柱子显示均值、误差线 和原始数据点。特别适用于认知神经科学中的组间比较分析。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[Sequence[Sequence[float]]]
|
三层嵌套的数据结构 - 第一层:组 (groups) - 第二层:每组内的柱子 (bars),所有组的柱子数量必须一致 - 第三层:每个柱子内的数据点 (points),数量可以不同 |
required |
ax
|
Axes | None
|
matplotlib 的 Axes 对象。如果为 None,使用当前活动的 Axes。 |
None
|
group_labels
|
list[str] | None
|
每个组的标签。如果为 None,自动生成 "Group 1", "Group 2" 等。 |
None
|
bar_labels
|
list[str] | None
|
每个柱子的标签,用于图例。如果为 None,自动生成 "Bar 1", "Bar 2" 等。 |
None
|
bar_width
|
Num
|
柱子的宽度。默认为 0.2。 |
0.2
|
bar_gap
|
Num
|
同一组内柱子之间的间隔。默认为 0.1。 |
0.1
|
bar_color
|
list[str] | None
|
每个柱子的颜色列表。如果为 None,所有柱子使用灰色。 |
None
|
errorbar_type
|
str
|
误差线类型,'sd' 表示标准差,'se' 表示标准误。默认为 'sd'。 |
'sd'
|
dots_color
|
str
|
散点的颜色。默认为 'gray'。 |
'gray'
|
dots_size
|
int
|
散点的大小。默认为 35。 |
35
|
legend
|
bool
|
是否显示图例。默认为 True。 |
True
|
legend_position
|
tuple[Num, Num]
|
图例位置,使用 bbox_to_anchor 坐标。默认为 (1.2, 1)。 |
(1.2, 1)
|
title_name
|
str
|
图表标题。默认为空字符串。 |
''
|
title_fontsize
|
int
|
标题字体大小。默认为 12。 |
12
|
title_pad
|
int
|
标题与图表的间距。默认为 10。 |
10
|
x_label_name
|
str
|
x 轴标签文本。默认为空字符串。 |
''
|
x_label_ha
|
str
|
x 轴刻度标签的水平对齐方式。默认为 'center'。 |
'center'
|
x_label_fontsize
|
int
|
x 轴标签字体大小。默认为 10。 |
10
|
x_tick_fontsize
|
int
|
x 轴刻度字体大小。默认为 8。 |
8
|
x_tick_rotation
|
int
|
x 轴刻度旋转角度。默认为 0。 |
0
|
y_label_name
|
str
|
y 轴标签文本。默认为空字符串。 |
''
|
y_label_fontsize
|
int
|
y 轴标签字体大小。默认为 10。 |
10
|
y_tick_fontsize
|
int
|
y 轴刻度字体大小。默认为 8。 |
8
|
y_tick_rotation
|
int
|
y 轴刻度旋转角度。默认为 0。 |
0
|
y_lim
|
tuple[float, float] | None
|
手动指定的 y 轴范围 (y_min, y_max)。 如果为 None,根据数据自动计算。 |
None
|
statistic
|
bool
|
是否添加统计显著性标注。默认为 False。 |
False
|
test_method
|
str
|
统计检验方法。当前仅支持 'external'(使用外部提供的 p 值)。 |
'external'
|
p_list
|
list[list[Num]] | None
|
外部提供的 p 值列表。 结构为 [组1的p值列表, 组2的p值列表, ...],每个组的 p 值列表对应该组内所有两两比较。 当 statistic=True 且 test_method='external' 时必须提供。 |
None
|
line_color
|
str
|
显著性标注连线的颜色。默认为 '0.5'(中灰色)。 |
'0.5'
|
asterisk_fontsize
|
int
|
显著性星号的字体大小。默认为 10。 |
10
|
asterisk_color
|
str
|
显著性星号的颜色。默认为 'k'(黑色)。 |
'k'
|
y_base
|
float | None
|
显著性标注的起始 y 坐标。如果为 None,自动计算为数据最大值。 |
None
|
interval
|
float | None
|
多个显著性标注之间的垂直间隔。 如果为 None,自动计算为 (y_max - 数据最大值) / (比较数量 + 1)。 |
None
|
ax_bottom_is_0
|
bool
|
是否将 y 轴底部固定为 0。默认为 False。 |
False
|
y_max_tick_is_1
|
bool
|
是否将最大刻度限制为 1。默认为 False。 |
False
|
math_text
|
bool
|
是否使用科学计数法格式。默认为 True。 |
True
|
one_decimal_place
|
bool
|
是否将刻度格式化为一位小数。默认为 False。 |
False
|
percentage
|
bool
|
是否将刻度格式化为百分比形式。默认为 False。 |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Axes |
Axes
|
包含绘制内容的 matplotlib Axes 对象。 |
Raises:
| Type | Description |
|---|---|
ValueError
|
当 data 不是三层嵌套结构时抛出。 |
ValueError
|
当所有组的柱子数量不一致时抛出。 |
ValueError
|
当 errorbar_type 不是 'sd' 或 'se' 时抛出。 |
ValueError
|
当 statistic=True 且 test_method='external' 但 p_list 为 None 时抛出。 |
Examples:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from plotfig import plot_multi_group_bar_figure
>>>
>>> # 创建示例数据:2 组,每组 3 个柱子
>>> data = [
... [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
... [[2, 3, 4], [5, 6, 7], [8, 9, 10]]
... ]
>>>
>>> # 绘制基本图表
>>> fig, ax = plt.subplots(figsize=(8, 6))
>>> ax = plot_multi_group_bar_figure(
... data,
... ax=ax,
... group_labels=['Control', 'Treatment'],
... bar_labels=['Condition A', 'Condition B', 'Condition C'],
... title_name='Multi-Group Comparison'
... )
>>> fig.show()
Notes
- 显著性星号规则:* (p≤0.05), ** (p≤0.01), *** (p≤0.001)
Source code in src/plotfig/multi_bars.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 234 235 236 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 290 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 | |
correlation
¶
Functions:
| Name | Description |
|---|---|
plot_correlation_figure |
绘制两个数据集之间的相关性图,支持线性回归、置信区间和统计方法(Spearman 或 Pearson)。 |
plot_correlation_figure
¶
plot_correlation_figure(data1: list[Num] | ndarray, data2: list[Num] | ndarray, ax: Axes | None = None, stats_method: str = 'spearman', ci: bool = False, ci_color: str = 'gray', dots_color: str | list[str] = 'steelblue', dots_size: int | float = 10, line_color: str = 'r', title_name: str = '', title_fontsize: int = 12, title_pad: int = 10, x_label_name: str = '', x_label_fontsize: int = 10, x_tick_fontsize: int = 8, x_tick_rotation: int = 0, x_major_locator: float | None = None, x_max_tick_to_value: float | None = None, x_format: str = 'normal', y_label_name: str = '', y_label_fontsize: int = 10, y_tick_fontsize: int = 8, y_tick_rotation: int = 0, y_major_locator: float | None = None, y_max_tick_to_value: float | None = None, y_format: str = 'sci', asterisk_fontsize: int = 10, show_p_value: bool = False, hexbin: bool = False, hexbin_cmap: LinearSegmentedColormap | None = None, hexbin_gridsize: int = 50, xlim: list[Num] | tuple[Num, Num] | None = None, ylim: list[Num] | tuple[Num, Num] | None = None) -> Axes
绘制两个数据集之间的相关性图,支持线性回归、置信区间和统计方法(Spearman 或 Pearson)。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data1
|
list[Num] | ndarray
|
第一个数据集,可以是整数或浮点数列表或数组。 |
required |
data2
|
list[Num] | ndarray
|
第二个数据集,可以是整数或浮点数列表或数组。 |
required |
ax
|
Axes | None
|
matplotlib 的 Axes 对象,用于绘图。默认为 None,使用当前 Axes。 |
None
|
stats_method
|
str
|
相关性统计方法,支持 "spearman" 和 "pearson"。默认为 "spearman"。 |
'spearman'
|
ci
|
bool
|
是否绘制置信区间带。默认为 False。 |
False
|
ci_color
|
str
|
置信区间带颜色。默认为 "salmon"。 |
'gray'
|
dots_color
|
str
|
散点的颜色。默认为 "steelblue"。 |
'steelblue'
|
dots_size
|
int | float
|
散点的大小。默认为 1。 |
10
|
line_color
|
str
|
回归线的颜色。默认为 "r"(红色)。 |
'r'
|
title_name
|
str
|
图形标题。默认为空字符串。 |
''
|
title_fontsize
|
int
|
标题字体大小。默认为 10。 |
12
|
title_pad
|
int
|
标题与图形之间的间距。默认为 10。 |
10
|
x_label_name
|
str
|
X 轴标签名称。默认为空字符串。 |
''
|
x_label_fontsize
|
int
|
X 轴标签字体大小。默认为 10。 |
10
|
x_tick_fontsize
|
int
|
X 轴刻度标签字体大小。默认为 10。 |
8
|
x_tick_rotation
|
int
|
X 轴刻度标签旋转角度。默认为 0。 |
0
|
x_major_locator
|
float | None
|
设置 X 轴主刻度间隔。默认为 None。 |
None
|
x_max_tick_to_value
|
float | None
|
设置 X 轴最大显示刻度值。默认为 None。 |
None
|
x_format
|
str
|
X 轴格式化方式,支持 "normal", "sci", "1f", "percent"。默认为 "normal"。 |
'normal'
|
y_label_name
|
str
|
Y 轴标签名称。默认为空字符串。 |
''
|
y_label_fontsize
|
int
|
Y 轴标签字体大小。默认为 10。 |
10
|
y_tick_fontsize
|
int
|
Y 轴刻度标签字体大小。默认为 10。 |
8
|
y_tick_rotation
|
int
|
Y 轴刻度标签旋转角度。默认为 0。 |
0
|
y_major_locator
|
float | None
|
设置 Y 轴主刻度间隔。默认为 None。 |
None
|
y_max_tick_to_value
|
float | None
|
设置 Y 轴最大显示刻度值。默认为 None。 |
None
|
y_format
|
str
|
Y 轴格式化方式,支持 "normal", "sci", "1f", "percent"。默认为 "normal"。 |
'sci'
|
asterisk_fontsize
|
int
|
显著性星号字体大小。默认为 10。 |
10
|
show_p_value
|
bool
|
是否显示 p 值。默认为 True。 |
False
|
hexbin
|
bool
|
是否使用六边形箱图。默认为 False。 |
False
|
hexbin_cmap
|
LinearSegmentedColormap | None
|
六边形箱图的颜色映射。默认为 None。 |
None
|
hexbin_gridsize
|
int
|
六边形箱图的网格大小。默认为 50。 |
50
|
xlim
|
list[Num] | tuple[Num, Num] | None
|
X 轴范围限制。默认为 None。 |
None
|
ylim
|
list[Num] | tuple[Num, Num] | None
|
Y 轴范围限制。默认为 None。 |
None
|
Returns:
| Type | Description |
|---|---|
Axes
|
None |
Source code in src/plotfig/correlation.py
20 21 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 87 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 234 235 236 237 238 239 | |
matrix
¶
Functions:
| Name | Description |
|---|---|
plot_matrix_figure |
将矩阵绘制为热图,可选显示标签、颜色条和标题。 |
plot_matrix_figure
¶
plot_matrix_figure(data: ndarray, ax: Axes | None = None, row_labels_name: Sequence[str] | None = None, col_labels_name: Sequence[str] | None = None, cmap: str = 'bwr', vmin: Num | None = None, vmax: Num | None = None, aspect: str = 'equal', colorbar: bool = True, colorbar_label_name: str = '', colorbar_pad: Num = 0.1, colorbar_label_fontsize: Num = 10, colorbar_tick_fontsize: Num = 10, colorbar_tick_rotation: Num = 0, row_labels_fontsize: Num = 10, col_labels_fontsize: Num = 10, x_rotation: Num = 60, title_name: str = '', title_fontsize: Num = 15, title_pad: Num = 20, diag_border: bool = False, xlabel: str | None = None, ylabel: str | None = None, **imshow_kwargs: Any) -> Axes
将矩阵绘制为热图,可选显示标签、颜色条和标题。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
形状为 (N, M) 的二维数组,用于显示矩阵。 |
required |
ax
|
Axes | None
|
要绘图的 Matplotlib 坐标轴。如果为 None,则使用当前坐标轴。 |
None
|
row_labels_name
|
Sequence[str] | None
|
行标签列表。 |
None
|
col_labels_name
|
Sequence[str] | None
|
列标签列表。 |
None
|
cmap
|
str
|
矩阵使用的颜色映射。 |
'bwr'
|
vmin
|
Num | None
|
颜色缩放的最小值,默认使用 data.min()。 |
None
|
vmax
|
Num | None
|
颜色缩放的最大值,默认使用 data.max()。 |
None
|
aspect
|
str
|
图像的纵横比,通常为 "equal" 或 "auto"。 |
'equal'
|
colorbar
|
bool
|
是否显示颜色条。 |
True
|
colorbar_label_name
|
str
|
颜色条的标签。 |
''
|
colorbar_pad
|
Num
|
颜色条与矩阵之间的间距。 |
0.1
|
colorbar_label_fontsize
|
Num
|
颜色条标签的字体大小。 |
10
|
colorbar_tick_fontsize
|
Num
|
颜色条刻度的字体大小。 |
10
|
colorbar_tick_rotation
|
Num
|
颜色条刻度标签的旋转角度。 |
0
|
row_labels_fontsize
|
Num
|
行标签的字体大小。 |
10
|
col_labels_fontsize
|
Num
|
列标签的字体大小。 |
10
|
x_rotation
|
Num
|
x 轴(列)标签的旋转角度。 |
60
|
title_name
|
Num
|
图表标题。 |
''
|
title_fontsize
|
Num
|
标题的字体大小。 |
15
|
title_pad
|
Num
|
标题上方的间距。 |
20
|
diag_border
|
bool
|
是否绘制对角线单元格边框。 |
False
|
xlabel
|
str | None
|
X轴的整体标签名称。 |
None
|
ylabel
|
str | None
|
Y轴的整体标签名称。 |
None
|
**imshow_kwargs
|
Any
|
传递给 |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
Axes |
Axes
|
绘图的坐标轴对象。 |
Source code in src/plotfig/matrix.py
14 15 16 17 18 19 20 21 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 87 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 | |
brain_surface
¶
Functions:
| Name | Description |
|---|---|
plot_brain_surface_figure |
在大脑皮层表面绘制数值数据的函数。 |
plot_brain_surface_figure
¶
plot_brain_surface_figure(data: Mapping[str, Num], species: str = 'human', atlas: str = 'glasser', surf: str = 'veryinflated', ax: Axes | None = None, vmin: Num | None = None, vmax: Num | None = None, cmap: str = 'viridis', colorbar: bool = True, colorbar_location: str = 'right', colorbar_label_name: str = '', colorbar_label_rotation: int = 0, colorbar_decimals: int = 1, colorbar_fontsize: int = 8, colorbar_nticks: int = 2, colorbar_shrink: float = 0.15, colorbar_aspect: int = 8, colorbar_draw_border: bool = False, title_name: str = '', title_fontsize: int = 12, as_outline: bool = False) -> Axes
在大脑皮层表面绘制数值数据的函数。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, float]
|
包含脑区名称和对应数值的字典,键为脑区名称(如"lh_bankssts"),值为数值 |
required |
species
|
str
|
物种名称,支持"human"、"chimpanzee"、"macaque". Defaults to "human". |
'human'
|
atlas
|
str
|
脑图集名称,根据物种不同可选不同图集。人上包括"glasser"、"bna",黑猩猩上包括"bna",猕猴上包括"charm5"、"charm6"、"bna"以及"d99". Defaults to "glasser". |
'glasser'
|
surf
|
str
|
大脑皮层表面类型,如"inflated"、"veryinflated"、"midthickness"等. Defaults to "veryinflated". |
'veryinflated'
|
ax
|
Axes | None
|
matplotlib的坐标轴对象,如果为None则使用当前坐标轴. Defaults to None. |
None
|
vmin
|
Num | None
|
颜色映射的最小值,None表示使用数据中的最小值. Defaults to None. |
None
|
vmax
|
Num | None
|
颜色映射的最大值,None表示使用数据中的最大值. Defaults to None. |
None
|
cmap
|
str
|
颜色映射方案,如"viridis"、"Blues"、"Reds"等. Defaults to "viridis". |
'viridis'
|
colorbar
|
bool
|
是否显示颜色条. Defaults to True. |
True
|
colorbar_location
|
str
|
颜色条位置,可选"left"、"right"、"top"、"bottom". Defaults to "right". |
'right'
|
colorbar_label_name
|
str
|
颜色条标签名称. Defaults to "". |
''
|
colorbar_label_rotation
|
int
|
颜色条标签旋转角度. Defaults to 0. |
0
|
colorbar_decimals
|
int
|
颜色条刻度标签的小数位数. Defaults to 1. |
1
|
colorbar_fontsize
|
int
|
颜色条字体大小. Defaults to 8. |
8
|
colorbar_nticks
|
int
|
颜色条刻度数量. Defaults to 2. |
2
|
colorbar_shrink
|
float
|
颜色条收缩比例. Defaults to 0.15. |
0.15
|
colorbar_aspect
|
int
|
颜色条宽高比. Defaults to 8. |
8
|
colorbar_draw_border
|
bool
|
是否绘制颜色条边框. Defaults to False. |
False
|
title_name
|
str
|
图形标题. Defaults to "". |
''
|
title_fontsize
|
int
|
标题字体大小. Defaults to 12. |
12
|
as_outline
|
bool
|
是否以轮廓线形式显示. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
当指定的物种不支持时抛出 |
ValueError
|
当指定的图集不支持时抛出 |
ValueError
|
当数据为空时抛出 |
ValueError
|
当vmin大于vmax时抛出 |
Returns:
| Name | Type | Description |
|---|---|---|
Axes |
Axes
|
包含绘制图像的matplotlib坐标轴对象 |
Source code in src/plotfig/brain_surface.py
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 87 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
circos
¶
Functions:
| Name | Description |
|---|---|
plot_circos_figure |
绘制脑连接组的环形图(Circos plot)。 |
plot_circos_figure
¶
plot_circos_figure(connectome: NDArray, ax: Axes | None = None, symmetric: bool = True, node_names: list[str] | None = None, node_colors: list[str] | None = None, node_space: float = 0.0, node_label_fontsize: int = 10, node_label_orientation: Literal['vertical', 'horizontal'] = 'horizontal', vmin: float | None = None, vmax: float | None = None, cmap: str | None = None, edge_color: str = 'red', edge_alpha: float = 1.0, colorbar: bool = True, colorbar_orientation: Literal['vertical', 'horizontal'] = 'vertical', colorbar_label: str = '') -> Figure | PolarAxes
绘制脑连接组的环形图(Circos plot)。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connectome
|
NDArray
|
脑连接矩阵,必须为对称方阵。形状为(n, n),其中n为脑区数量 |
required |
ax
|
Axes | None
|
matplotlib的极坐标轴对象,如果提供则在此轴上绘图。默认为None |
None
|
symmetric
|
bool
|
是否为对称布局(用于左右脑半球数据)。默认为True |
True
|
node_names
|
list[str] | None
|
脑区名称列表,长度应与connectome的维度一致。默认为None时自动生成"Node_1", "Node_2"...格式的名称 |
None
|
node_colors
|
list[str] | None
|
脑区颜色列表,长度应与脑区数量一致。默认为None时自动生成颜色 |
None
|
node_space
|
float
|
脑区间间隔角度(度)。默认为0.0 |
0.0
|
node_label_fontsize
|
int
|
脑区标签字体大小。默认为10 |
10
|
node_label_orientation
|
Literal['vertical', 'horizontal']
|
脑区标签方向。默认为"horizontal" |
'horizontal'
|
vmin
|
float | None
|
连接强度颜色映射的最小值。默认为None时根据数据自动确定 |
None
|
vmax
|
float | None
|
连接强度颜色映射的最大值。默认为None时根据数据自动确定 |
None
|
cmap
|
str | None
|
颜色映射表名称。默认为None时根据edge_color生成 |
None
|
edge_color
|
str
|
连线颜色,当cmap为None时使用此颜色生成颜色映射。默认为"red" |
'red'
|
edge_alpha
|
float
|
连线透明度,范围0-1。默认为1.0(不透明) |
1.0
|
colorbar
|
bool
|
是否显示颜色条。默认为True |
True
|
colorbar_orientation
|
Literal['vertical', 'horizontal']
|
颜色条方向。默认为"vertical" |
'vertical'
|
colorbar_label
|
str
|
颜色条标签文本。默认为空字符串 |
''
|
Raises:
| Type | Description |
|---|---|
ValueError
|
当connectome不是对称矩阵时抛出 |
ValueError
|
当vmin大于vmax时抛出 |
TypeError
|
当提供的ax不是PolarAxes类型时抛出 |
Returns:
| Type | Description |
|---|---|
Figure | PolarAxes
|
Figure | Axes: 如果ax为None则返回Figure对象,否则返回Axes对象 |
Source code in src/plotfig/circos.py
86 87 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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 234 235 236 237 238 239 240 241 | |
brain_connection
¶
Functions:
| Name | Description |
|---|---|
batch_crop_images |
批量裁剪指定目录下的图像文件。 |
create_gif_from_images |
从指定文件夹中的图片生成 GIF 文件。 |
plot_brain_connection_figure |
绘制大脑连接图,保存在指定的html文件中。 |
save_brain_connection_frames |
生成不同角度的静态图片帧,可用于制作旋转大脑连接图的 GIF。 |
batch_crop_images
¶
batch_crop_images(directory_path: Path, suffix: str = '_cropped', left_frac: float = 0.25, top_frac: float = 0.25, right_frac: float = 0.25, bottom_frac: float = 0.25)
批量裁剪指定目录下的图像文件。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory_path
|
Path
|
图像文件所在的目录路径。 |
required |
suffix
|
str
|
新文件名后缀。默认为 "_cropped"。 |
'_cropped'
|
left_frac
|
float
|
左侧裁剪比例(0-1)。默认为 0.2。 |
0.25
|
top_frac
|
float
|
上侧裁剪比例(0-1)。默认为 0.15。 |
0.25
|
right_frac
|
float
|
右侧裁剪比例(0-1)。默认为 0.2。 |
0.25
|
bottom_frac
|
float
|
下侧裁剪比例(0-1)。默认为 0.15。 |
0.25
|
Notes
支持常见图像格式 (PNG, JPG, JPEG, BMP, TIFF)。 裁剪后的图像将保存在原目录中,并添加指定后缀,原图像保持不变。 所有裁剪均基于图像尺寸的百分比计算,无绝对像素值。
Source code in src/plotfig/brain_connection.py
create_gif_from_images
¶
create_gif_from_images(folder_path: str | Path, output_name: str = 'output.gif', fps: int = 10) -> None
从指定文件夹中的图片生成 GIF 文件。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path
|
str | Path
|
图片所在文件夹路径 |
required |
output_name
|
str
|
输出 GIF 文件名,默认为 "output.gif" |
'output.gif'
|
fps
|
int
|
GIF 帧率,默认为 10 |
10
|
Source code in src/plotfig/brain_connection.py
plot_brain_connection_figure
¶
plot_brain_connection_figure(connectome: NDArray, lh_surfgii_file: str | Path, rh_surfgii_file: str | Path, niigz_file: str | Path, output_file: str | Path | None = None, show_all_nodes: bool = False, nodes_size: Sequence[Num] | NDArray | None = None, nodes_name: list[str] | None = None, nodes_color: list[str] | None = None, scale_method: Literal['', 'width', 'color', 'width_color', 'color_width'] = '', line_width: Num = 10, line_color: str = 'red') -> Figure
绘制大脑连接图,保存在指定的html文件中。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connectome
|
NDArray
|
大脑连接矩阵,形状为 (n, n),其中 n 是脑区数量。 矩阵中的值表示脑区之间的连接强度,正值表示正相关连接,负值表示负相关连接,0表示无连接。 |
required |
lh_surfgii_file
|
str | Path
|
左半脑表面几何文件路径 (.surf.gii 格式),用于绘制左半脑表面 |
required |
rh_surfgii_file
|
str | Path
|
右半脑表面几何文件路径 (.surf.gii 格式),用于绘制右半脑表面 |
required |
niigz_file
|
str | Path
|
NIfTI格式的脑区图谱文件路径 (.nii.gz 格式),用于定位脑区节点的三维坐标 |
required |
output_file
|
str | Path | None
|
输出HTML文件路径。如果未指定,则使用当前时间戳生成文件名。默认为None |
None
|
show_all_nodes
|
bool
|
是否显示所有脑区节点。如果为False,则只显示有连接的节点。默认为False |
False
|
nodes_size
|
Sequence[Num] | NDArray | None
|
每个节点的大小,长度应与脑区数量一致。默认为None,即所有节点大小为5 |
None
|
nodes_name
|
list[str] | None
|
每个节点的名称标签,长度应与脑区数量一致。默认为None,即不显示名称 |
None
|
nodes_color
|
list[str] | None
|
每个节点的颜色,长度应与脑区数量一致。默认为None,即所有节点为白色 |
None
|
scale_method
|
Literal['', 'width', 'color', 'width_color', 'color_width']
|
连接线的缩放方法: - "" : 所有连接线宽度和颜色固定 - "width" : 根据连接强度调整线宽,正连接为红色,负连接为蓝色 - "color" : 根据连接强度调整颜色(使用蓝白红颜色映射),线宽固定 - "width_color" or "color_width" : 同时根据连接强度调整线宽和颜色 默认为 "" |
''
|
line_width
|
Num
|
连接线的基本宽度。当scale_method包含"width"时,此值作为最大宽度参考。默认为10 |
10
|
line_color
|
str
|
连接线的基本颜色。当scale_method不包含"color"时生效。默认为"#ff0000"(红色) |
'red'
|
Returns:
| Type | Description |
|---|---|
Figure
|
go.Figure: Plotly图形对象,包含绘制的大脑连接图 |
Source code in src/plotfig/brain_connection.py
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 234 235 236 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 290 291 292 293 294 295 296 297 298 | |
save_brain_connection_frames
¶
生成不同角度的静态图片帧,可用于制作旋转大脑连接图的 GIF。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fig
|
Figure
|
Plotly 的 Figure 对象,包含大脑表面和连接图。 |
required |
output_dir
|
str
|
图片保存的文件夹路径,若文件夹不存在则自动创建。 |
required |
n_frames
|
int
|
旋转帧的数量。默认 36,即每 10 度一帧。 |
36
|
Source code in src/plotfig/brain_connection.py
utils
¶
Modules:
| Name | Description |
|---|---|
bar |
|
color |
|
matrix |
|
Functions:
| Name | Description |
|---|---|
gen_hex_colors |
生成指定数量的随机十六进制颜色代码。 |
gen_symmetric_matrix |
生成一个对称方阵,可以指定元素范围和稀疏度。 |
gen_white_to_color_cmap |
生成从白色到指定颜色的线性渐变色图。 |
is_symmetric_square |
判断一个矩阵是否为对称方阵。 |
value_to_hex |
将数值通过色图和归一化映射为十六进制颜色字符串。 |
gen_hex_colors
¶
生成指定数量的随机十六进制颜色代码。
该函数使用 NumPy 的随机数生成器创建随机 RGB 颜色值,并转换为十六进制格式。 通过固定随机种子确保结果可重复,适用于需要一致配色方案的科学可视化。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
需要生成的颜色数量,必须为正整数。 |
required |
seed
|
int
|
随机种子,用于确保结果可重复。默认为 42。 |
42
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: 包含 n 个十六进制颜色字符串的列表,格式为 "#RRGGBB"。 |
Examples:
>>> # 在绘图中使用
>>> import matplotlib.pyplot as plt
>>> colors = gen_hex_colors(10)
>>> for i, color in enumerate(colors):
... plt.bar(i, i+1, color=color)
Notes
- RGB 值范围为 [0, 255]
- 相同的 n 和 seed 参数总是生成相同的颜色序列
- 生成的颜色是完全随机的,可能包含对比度较低的颜色
Source code in src/plotfig/utils/color.py
gen_symmetric_matrix
¶
生成一个对称方阵,可以指定元素范围和稀疏度。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
方阵的维度。 |
required |
mode
|
str
|
元素类型,"nonneg" 表示非负,"all" 表示可为负。默认为 "nonneg"。 |
'nonneg'
|
sparsity
|
float
|
稀疏度,取值范围 [0, 1],1 表示全密集,0 表示全零。默认为 1.0。 |
1.0
|
seed
|
int
|
随机种子,默认为 42。 |
42
|
Raises:
| Type | Description |
|---|---|
ValueError
|
如果 mode 不是 "nonneg" 或 "all"。 |
Returns:
| Name | Type | Description |
|---|---|---|
NDArray |
NDArray
|
生成的对称方阵。 |
Source code in src/plotfig/utils/matrix.py
gen_white_to_color_cmap
¶
gen_white_to_color_cmap(color: str = 'red') -> Colormap
生成从白色到指定颜色的线性渐变色图。
该函数创建一个双色线性渐变色图,从白色(最小值)平滑过渡到指定颜色(最大值)。 适用于热力图、相关矩阵、脑连接图等需要表示连续数值强度的可视化场景。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
color
|
str
|
渐变的目标颜色,支持 matplotlib 颜色名称(如 "red", "blue")、 十六进制格式(如 "#FF0000")或 RGB 元组。默认为 "red"。 |
'red'
|
Returns:
| Name | Type | Description |
|---|---|---|
Colormap |
Colormap
|
matplotlib 的线性渐变色图对象,可直接用于绘图函数。 |
Examples:
>>> # 生成红色渐变色图
>>> cmap = gen_cmap("red")
>>>
>>> # 在热力图中使用
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> data = np.random.rand(10, 10)
>>> plt.imshow(data, cmap=gen_cmap("blue"))
>>> plt.colorbar()
>>>
>>> # 使用十六进制颜色
>>> cmap = gen_cmap("#FF5733")
>>>
>>> # 在散点图中使用
>>> x = np.random.rand(100)
>>> y = np.random.rand(100)
>>> c = np.random.rand(100)
>>> plt.scatter(x, y, c=c, cmap=gen_cmap("purple"))
Notes
- 色图名称固定为 "white_to_color"
- 渐变是线性的,白色对应数值范围的最小值,指定颜色对应最大值
- 支持所有 matplotlib 认可的颜色格式
- 常用于表示正值数据,如相关系数的绝对值、连接强度等
See Also
value_to_hex : 将数值映射为十六进制颜色 matplotlib.colors.LinearSegmentedColormap : 底层实现类
Source code in src/plotfig/utils/color.py
is_symmetric_square
¶
判断一个矩阵是否为对称方阵。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
matrix
|
NDArray
|
待判断的矩阵。 |
required |
tol
|
float
|
判断对称性的容差,默认为 1e-8。 |
1e-08
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
如果是对称方阵则返回 True,否则返回 False。 |
Source code in src/plotfig/utils/matrix.py
value_to_hex
¶
将数值通过色图和归一化映射为十六进制颜色字符串。
该函数实现了从数值到颜色的完整映射流程:首先使用归一化对象将数值映射到 [0, 1] 区间, 然后通过色图将归一化值转换为 RGBA 颜色,最后转换为十六进制格式。常用于根据数据强度 动态生成颜色,如脑连接图中根据连接强度着色连线。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
需要映射的原始数值,可以是任意范围的浮点数。 |
required |
cmap
|
Colormap
|
matplotlib 色图对象,定义了归一化值到颜色的映射关系。 |
required |
norm
|
Normalize
|
matplotlib 归一化对象,定义了原始数值到 [0, 1] 的映射规则。 常用类型包括 Normalize(线性)、LogNorm(对数)等。 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
十六进制颜色字符串,格式为 "#RRGGBB"。 |
Examples:
>>> import matplotlib.pyplot as plt
>>> from matplotlib.colors import Normalize
>>>
>>> # 创建色图和归一化对象
>>> cmap = gen_cmap("red")
>>> norm = Normalize(vmin=0, vmax=100)
>>>
>>> # 将数值映射为颜色
>>> color1 = value_to_hex(25, cmap, norm) # 浅红色
>>> color2 = value_to_hex(75, cmap, norm) # 深红色
>>> print(color1, color2)
'#ffbfbf' '#bf4040'
>>>
>>> # 在脑连接图中使用
>>> connection_strengths = [0.1, 0.5, 0.9]
>>> norm = Normalize(vmin=0, vmax=1)
>>> colors = [value_to_hex(s, cmap, norm) for s in connection_strengths]
>>>
>>> # 使用对数归一化
>>> from matplotlib.colors import LogNorm
>>> norm_log = LogNorm(vmin=1, vmax=1000)
>>> color = value_to_hex(100, cmap, norm_log)
Notes
- 归一化对象决定了数值如何映射到 [0, 1] 区间
- 色图决定了 [0, 1] 区间的值如何映射到具体颜色
- 返回的十六进制颜色可直接用于 matplotlib 或 plotly 绘图
- 如果 value 超出归一化对象的范围,会被裁剪到边界值
See Also
gen_cmap : 生成线性渐变色图 matplotlib.colors.Normalize : 线性归一化 matplotlib.colors.LogNorm : 对数归一化