Spaces:
Running
Running
File size: 43,595 Bytes
226ac39 |
1 2 3 4 5 6 7 8 9 10 11 12 13 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 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 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 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 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 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 799 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 900 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 984 985 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 1042 1043 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 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 |
"""
Matplotlib + Seaborn Visualization Engine
Production-quality visualizations that work reliably with Gradio UI.
All functions return matplotlib Figure objects (not file paths).
Designed for publication-quality plots with professional styling.
"""
import matplotlib
matplotlib.use('Agg') # Use non-interactive backend for Gradio compatibility
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from typing import Dict, Any, List, Optional, Tuple, Union
from pathlib import Path
import warnings
warnings.filterwarnings('ignore')
# Set global style
sns.set_style('whitegrid')
plt.rcParams['figure.facecolor'] = 'white'
plt.rcParams['axes.facecolor'] = 'white'
plt.rcParams['font.size'] = 10
plt.rcParams['axes.labelsize'] = 12
plt.rcParams['axes.titlesize'] = 14
plt.rcParams['xtick.labelsize'] = 10
plt.rcParams['ytick.labelsize'] = 10
plt.rcParams['legend.fontsize'] = 10
# ============================================================================
# BASIC PLOTS
# ============================================================================
def create_scatter_plot(
x: Union[np.ndarray, pd.Series, list],
y: Union[np.ndarray, pd.Series, list],
hue: Optional[Union[np.ndarray, pd.Series, list]] = None,
size: Optional[Union[np.ndarray, pd.Series, list]] = None,
title: str = "Scatter Plot",
xlabel: str = "X",
ylabel: str = "Y",
figsize: Tuple[int, int] = (10, 6),
alpha: float = 0.6,
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a professional scatter plot with optional color coding and size variation.
Args:
x: X-axis data
y: Y-axis data
hue: Optional categorical data for color coding
size: Optional numeric data for size variation
title: Plot title
xlabel: X-axis label
ylabel: Y-axis label
figsize: Figure size (width, height)
alpha: Point transparency (0-1)
save_path: Optional path to save PNG file
Returns:
matplotlib Figure object
Example:
>>> fig = create_scatter_plot(df['feature1'], df['target'],
... hue=df['category'], title='Feature vs Target')
>>> # Display in Gradio: gr.Plot(value=fig)
"""
try:
fig, ax = plt.subplots(figsize=figsize)
# Convert inputs to arrays
x = np.array(x)
y = np.array(y)
if hue is not None:
hue = np.array(hue)
unique_hues = np.unique(hue)
colors = sns.color_palette('Set2', n_colors=len(unique_hues))
for i, hue_val in enumerate(unique_hues):
mask = hue == hue_val
scatter_size = 50 if size is None else np.array(size)[mask]
ax.scatter(x[mask], y[mask],
c=[colors[i]],
s=scatter_size,
alpha=alpha,
label=str(hue_val),
edgecolors='black',
linewidth=0.5)
ax.legend(title='Category', loc='best', framealpha=0.9)
else:
scatter_size = 50 if size is None else size
ax.scatter(x, y,
c='steelblue',
s=scatter_size,
alpha=alpha,
edgecolors='black',
linewidth=0.5)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)
ax.grid(True, alpha=0.3, linestyle='--')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved scatter plot to {save_path}")
return fig
except Exception as e:
print(f" β Error creating scatter plot: {str(e)}")
return None
def create_line_plot(
x: Union[np.ndarray, pd.Series, list],
y: Union[Dict[str, np.ndarray], np.ndarray, pd.Series, list],
title: str = "Line Plot",
xlabel: str = "X",
ylabel: str = "Y",
figsize: Tuple[int, int] = (10, 6),
markers: bool = True,
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a line plot (supports multiple lines via dict).
Args:
x: X-axis data
y: Y-axis data (dict for multiple lines: {'label1': y1, 'label2': y2})
title: Plot title
xlabel: X-axis label
ylabel: Y-axis label
figsize: Figure size
markers: Show markers on lines
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, ax = plt.subplots(figsize=figsize)
x = np.array(x)
marker_style = 'o' if markers else None
if isinstance(y, dict):
colors = sns.color_palette('husl', n_colors=len(y))
for i, (label, y_data) in enumerate(y.items()):
ax.plot(x, np.array(y_data),
marker=marker_style,
label=label,
linewidth=2,
markersize=6,
color=colors[i])
ax.legend(loc='best', framealpha=0.9)
else:
ax.plot(x, np.array(y),
marker=marker_style,
linewidth=2,
markersize=6,
color='steelblue')
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)
ax.grid(True, alpha=0.3, linestyle='--')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved line plot to {save_path}")
return fig
except Exception as e:
print(f" β Error creating line plot: {str(e)}")
return None
def create_bar_chart(
categories: Union[list, np.ndarray],
values: Union[np.ndarray, pd.Series, list],
title: str = "Bar Chart",
xlabel: str = "Category",
ylabel: str = "Value",
figsize: Tuple[int, int] = (10, 6),
horizontal: bool = False,
color: str = 'steelblue',
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a bar chart (vertical or horizontal).
Args:
categories: Category names
values: Values for each category
title: Plot title
xlabel: X-axis label
ylabel: Y-axis label
figsize: Figure size
horizontal: If True, create horizontal bars
color: Bar color
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, ax = plt.subplots(figsize=figsize)
categories = list(categories)
values = np.array(values)
if horizontal:
ax.barh(categories, values, color=color, edgecolor='black', linewidth=0.7)
ax.set_xlabel(ylabel, fontsize=12)
ax.set_ylabel(xlabel, fontsize=12)
else:
ax.bar(categories, values, color=color, edgecolor='black', linewidth=0.7)
ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)
# Rotate labels if many categories
if len(categories) > 10:
plt.xticks(rotation=45, ha='right')
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.grid(True, alpha=0.3, linestyle='--', axis='y' if not horizontal else 'x')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved bar chart to {save_path}")
return fig
except Exception as e:
print(f" β Error creating bar chart: {str(e)}")
return None
def create_histogram(
data: Union[np.ndarray, pd.Series, list],
title: str = "Histogram",
xlabel: str = "Value",
ylabel: str = "Frequency",
bins: int = 30,
figsize: Tuple[int, int] = (10, 6),
kde: bool = True,
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a histogram with optional KDE overlay.
Args:
data: Data to plot
title: Plot title
xlabel: X-axis label
ylabel: Y-axis label
bins: Number of bins
figsize: Figure size
kde: Show kernel density estimate
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, ax = plt.subplots(figsize=figsize)
data = np.array(data)
data = data[~np.isnan(data)] # Remove NaN values
if len(data) == 0:
print(" β No valid data for histogram")
return None
# Create histogram
ax.hist(data, bins=bins, color='steelblue',
edgecolor='black', alpha=0.7, density=kde)
# Add KDE if requested
if kde:
ax2 = ax.twinx()
sns.kdeplot(data, ax=ax2, color='darkred', linewidth=2, label='KDE')
ax2.set_ylabel('Density', fontsize=12)
ax2.legend(loc='upper right')
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)
ax.grid(True, alpha=0.3, linestyle='--')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved histogram to {save_path}")
return fig
except Exception as e:
print(f" β Error creating histogram: {str(e)}")
return None
def create_boxplot(
data: Union[Dict[str, np.ndarray], pd.DataFrame],
title: str = "Box Plot",
xlabel: str = "Category",
ylabel: str = "Value",
figsize: Tuple[int, int] = (10, 6),
horizontal: bool = False,
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create box plots for multiple columns/categories.
Args:
data: Dictionary of {column_name: values} or DataFrame
title: Plot title
xlabel: X-axis label
ylabel: Y-axis label
figsize: Figure size
horizontal: If True, create horizontal boxplots
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, ax = plt.subplots(figsize=figsize)
if isinstance(data, pd.DataFrame):
data_to_plot = [data[col].dropna() for col in data.columns]
labels = data.columns
elif isinstance(data, dict):
data_to_plot = [np.array(v)[~np.isnan(np.array(v))] for v in data.values()]
labels = list(data.keys())
else:
raise ValueError("Data must be DataFrame or dict")
bp = ax.boxplot(data_to_plot,
labels=labels,
vert=not horizontal,
patch_artist=True,
notch=True,
showmeans=True)
# Styling
for patch in bp['boxes']:
patch.set_facecolor('lightblue')
patch.set_alpha(0.7)
for whisker in bp['whiskers']:
whisker.set(linewidth=1.5, color='gray')
for cap in bp['caps']:
cap.set(linewidth=1.5, color='gray')
for median in bp['medians']:
median.set(linewidth=2, color='darkred')
for mean in bp['means']:
mean.set(marker='D', markerfacecolor='green', markersize=6)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
if horizontal:
ax.set_xlabel(ylabel, fontsize=12)
ax.set_ylabel(xlabel, fontsize=12)
else:
ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)
if len(labels) > 8:
plt.xticks(rotation=45, ha='right')
ax.grid(True, alpha=0.3, linestyle='--', axis='y' if not horizontal else 'x')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved boxplot to {save_path}")
return fig
except Exception as e:
print(f" β Error creating boxplot: {str(e)}")
return None
# ============================================================================
# STATISTICAL PLOTS
# ============================================================================
def create_correlation_heatmap(
data: Union[pd.DataFrame, np.ndarray],
columns: Optional[List[str]] = None,
title: str = "Correlation Heatmap",
figsize: Tuple[int, int] = (12, 10),
annot: bool = True,
cmap: str = 'RdBu_r',
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a correlation heatmap with annotations.
Args:
data: DataFrame or correlation matrix
columns: Column names (if data is np.ndarray)
title: Plot title
figsize: Figure size
annot: Show correlation values as annotations
cmap: Colormap (diverging, centered at 0)
save_path: Optional save path
Returns:
matplotlib Figure object
Example:
>>> fig = create_correlation_heatmap(df[numeric_cols])
"""
try:
fig, ax = plt.subplots(figsize=figsize)
# Calculate correlation if DataFrame
if isinstance(data, pd.DataFrame):
corr_matrix = data.corr()
else:
corr_matrix = pd.DataFrame(data, columns=columns, index=columns)
# Create heatmap
mask = np.triu(np.ones_like(corr_matrix, dtype=bool)) # Mask upper triangle
sns.heatmap(corr_matrix,
mask=mask,
annot=annot,
fmt='.2f',
cmap=cmap,
center=0,
square=True,
linewidths=0.5,
cbar_kws={'shrink': 0.8, 'label': 'Correlation'},
ax=ax,
vmin=-1,
vmax=1)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved correlation heatmap to {save_path}")
return fig
except Exception as e:
print(f" β Error creating correlation heatmap: {str(e)}")
return None
def create_distribution_plot(
data: Union[np.ndarray, pd.Series, list],
title: str = "Distribution Plot",
xlabel: str = "Value",
figsize: Tuple[int, int] = (10, 6),
show_rug: bool = False,
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a distribution plot with histogram and KDE.
Args:
data: Data to plot
title: Plot title
xlabel: X-axis label
figsize: Figure size
show_rug: Show rug plot (data points on x-axis)
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, ax = plt.subplots(figsize=figsize)
data = np.array(data)
data = data[~np.isnan(data)]
if len(data) == 0:
print(" β No valid data for distribution plot")
return None
# Create distribution plot
sns.histplot(data, kde=True, ax=ax, color='steelblue',
edgecolor='black', alpha=0.6, bins=30)
if show_rug:
sns.rugplot(data, ax=ax, color='darkred', alpha=0.5, height=0.05)
# Add statistics text
mean_val = np.mean(data)
median_val = np.median(data)
std_val = np.std(data)
stats_text = f'Mean: {mean_val:.2f}\nMedian: {median_val:.2f}\nStd: {std_val:.2f}'
ax.text(0.98, 0.98, stats_text,
transform=ax.transAxes,
verticalalignment='top',
horizontalalignment='right',
bbox=dict(boxstyle='round', facecolor='wheat', alpha=0.5),
fontsize=10)
# Add vertical lines for mean and median
ax.axvline(mean_val, color='red', linestyle='--', linewidth=2, label='Mean')
ax.axvline(median_val, color='green', linestyle='--', linewidth=2, label='Median')
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel('Frequency / Density', fontsize=12)
ax.legend(loc='upper left')
ax.grid(True, alpha=0.3, linestyle='--')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved distribution plot to {save_path}")
return fig
except Exception as e:
print(f" β Error creating distribution plot: {str(e)}")
return None
def create_violin_plot(
data: Union[Dict[str, np.ndarray], pd.DataFrame],
title: str = "Violin Plot",
xlabel: str = "Category",
ylabel: str = "Value",
figsize: Tuple[int, int] = (10, 6),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create violin plots showing distribution for multiple categories.
Args:
data: Dictionary or DataFrame with categories
title: Plot title
xlabel: X-axis label
ylabel: Y-axis label
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, ax = plt.subplots(figsize=figsize)
if isinstance(data, dict):
# Convert dict to DataFrame for seaborn
df_list = []
for key, values in data.items():
df_list.append(pd.DataFrame({
'Category': [key] * len(values),
'Value': values
}))
plot_df = pd.concat(df_list, ignore_index=True)
else:
plot_df = data
# Create violin plot
sns.violinplot(data=plot_df, x='Category', y='Value', ax=ax,
palette='Set2', inner='box')
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.set_xlabel(xlabel, fontsize=12)
ax.set_ylabel(ylabel, fontsize=12)
if len(plot_df['Category'].unique()) > 8:
plt.xticks(rotation=45, ha='right')
ax.grid(True, alpha=0.3, linestyle='--', axis='y')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved violin plot to {save_path}")
return fig
except Exception as e:
print(f" β Error creating violin plot: {str(e)}")
return None
def create_pairplot(
data: pd.DataFrame,
hue: Optional[str] = None,
title: str = "Pair Plot",
figsize: Tuple[int, int] = (12, 12),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a pairplot (scatterplot matrix) for multiple features.
Args:
data: DataFrame with features to plot
hue: Column name for color coding
title: Plot title
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
# Seaborn pairplot returns a PairGrid, we need to extract the figure
if hue and hue in data.columns:
pair_grid = sns.pairplot(data, hue=hue, palette='Set2',
diag_kind='kde', corner=True)
else:
pair_grid = sns.pairplot(data, palette='Set2',
diag_kind='kde', corner=True)
fig = pair_grid.fig
fig.suptitle(title, fontsize=14, fontweight='bold', y=1.01)
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved pairplot to {save_path}")
return fig
except Exception as e:
print(f" β Error creating pairplot: {str(e)}")
return None
# ============================================================================
# MACHINE LEARNING PLOTS
# ============================================================================
def create_roc_curve(
models_data: Dict[str, Tuple[np.ndarray, np.ndarray, float]],
title: str = "ROC Curve Comparison",
figsize: Tuple[int, int] = (10, 8),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create ROC curves for multiple models on the same plot.
Args:
models_data: Dict of {model_name: (fpr, tpr, auc_score)}
title: Plot title
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
Example:
>>> from sklearn.metrics import roc_curve, auc
>>> fpr, tpr, _ = roc_curve(y_true, y_pred_proba)
>>> auc_score = auc(fpr, tpr)
>>> models = {'Random Forest': (fpr, tpr, auc_score)}
>>> fig = create_roc_curve(models)
"""
try:
fig, ax = plt.subplots(figsize=figsize)
colors = sns.color_palette('husl', n_colors=len(models_data))
for i, (model_name, (fpr, tpr, auc_score)) in enumerate(models_data.items()):
ax.plot(fpr, tpr,
linewidth=2.5,
label=f'{model_name} (AUC = {auc_score:.3f})',
color=colors[i])
# Add diagonal reference line (random classifier)
ax.plot([0, 1], [0, 1],
linestyle='--',
linewidth=2,
color='gray',
label='Random Classifier (AUC = 0.500)')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate', fontsize=12)
ax.set_ylabel('True Positive Rate', fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.legend(loc='lower right', fontsize=10, framealpha=0.9)
ax.grid(True, alpha=0.3, linestyle='--')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved ROC curve to {save_path}")
return fig
except Exception as e:
print(f" β Error creating ROC curve: {str(e)}")
return None
def create_confusion_matrix(
cm: np.ndarray,
class_names: Optional[List[str]] = None,
title: str = "Confusion Matrix",
figsize: Tuple[int, int] = (10, 8),
show_percentages: bool = True,
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a confusion matrix heatmap with annotations.
Args:
cm: Confusion matrix (from sklearn.metrics.confusion_matrix)
class_names: Names of classes (optional)
title: Plot title
figsize: Figure size
show_percentages: Show percentages in addition to counts
save_path: Optional save path
Returns:
matplotlib Figure object
Example:
>>> from sklearn.metrics import confusion_matrix
>>> cm = confusion_matrix(y_true, y_pred)
>>> fig = create_confusion_matrix(cm, class_names=['Class 0', 'Class 1'])
"""
try:
fig, ax = plt.subplots(figsize=figsize)
if class_names is None:
class_names = [f'Class {i}' for i in range(len(cm))]
# Normalize for percentages
cm_percent = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] * 100
# Create annotations
if show_percentages:
annotations = np.array([[f'{count}\n({percent:.1f}%)'
for count, percent in zip(row_counts, row_percents)]
for row_counts, row_percents in zip(cm, cm_percent)])
else:
annotations = cm
# Create heatmap
sns.heatmap(cm,
annot=annotations,
fmt='',
cmap='Blues',
square=True,
linewidths=0.5,
cbar_kws={'label': 'Count'},
xticklabels=class_names,
yticklabels=class_names,
ax=ax)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.set_ylabel('Actual', fontsize=12)
ax.set_xlabel('Predicted', fontsize=12)
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved confusion matrix to {save_path}")
return fig
except Exception as e:
print(f" β Error creating confusion matrix: {str(e)}")
return None
def create_precision_recall_curve(
models_data: Dict[str, Tuple[np.ndarray, np.ndarray, float]],
title: str = "Precision-Recall Curve",
figsize: Tuple[int, int] = (10, 8),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create precision-recall curves for multiple models.
Args:
models_data: Dict of {model_name: (precision, recall, avg_precision)}
title: Plot title
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, ax = plt.subplots(figsize=figsize)
colors = sns.color_palette('husl', n_colors=len(models_data))
for i, (model_name, (precision, recall, avg_precision)) in enumerate(models_data.items()):
ax.plot(recall, precision,
linewidth=2.5,
label=f'{model_name} (AP = {avg_precision:.3f})',
color=colors[i])
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('Recall', fontsize=12)
ax.set_ylabel('Precision', fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.legend(loc='best', fontsize=10, framealpha=0.9)
ax.grid(True, alpha=0.3, linestyle='--')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved precision-recall curve to {save_path}")
return fig
except Exception as e:
print(f" β Error creating precision-recall curve: {str(e)}")
return None
def create_feature_importance(
feature_names: List[str],
importances: np.ndarray,
title: str = "Feature Importance",
top_n: int = 20,
figsize: Tuple[int, int] = (10, 8),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a horizontal bar chart of feature importances.
Args:
feature_names: List of feature names
importances: Array of importance values
title: Plot title
top_n: Number of top features to show
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
Example:
>>> importances = model.feature_importances_
>>> fig = create_feature_importance(feature_names, importances, top_n=15)
"""
try:
# Sort by importance
indices = np.argsort(importances)[::-1][:top_n]
sorted_features = [feature_names[i] for i in indices]
sorted_importances = importances[indices]
# Create figure with appropriate height
height = max(8, top_n * 0.4)
fig, ax = plt.subplots(figsize=(figsize[0], height))
# Color bars by positive/negative (if any negative values)
colors = ['green' if x >= 0 else 'red' for x in sorted_importances]
# Create horizontal bar chart
y_pos = np.arange(len(sorted_features))
ax.barh(y_pos, sorted_importances, color=colors, edgecolor='black', linewidth=0.7)
ax.set_yticks(y_pos)
ax.set_yticklabels(sorted_features)
ax.invert_yaxis() # Top features at top
ax.set_xlabel('Importance Score', fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.grid(True, alpha=0.3, linestyle='--', axis='x')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved feature importance to {save_path}")
return fig
except Exception as e:
print(f" β Error creating feature importance plot: {str(e)}")
return None
def create_residual_plot(
y_true: np.ndarray,
y_pred: np.ndarray,
title: str = "Residual Plot",
figsize: Tuple[int, int] = (10, 6),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a residual plot (Predicted vs Actual) for regression models.
Args:
y_true: True target values
y_pred: Predicted values
title: Plot title
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(figsize[0]*2, figsize[1]))
residuals = y_true - y_pred
# Plot 1: Predicted vs Actual
ax1.scatter(y_true, y_pred, alpha=0.5, s=50, edgecolors='black', linewidth=0.5)
# Add perfect prediction line
min_val = min(y_true.min(), y_pred.min())
max_val = max(y_true.max(), y_pred.max())
ax1.plot([min_val, max_val], [min_val, max_val],
'r--', linewidth=2, label='Perfect Prediction')
ax1.set_xlabel('Actual Values', fontsize=12)
ax1.set_ylabel('Predicted Values', fontsize=12)
ax1.set_title('Predicted vs Actual', fontsize=13, fontweight='bold')
ax1.legend()
ax1.grid(True, alpha=0.3, linestyle='--')
# Plot 2: Residuals vs Predicted
ax2.scatter(y_pred, residuals, alpha=0.5, s=50,
color='steelblue', edgecolors='black', linewidth=0.5)
ax2.axhline(y=0, color='red', linestyle='--', linewidth=2)
ax2.set_xlabel('Predicted Values', fontsize=12)
ax2.set_ylabel('Residuals', fontsize=12)
ax2.set_title('Residuals vs Predicted', fontsize=13, fontweight='bold')
ax2.grid(True, alpha=0.3, linestyle='--')
fig.suptitle(title, fontsize=14, fontweight='bold', y=1.02)
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved residual plot to {save_path}")
return fig
except Exception as e:
print(f" β Error creating residual plot: {str(e)}")
return None
def create_learning_curve(
train_sizes: np.ndarray,
train_scores_mean: np.ndarray,
train_scores_std: np.ndarray,
val_scores_mean: np.ndarray,
val_scores_std: np.ndarray,
title: str = "Learning Curve",
figsize: Tuple[int, int] = (10, 6),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a learning curve showing training and validation scores.
Args:
train_sizes: Array of training set sizes
train_scores_mean: Mean training scores
train_scores_std: Std of training scores
val_scores_mean: Mean validation scores
val_scores_std: Std of validation scores
title: Plot title
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, ax = plt.subplots(figsize=figsize)
# Plot training scores
ax.plot(train_sizes, train_scores_mean, 'o-', color='blue',
linewidth=2, markersize=8, label='Training Score')
ax.fill_between(train_sizes,
train_scores_mean - train_scores_std,
train_scores_mean + train_scores_std,
alpha=0.2, color='blue')
# Plot validation scores
ax.plot(train_sizes, val_scores_mean, 'o-', color='orange',
linewidth=2, markersize=8, label='Validation Score')
ax.fill_between(train_sizes,
val_scores_mean - val_scores_std,
val_scores_mean + val_scores_std,
alpha=0.2, color='orange')
ax.set_xlabel('Training Set Size', fontsize=12)
ax.set_ylabel('Score', fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.legend(loc='best', fontsize=11, framealpha=0.9)
ax.grid(True, alpha=0.3, linestyle='--')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved learning curve to {save_path}")
return fig
except Exception as e:
print(f" β Error creating learning curve: {str(e)}")
return None
# ============================================================================
# DATA QUALITY PLOTS
# ============================================================================
def create_missing_values_heatmap(
df: pd.DataFrame,
title: str = "Missing Values Heatmap",
figsize: Tuple[int, int] = (12, 8),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a heatmap showing missing values pattern.
Args:
df: DataFrame to analyze
title: Plot title
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
fig, ax = plt.subplots(figsize=figsize)
# Create binary matrix (1 = missing, 0 = present)
missing_matrix = df.isnull().astype(int)
# Plot heatmap
sns.heatmap(missing_matrix.T,
cbar=False,
cmap='RdYlGn_r',
ax=ax,
yticklabels=df.columns)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.set_xlabel('Sample Index', fontsize=12)
ax.set_ylabel('Features', fontsize=12)
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved missing values heatmap to {save_path}")
return fig
except Exception as e:
print(f" β Error creating missing values heatmap: {str(e)}")
return None
def create_missing_values_bar(
df: pd.DataFrame,
title: str = "Missing Values by Column",
figsize: Tuple[int, int] = (10, 6),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a bar chart showing percentage of missing values per column.
Args:
df: DataFrame to analyze
title: Plot title
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
# Calculate missing percentages
missing_pct = (df.isnull().sum() / len(df) * 100).sort_values(ascending=False)
missing_pct = missing_pct[missing_pct > 0] # Only columns with missing values
if len(missing_pct) == 0:
print(" βΉ No missing values found")
return None
height = max(6, len(missing_pct) * 0.3)
fig, ax = plt.subplots(figsize=(figsize[0], height))
# Create horizontal bar chart
colors = plt.cm.Reds(missing_pct / 100)
ax.barh(range(len(missing_pct)), missing_pct.values,
color=colors, edgecolor='black', linewidth=0.7)
ax.set_yticks(range(len(missing_pct)))
ax.set_yticklabels(missing_pct.index)
ax.set_xlabel('Missing Values (%)', fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.grid(True, alpha=0.3, linestyle='--', axis='x')
# Add percentage labels
for i, v in enumerate(missing_pct.values):
ax.text(v + 1, i, f'{v:.1f}%', va='center', fontsize=10)
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved missing values bar chart to {save_path}")
return fig
except Exception as e:
print(f" β Error creating missing values bar chart: {str(e)}")
return None
def create_outlier_detection_boxplot(
df: pd.DataFrame,
columns: Optional[List[str]] = None,
title: str = "Outlier Detection",
figsize: Tuple[int, int] = (12, 6),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create box plots for outlier detection across multiple columns.
Args:
df: DataFrame with numeric columns
columns: Columns to plot (None = all numeric)
title: Plot title
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
if columns is None:
columns = df.select_dtypes(include=[np.number]).columns.tolist()[:10]
return create_boxplot(df[columns], title=title, figsize=figsize, save_path=save_path)
except Exception as e:
print(f" β Error creating outlier detection plot: {str(e)}")
return None
def create_skewness_plot(
df: pd.DataFrame,
title: str = "Feature Skewness Distribution",
figsize: Tuple[int, int] = (10, 6),
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a bar chart showing skewness of numeric features.
Args:
df: DataFrame with numeric columns
title: Plot title
figsize: Figure size
save_path: Optional save path
Returns:
matplotlib Figure object
"""
try:
# Calculate skewness for numeric columns
numeric_cols = df.select_dtypes(include=[np.number]).columns
skewness = df[numeric_cols].skew().sort_values(ascending=False)
if len(skewness) == 0:
print(" βΉ No numeric columns to analyze")
return None
height = max(6, len(skewness) * 0.3)
fig, ax = plt.subplots(figsize=(figsize[0], height))
# Color by skewness level
colors = ['green' if abs(x) < 0.5 else 'orange' if abs(x) < 1 else 'red'
for x in skewness.values]
ax.barh(range(len(skewness)), skewness.values,
color=colors, edgecolor='black', linewidth=0.7)
ax.set_yticks(range(len(skewness)))
ax.set_yticklabels(skewness.index)
ax.set_xlabel('Skewness', fontsize=12)
ax.set_title(title, fontsize=14, fontweight='bold', pad=20)
ax.axvline(x=0, color='black', linestyle='-', linewidth=1)
ax.axvline(x=-0.5, color='gray', linestyle='--', linewidth=1, alpha=0.5)
ax.axvline(x=0.5, color='gray', linestyle='--', linewidth=1, alpha=0.5)
ax.grid(True, alpha=0.3, linestyle='--', axis='x')
# Add legend
from matplotlib.patches import Patch
legend_elements = [
Patch(facecolor='green', label='Low (|skew| < 0.5)'),
Patch(facecolor='orange', label='Moderate (0.5 β€ |skew| < 1)'),
Patch(facecolor='red', label='High (|skew| β₯ 1)')
]
ax.legend(handles=legend_elements, loc='best')
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved skewness plot to {save_path}")
return fig
except Exception as e:
print(f" β Error creating skewness plot: {str(e)}")
return None
# ============================================================================
# UTILITY FUNCTIONS
# ============================================================================
def save_figure(fig: plt.Figure, path: str, dpi: int = 300) -> None:
"""
Save a matplotlib figure to file.
Args:
fig: Matplotlib Figure object
path: Output file path (supports .png, .jpg, .pdf, .svg)
dpi: Resolution (dots per inch)
"""
try:
Path(path).parent.mkdir(parents=True, exist_ok=True)
fig.savefig(path, dpi=dpi, bbox_inches='tight', facecolor='white')
print(f" β Saved figure to {path}")
except Exception as e:
print(f" β Error saving figure: {str(e)}")
def close_figure(fig: plt.Figure) -> None:
"""
Close a matplotlib figure to free memory.
Args:
fig: Matplotlib Figure object
"""
if fig is not None:
plt.close(fig)
def create_subplots_grid(
plot_data: List[Dict[str, Any]],
rows: int,
cols: int,
figsize: Tuple[int, int] = (15, 12),
title: str = "Plot Grid",
save_path: Optional[str] = None
) -> plt.Figure:
"""
Create a grid of subplots.
Args:
plot_data: List of dicts with plot specifications
rows: Number of rows
cols: Number of columns
figsize: Figure size
title: Overall title
save_path: Optional save path
Returns:
matplotlib Figure object
Example:
>>> plots = [
... {'type': 'scatter', 'x': x1, 'y': y1, 'title': 'Plot 1'},
... {'type': 'hist', 'data': data1, 'title': 'Plot 2'}
... ]
>>> fig = create_subplots_grid(plots, 2, 2)
"""
try:
fig, axes = plt.subplots(rows, cols, figsize=figsize)
axes = axes.flatten() if rows * cols > 1 else [axes]
for i, (ax, plot_spec) in enumerate(zip(axes, plot_data)):
plot_type = plot_spec.get('type', 'scatter')
if plot_type == 'scatter':
ax.scatter(plot_spec['x'], plot_spec['y'], alpha=0.6)
elif plot_type == 'hist':
ax.hist(plot_spec['data'], bins=30, edgecolor='black')
elif plot_type == 'line':
ax.plot(plot_spec['x'], plot_spec['y'])
ax.set_title(plot_spec.get('title', f'Subplot {i+1}'), fontweight='bold')
ax.grid(True, alpha=0.3)
# Hide unused subplots
for i in range(len(plot_data), len(axes)):
axes[i].axis('off')
fig.suptitle(title, fontsize=16, fontweight='bold', y=0.995)
plt.tight_layout()
if save_path:
fig.savefig(save_path, dpi=300, bbox_inches='tight')
print(f" β Saved subplot grid to {save_path}")
return fig
except Exception as e:
print(f" β Error creating subplot grid: {str(e)}")
return None
|