1. 折线图

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# 设置风格
sns.set(style="whitegrid")

# 生成数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# 创建图表
plt.figure(figsize=(10, 6))
plt.plot(x, y1, label="Sine Wave", color="b", linewidth=2)
plt.plot(x, y2, label="Cosine Wave", color="r", linestyle="--", linewidth=2)

# 添加装饰
plt.fill_between(x, y1, y2, color="gray", alpha=0.1)
plt.title("Line Plot", fontsize=15)
plt.xlabel("X-axis", fontsize=12)
plt.ylabel("Y-axis", fontsize=12)
plt.legend()
plt.show()

image.png

2. 散点图

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# 设置风格
sns.set(style="whitegrid")

# 生成数据
x = np.random.rand(100)
y = np.random.rand(100)
colors = np.random.rand(100)
sizes = 1000 * np.random.rand(100)

# 创建图表
plt.figure(figsize=(10, 6))
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='viridis')
plt.colorbar()

# 添加装饰
plt.title("Scatter Plot", fontsize=15)
plt.xlabel("X-axis", fontsize=12)
plt.ylabel("Y-axis", fontsize=12)
plt.show()

image.png

3. 条形图

import matplotlib.pyplot as plt
import seaborn as sns

# 设置风格
sns.set(style="whitegrid")

# 生成数据
categories = ['A', 'B', 'C', 'D']
values1 = [5, 7, 8, 6]
values2 = [3, 4, 5, 2]

# 创建图表
fig, ax = plt.subplots(figsize=(10, 6))
bar1 = ax.bar(categories, values1, label='Group 1')
bar2 = ax.bar(categories, values2, bottom=values1, label='Group 2')

# 添加装饰
ax.set_title("Stacked Bar Chart", fontsize=15)
ax.set_xlabel("Categories", fontsize=12)
ax.set_ylabel("Values", fontsize=12)
ax.legend()

# 添加数值标签
for rect in bar1 + bar2:
height = rect.get_height()
ax.annotate(f'{height}', xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), textcoords="offset points", ha='center', va='bottom')

plt.show()

image.png

4. 热力图

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# 生成数据
data = np.random.rand(10, 12)

# 创建热图
plt.figure(figsize=(10, 6))
sns.heatmap(data, annot=True, fmt=".2f", cmap='coolwarm')

# 添加装饰
plt.title("Heatmap", fontsize=15)
plt.show()

image.png

5. 箱线图

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# 设置风格
sns.set(style="whitegrid")

# 生成数据
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2

# 创建图表
plt.figure(figsize=(10, 6))
sns.boxplot(data=data, palette="vlag")

# 添加装饰
plt.title("Box Plot", fontsize=15)
plt.show()

image.png

6. 蜘蛛图

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# 设置风格
sns.set(style="whitegrid")

# 数据准备
labels = np.array(['A', 'B', 'C', 'D', 'E'])
stats = [10, 20, 30, 40, 50]
stats2 = [30, 10, 20, 30, 40]

# 创建蜘蛛图
angles = np.linspace(0, 2 * np.pi, len(labels), endpoint=False).tolist()
stats = np.concatenate((stats, [stats[0]]))
stats2 = np.concatenate((stats2, [stats2[0]]))
angles += angles[:1]

fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(polar=True))
ax.fill(angles, stats, color='blue', alpha=0.25, label='Group 1')
ax.plot(angles, stats, color='blue', linewidth=2)
ax.fill(angles, stats2, color='red', alpha=0.25, label='Group 2')
ax.plot(angles, stats2, color='red', linewidth=2)
ax.set_yticklabels([])
ax.set_xticks(angles[:-1])
ax.set_xticklabels(labels, fontsize=12)
ax.grid(True)

# 添加标题和图例
plt.title('Enhanced Spider Chart', size=20, color='black', y=1.1)
plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))
plt.show()

image.png

7. 双轴图

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# 设置风格
sns.set(style="whitegrid")

# 生成数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# 创建图表
fig, ax1 = plt.subplots(figsize=(10, 6))

ax2 = ax1.twinx()
ax1.plot(x, y1, 'g-')
ax2.plot(x, y2, 'b-')

# 添加装饰
ax1.set_xlabel('X-axis')
ax1.set_ylabel('Sine', color='g')
ax2.set_ylabel('Cosine', color='b')
plt.title('Dual Axis Plot')
plt.show()

image.png

8. 面积图

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# 设置风格
sns.set(style="whitegrid")

# 生成数据
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# 创建图表
plt.figure(figsize=(10, 6))
plt.fill_between(x, y1, color="skyblue", alpha=0.4)
plt.fill_between(x, y2, color="orange", alpha=0.4)

# 添加装饰
plt.title("Area Chart", fontsize=15)
plt.xlabel("X-axis", fontsize=12)
plt.ylabel("Y-axis", fontsize=12)
plt.show()

image.png

9. 带状图

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# 设置风格
sns.set(style="whitegrid")

# 生成数据
x = np.linspace(0, 10, 100)
y = np.sin(x)
z = np.sin(x) + np.random.normal(0, 0.1, 100)

# 创建图表
plt.figure(figsize=(10, 6))
plt.plot(x, y, label='Sine Wave')
plt.fill_between(x, y, z, where=(y >= z), interpolate=True, color='green', alpha=0.3)
plt.fill_between(x, y, z, where=(y < z), interpolate=True, color='red', alpha=0.3)

# 添加装饰
plt.title("Band Chart", fontsize=15)
plt.xlabel("X-axis", fontsize=12)
plt.ylabel("Y-axis", fontsize=12)
plt.legend()
plt.show()

image.png

10. 等高线图

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# 设置风格
sns.set(style="white")

# 数据准备
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

# 创建等高线图
plt.figure(figsize=(10, 6))
contour = plt.contourf(X, Y, Z, cmap='coolwarm', levels=20)
plt.colorbar(contour)

# 添加装饰
plt.title('Contour Plot', fontsize=15)
plt.show()

image.png

11. 极坐标图

import numpy as; np
import matplotlib.pyplot as plt
import seaborn as sns

# 设置风格
sns.set(style="white")

# 数据准备
theta = np.linspace(0, 2*np.pi, 100)
r = np.abs(np.sin(theta) * np.cos(theta))

# 创建极坐标图
plt.figure(figsize=(8, 8))
ax = plt.subplot(111, polar=True)
ax.plot(theta, r, color='b', linewidth=2)

# 添加装饰
plt.title('Polar Plot', fontsize=15)
plt.show()


image.png

12. 3D曲面图

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns

# 设置风格
sns.set(style="white")

# 数据准备
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

# 创建3D曲面图
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap='viridis')
fig.colorbar(surf)

# 添加装饰
plt.title('3D Surface Plot', fontsize=15)
plt.show()

image.png

13. 3D散点图

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns

# 设置风格
sns.set(style="white")

# 数据准备
x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

# 创建3D散点图
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
scatter = ax.scatter(x, y, z, c=z, cmap='viridis')

# 添加装饰
fig.colorbar(scatter)
plt.title('3D Scatter Plot', fontsize=15)
plt.show()

image.png

14. 3D条形图

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns

# 设置风格
sns.set(style="white")

# 数据准备
x = np.arange(1, 11)
y = np.random.randint(1, 10, 10)
z = np.zeros(10)

# 创建3D条形图
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111, projection='3d')
bars = ax.bar3d(x, y, z, 1, 1, y, shade=True)

# 添加装饰
plt.title('3D Bar Plot', fontsize=15)
plt.show()

image.png

15. 直方图

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# 设置风格
sns.set(style="whitegrid")

# 生成数据
data = np.random.randn(1000)

# 创建图表
plt.figure(figsize=(10, 6))
sns.histplot(data, kde=True, color="purple", bins=30)

# 添加装饰
plt.title("Histogram", fontsize=15)
plt.xlabel("Value", fontsize=12)
plt.ylabel("Frequency", fontsize=12)
plt.show()

image.png

16. 小提琴图

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# 设置风格
sns.set(style="whitegrid")

# 生成数据
data = np.random.normal(size=(20, 6)) + np.arange(6) / 2

# 创建图表
plt.figure(figsize=(10, 6))
sns.violinplot(data=data, palette="muted")

# 添加装饰
plt.title("Violin Plot", fontsize=15)
plt.show()

image.png

17. 成对关系图

import seaborn as sns
import matplotlib.pyplot as plt

# 生成数据
iris = sns.load_dataset("iris")

# 创建图表
sns.pairplot(iris, hue="species", palette="muted")
plt.suptitle("Pair Plot", y=1.02, fontsize=15)
plt.show()

image.png

18. 18.Facet Grid 图

import seaborn as sns
import matplotlib.pyplot as plt

# 生成数据
tips = sns.load_dataset("tips")

# 创建图表
g = sns.FacetGrid(tips, col="time", row="smoker", margin_titles=True)
g.map(sns.scatterplot, "total_bill", "tip", alpha=.7)
g.add_legend()

# 添加装饰
plt.suptitle("Facet Grid", y=1.02, fontsize=15)
plt.show()

image.png

19. 热力图plus

19.1. 普通热图

19.1.1. 正方形

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import os

# === 第一步:读取 Excel 文件 ===
file_path = "data.xlsx"
xls = pd.ExcelFile(file_path, engine="openpyxl")
df = xls.parse("Sheet1")

# === 清洗数据:去除字符串中的空格并尝试转换为数值 ===
for col in df.columns:
    df[col] = pd.to_numeric(df[col].astype(str).str.strip(), errors='coerce')
numeric_df = df.drop(columns=["Time"])  # 如无 "Time" 列请注释此行

# === 第二步:计算相关系数和 p 值 ===
cols = numeric_df.columns
corr_matrix = numeric_df.corr()
pval_matrix = pd.DataFrame(np.ones_like(corr_matrix), columns=cols, index=cols)

for i in range(len(cols)):
    for j in range(len(cols)):
        if i != j:
            corr, p = stats.pearsonr(numeric_df[cols[i]], numeric_df[cols[j]])
            pval_matrix.iloc[i, j] = p

# === 第三步:绘图(不使用 annot,而是手动 text)===
fig, ax = plt.subplots(figsize=(10, 8),dpi = 300)
cmap = sns.diverging_palette(120, 0, as_cmap=True)
# cmap = 'coolwarm'
sns.heatmap(
    corr_matrix,
    cmap=cmap,
    linewidths=0.5,
    vmin=-1,
    vmax=1,
    square=True,
    cbar=False,  # 手动添加 colorbar
    ax=ax
)

# === 添加 colorbar,并调整大小 ===
norm = plt.Normalize(vmin=-1, vmax=1)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax, shrink=0.6, aspect=20,
                    ticks=[-1, -0.5, 0, 0.5, 1])
cbar.set_label("Pearson Correlation", fontsize=10)
cbar.ax.tick_params(labelsize=12)

# === 手动添加相关系数数值和显著性星号(数字 + 星号分开)===
for i in range(len(cols)):
    for j in range(len(cols)):
        corr_val = corr_matrix.iloc[i, j]
        p = pval_matrix.iloc[i, j]
        # 相关系数文本
        ax.text(j + 0.5, i + 0.5, f"{corr_val:.2f}",
                ha='center', va='center', color='black',
                fontsize=11,fontweight='light')
        # 显著性星号
        star = ""
        if p < 0.001:
            star = "***"
        elif p < 0.01:
            star = "**"
        elif p < 0.05:
            star = "*"
        if star:
            ax.text(j + 0.5, i + 0.35, star,
                    ha='center', va='bottom', color='black',
                    fontsize=9,fontweight='bold')


# === 坐标轴设置 ===
ax.set_xticklabels(cols, fontsize=12, rotation=0, ha='center')
ax.set_yticklabels(cols, fontsize=12, rotation=0)
plt.title("Correlation Heatmap", fontsize=14)
plt.tight_layout()
plt.show()

20251109230529327

19.2. 上方块显著性下系数

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import os

# === 第一步:读取 Excel 文件 ===

file_path = "data.xlsx"
xls = pd.ExcelFile(file_path, engine="openpyxl")
df = xls.parse("Sheet1")
# === 清洗数据:去除字符串中的空格并尝试转换为数值 ===
for col in df.columns:
df[col] = pd.to_numeric(df[col].astype(str).str.strip(), errors='coerce')

numeric_df = df.drop(columns=["Time"]) # 如无 "Time" 列请注释此行

# === 第二步:计算相关系数和 p 值 ===
cols = numeric_df.columns
corr_matrix = numeric_df.corr()
pval_matrix = pd.DataFrame(np.ones_like(corr_matrix), columns=cols, index=cols)

for i in range(len(cols)):
for j in range(len(cols)):
if i != j:
corr, p = stats.pearsonr(numeric_df[cols[i]], numeric_df[cols[j]])
pval_matrix.iloc[i, j] = p

# === 第三步:绘图(不使用 annot,而是手动 text)===
fig, ax = plt.subplots(figsize=(10, 8),dpi = 300)
# cmap = plt.get_cmap('coolwarm')
cmap = sns.diverging_palette(120, 0, as_cmap=True)
# === 只显示上三角色块 ===
mask = np.tril(np.ones_like(corr_matrix, dtype=bool))
sns.heatmap(
corr_matrix,
mask=mask,
cmap=cmap,
linewidths=0.5,
linecolor='#a0a1a7',
vmin=-1,
vmax=1,
square=True,
cbar=False,
ax=ax
)

# === 添加 colorbar ===
norm = plt.Normalize(vmin=-1, vmax=1)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax, shrink=0.6, aspect=20, ticks=[-1, -0.5, 0, 0.5, 1])
cbar.set_label("Pearson Correlation", fontsize=14)
cbar.ax.tick_params(labelsize=12)

# === 手动添加文本 ===
for i in range(len(cols)):
for j in range(len(cols)):
corr_val = corr_matrix.iloc[i, j]
p = pval_matrix.iloc[i, j]
# 上三角(右上角):显著性星号
if i < j:
star = ""
if p < 0.001:
star = "***"
elif p < 0.01:
star = "**"
elif p < 0.05:
star = "*"
if star:
ax.text(j + 0.5, i + 0.5, star, ha='center', va='center', color='black', fontsize=13, fontweight='bold')
# 下三角(左下角):相关系数,颜色与色条对应
elif i > j:
color = cmap(norm(corr_val)) if not np.isnan(corr_val) else 'black'
ax.text(j + 0.5, i + 0.5, f"{corr_val:.2f}", ha='center', va='center', color=color, fontsize=12, fontweight='bold')
# 对角线:显示变量名
elif i == j:
ax.text(j + 0.5, i + 0.5, cols[i], ha='center', va='center', fontsize=12, color='black', fontweight='bold')

# === 坐标轴设置 ===
ax.set_xticks([])
ax.set_yticks([])
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xlabel('')
ax.set_ylabel('')
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
plt.title("Correlation Heatmap", fontsize=14)
plt.tight_layout()
plt.show()

20251109230937284

19.3. 气泡热力图(方块)

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import os

# === 第一步:读取 Excel 文件 ===
file_path = "data.xlsx"
xls = pd.ExcelFile(file_path, engine="openpyxl")
df = xls.parse("Sheet1")
# === 清洗数据:去除字符串中的空格并尝试转换为数值 ===
for col in df.columns:
df[col] = pd.to_numeric(df[col].astype(str).str.strip(), errors='coerce')

numeric_df = df.drop(columns=["Time"]) # 如无 "Time" 列请注释此行

# === 第二步:计算相关系数和 p 值 ===
cols = numeric_df.columns
corr_matrix = numeric_df.corr()
pval_matrix = pd.DataFrame(np.ones_like(corr_matrix), columns=cols, index=cols)

for i in range(len(cols)):
for j in range(len(cols)):
if i != j:
corr, p = stats.pearsonr(numeric_df[cols[i]], numeric_df[cols[j]])
pval_matrix.iloc[i, j] = p

# === 第三步:绘图(不使用 annot,而是手动 text)===
fig, ax = plt.subplots(figsize=(10, 8),dpi = 300)
cmap = sns.diverging_palette(120, 0, as_cmap=True)
# cmap = plt.get_cmap('coolwarm')
norm = plt.Normalize(vmin=-1, vmax=1)

# === 只显示白色方格和灰色边框 ===
sns.heatmap(
np.ones_like(corr_matrix), # 全1,配合白色cmap
cmap='Greys',
linewidths=1,
linecolor='grey',
vmin=1, vmax=1, # 保证格子为白色
square=True,
cbar=False,
ax=ax
)

# === 添加气泡 ===
for i in range(len(cols)):
for j in range(len(cols)):
corr_val = corr_matrix.iloc[i, j]
# 只在上三角画气泡
if not np.isnan(corr_val) and i < j:
size = 800 * abs(corr_val)
color = cmap(norm(corr_val))
ax.scatter(j + 0.5, i + 0.5, s=size, color=color, alpha=0.8, edgecolors='grey', linewidth=0.5, zorder=3)

sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax, shrink=0.6, aspect=20, ticks=[-1, -0.5, 0, 0.5, 1])
cbar.set_label("Pearson Correlation", fontsize=14)
cbar.ax.tick_params(labelsize=12)

# === 手动添加文本 ===
for i in range(len(cols)):
for j in range(len(cols)):
corr_val = corr_matrix.iloc[i, j]
p = pval_matrix.iloc[i, j]
# 上三角(右上角):显著性星号
if i < j:
star = ""
if p < 0.001:
star = "***"
elif p < 0.01:
star = "**"
elif p < 0.05:
star = "*"
if star:
ax.text(j + 0.5, i + 0.5, star, ha='center', va='center', color='black', fontsize=13, fontweight='bold', zorder=4)
# 下三角(左下角):相关系数,颜色与色条对应
elif i > j:
color = cmap(norm(corr_val)) if not np.isnan(corr_val) else 'black'
ax.text(j + 0.5, i + 0.5, f"{corr_val:.2f}", ha='center', va='center', color=color, fontsize=12, fontweight='bold', zorder=4)
# 对角线:显示变量名
elif i == j:
ax.text(j + 0.5, i + 0.5, cols[i], ha='center', va='center', fontsize=12, color='black', fontweight='bold', zorder=4)

# === 隐藏xy轴刻度和label ===
ax.set_xticks([])
ax.set_yticks([])
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xlabel('')
ax.set_ylabel('')
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
plt.title("Correlation Bubble Heatmap", fontsize=14)
plt.tight_layout()
plt.show()

20251109231108290

19.3.1. 2.保留上三角/下三角

19.4. 下三角

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import os

# === 第一步:读取 Excel 文件 ===
file_path = "data.xlsx"
xls = pd.ExcelFile(file_path, engine="openpyxl")
df = xls.parse("Sheet1")
numeric_df = df.drop(columns=["Time"]) # 如无 "Time" 列请注释此行

# === 第二步:计算相关系数和 p 值 ===
cols = numeric_df.columns
corr_matrix = numeric_df.corr()
pval_matrix = pd.DataFrame(np.ones_like(corr_matrix), columns=cols, index=cols)

for i in range(len(cols)):
for j in range(len(cols)):
if i != j:
corr, p = stats.pearsonr(numeric_df[cols[i]], numeric_df[cols[j]])
pval_matrix.iloc[i, j] = p

# === 只保留下三角 ===
mask_upper = np.triu(np.ones_like(corr_matrix, dtype=bool))
corr_matrix = corr_matrix.mask(mask_upper)
pval_matrix = pval_matrix.mask(mask_upper)

# === 绘图 ===
fig, ax = plt.subplots(figsize=(10, 8), dpi=300)
# cmap = 'coolwarm'
cmap = sns.diverging_palette(120, 0, as_cmap=True)
sns.heatmap(
corr_matrix,
cmap=cmap,
linewidths=0.5,
vmin=-1,
vmax=1,
square=True,
cbar=False,
ax=ax
)

# === 添加 colorbar(可选:控制大小)===
norm = plt.Normalize(vmin=-1, vmax=1)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
# 手动指定colorbar位置 [left, bottom, width, height],数值0~1为相对figure的比例
cbar_ax = fig.add_axes([0.79, 0.3, 0.03, 0.53]) # 你可以调整这四个参数
cbar = fig.colorbar(sm, cax=cbar_ax, shrink=1, aspect=20,
ticks=[-1, -0.5, 0, 0.5, 1])
cbar.set_label("Pearson Correlation", fontsize=14)
cbar.ax.tick_params(labelsize=12)


# === 添加文字(只对左下角非 NaN 位置)===
for i in range(len(cols)):
for j in range(len(cols)):
if not np.isnan(corr_matrix.iloc[i, j]):
corr_val = corr_matrix.iloc[i, j]
p = pval_matrix.iloc[i, j]
ax.text(j + 0.5, i + 0.5, f"{corr_val:.2f}",
ha='center', va='center', color='black',
fontsize=11, fontweight='light')
star = ""
if p < 0.001:
star = "***"
elif p < 0.01:
star = "**"
elif p < 0.05:
star = "*"
if star:
ax.text(j + 0.5, i + 0.35, star,
ha='center', va='bottom', color='black',
fontsize=9, fontweight='bold')
# 在对角线位置添加特征名
n = corr_matrix.shape[0]
for i in range(n):
ax.text(i + 0.5, i + 0.7, cols[i], ha='center', va='center',
fontsize=12, color='black')

# === 坐标轴设置 ===
# 获取所有标签
x_labels = cols
y_labels = cols

# 设置x轴标签,最后一列不显示
ax.set_xticks(np.arange(len(x_labels)-1) + 0.5) # 调整刻度位置
ax.set_xticklabels(x_labels[:-1], fontsize=12, rotation=0, ha='right')

# 设置y轴标签,第一行不显示
ax.set_yticks(np.arange(len(y_labels)-1) + 1.5) # 调整刻度位置
ax.set_yticklabels(y_labels[1:], fontsize=12, rotation=0)

plt.show()

20251109231314388

19.5. 上三角

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import os

# === 第一步:读取 Excel 文件 ===
file_path = "data.xlsx"
xls = pd.ExcelFile(file_path, engine="openpyxl")
df = xls.parse("Sheet1")
# === 清洗数据:去除字符串中的空格并尝试转换为数值 ===
for col in df.columns:
df[col] = pd.to_numeric(df[col].astype(str).str.strip(), errors='coerce')

numeric_df = df.drop(columns=["Time"]) # 如无 "Time" 列请注释此行

# === 第二步:计算相关系数和 p 值 ===
cols = numeric_df.columns
corr_matrix = numeric_df.corr()
pval_matrix = pd.DataFrame(np.ones_like(corr_matrix), columns=cols, index=cols)

for i in range(len(cols)):
for j in range(len(cols)):
if i != j:
corr, p = stats.pearsonr(numeric_df[cols[i]], numeric_df[cols[j]])
pval_matrix.iloc[i, j] = p

# === 只保留上三角 ===
mask_lower = np.tril(np.ones_like(corr_matrix, dtype=bool))
corr_matrix = corr_matrix.mask(mask_lower)
pval_matrix = pval_matrix.mask(mask_lower)

# === 绘图 ===
fig, ax = plt.subplots(figsize=(10, 8),dpi=300)
# cmap = 'coolwarm'
cmap = sns.diverging_palette(120, 0, as_cmap=True)
sns.heatmap(
corr_matrix,
cmap=cmap,
linewidths=0.5,
vmin=-1,
vmax=1,
square=True,
cbar=False,
ax=ax
)

# === 添加 colorbar ===
norm = plt.Normalize(vmin=-1, vmax=1)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
# 手动指定colorbar位置 [left, bottom, width, height]
cbar_ax = fig.add_axes([0.92, 0.15, 0.02, 0.7]) # 调整位置和大小
cbar = fig.colorbar(sm, cax=cbar_ax, ticks=[-1, -0.5, 0, 0.5, 1])
cbar.set_label("Pearson Correlation", fontsize=12, labelpad=10)
cbar.ax.tick_params(labelsize=10)

# === 添加文字(只对右上角非 NaN 位置)===
for i in range(len(cols)):
for j in range(len(cols)):
if not np.isnan(corr_matrix.iloc[i, j]):
corr_val = corr_matrix.iloc[i, j]
p = pval_matrix.iloc[i, j]
ax.text(j + 0.5, i + 0.5, f"{corr_val:.2f}",
ha='center', va='center', color='black',
fontsize=11, fontweight='light')
star = ""
if p < 0.001:
star = "***"
elif p < 0.01:
star = "**"
elif p < 0.05:
star = "*"
if star:
ax.text(j + 0.5, i + 0.35, star,
ha='center', va='bottom', color='black',
fontsize=9, fontweight='bold')

# 在对角线位置添加特征名
n = corr_matrix.shape[0]
for i in range(n):
ax.text(i + 0.5, i + 0.5, cols[i], ha='center', va='center',
fontsize=12, color='black')

# === 坐标轴设置 ===
# 获取所有标签
x_labels = cols
y_labels = cols

# 设置x轴标签(顶部),第一列不显示
ax.set_xticks(np.arange(len(x_labels)-1) + 1.5) # 调整刻度位置
ax.set_xticklabels(x_labels[1:], fontsize=12, rotation=0, ha='left')
ax.xaxis.set_ticks_position('top') # 将x轴刻度移到顶部

# 设置y轴标签(右侧),最后一行不显示
ax.set_yticks(np.arange(len(y_labels)-1) + 0.5) # 调整刻度位置
ax.set_yticklabels(y_labels[:-1], fontsize=12, rotation=0)
ax.yaxis.set_ticks_position('right') # 将y轴刻度移到右侧

plt.show()

20251109231413490

19.6. 圆形

import pandas as pd
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import os
from matplotlib import cm
from matplotlib.patches import Rectangle

# === 读取 Excel 文件 ===
file_path = "data.xlsx"
xls = pd.ExcelFile(file_path, engine="openpyxl")
df = xls.parse("Sheet1")
# === 清洗数据:去除字符串中的空格并尝试转换为数值 ===
for col in df.columns:
df[col] = pd.to_numeric(df[col].astype(str).str.strip(), errors='coerce')

numeric_df = df.drop(columns=["Time"]) # 如无 "Time" 列请注释此行

# === 计算相关系数与 p 值矩阵 ===
cols = numeric_df.columns
n = len(cols)
corr_matrix = numeric_df.corr()
pval_matrix = pd.DataFrame(np.ones_like(corr_matrix), columns=cols, index=cols)

for i in range(n):
for j in range(n):
if i != j:
corr, p = stats.pearsonr(numeric_df[cols[i]], numeric_df[cols[j]])
pval_matrix.iloc[i, j] = p

# === 准备绘图数据(仅左下角) ===
x_list, y_list, color_list, size_list, star_list, label_list = [], [], [], [], [], []

for i in range(n):
for j in range(n):
r = corr_matrix.iloc[i, j]
p = pval_matrix.iloc[i, j]
x_list.append(j)
y_list.append(i)
color_list.append(r)
size_list.append(abs(r) * 1500) # 圆大小与绝对值相关
label_list.append(f"{r:.2f}")
if p < 0.001:
star_list.append("***")
elif p < 0.01:
star_list.append("**")
elif p < 0.05:
star_list.append("*")
else:
star_list.append("")

# === 绘图 ===
fig, ax = plt.subplots(figsize=(10, 8), dpi=300)
cmap = sns.diverging_palette(120, 0, as_cmap=True)
sc = ax.scatter(x_list, y_list, s=size_list, c=color_list,
cmap=cmap, vmin=-1, vmax=1, edgecolors='black')


# 添加浅灰色格子边框
for i in range(n):
for j in range(n):
ax.add_patch(Rectangle((j - 0.5, i - 0.5), 1, 1,
edgecolor='lightgray', facecolor='none', lw=0.7))

# 添加相关系数数字和星号
for x, y, text, star in zip(x_list, y_list, label_list, star_list):
ax.text(x, y, text, fontsize=10, ha='center',
va='center', color='black')
if star:
ax.text(x, y - 0.4, star, fontsize=9,
ha='center', va='top', color='black', fontweight='bold')

# 添加 colorbar
cbar = plt.colorbar(sc, ax=ax, shrink=0.6, aspect=20,
ticks=[-1, -0.5, 0, 0.5, 1])
cbar.set_label("Pearson Correlation", fontsize=12)
cbar.ax.tick_params(labelsize=10)

# 设置坐标轴标签
ax.set_xticks(range(n))
ax.set_yticks(range(n))
ax.set_xticklabels(cols, rotation=0, ha='right', fontsize=11)
ax.set_yticklabels(cols, fontsize=11)
ax.set_xlim(-0.5, n - 0.5)
ax.set_ylim(-0.5, n - 0.5)
ax.set_title("Correlation Matrix (Circles, Lower Triangle)", fontsize=14)

ax.invert_yaxis() # 让 y 轴从上到下排列变量
ax.set_aspect('equal')
plt.tight_layout()
plt.show()

20251109231951029

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import os

# === 第一步:读取 Excel 文件 ===
file_path = "data.xlsx"
xls = pd.ExcelFile(file_path, engine="openpyxl")
df = xls.parse("Sheet1")
# === 清洗数据:去除字符串中的空格并尝试转换为数值 ===
for col in df.columns:
df[col] = pd.to_numeric(df[col].astype(str).str.strip(), errors='coerce')

numeric_df = df.drop(columns=["Time"]) # 如无 "Time" 列请注释此行

# === 第二步:计算相关系数和 p 值 ===
cols = numeric_df.columns
corr_matrix = numeric_df.corr()
pval_matrix = pd.DataFrame(np.ones_like(corr_matrix), columns=cols, index=cols)

for i in range(len(cols)):
for j in range(len(cols)):
if i != j:
corr, p = stats.pearsonr(numeric_df[cols[i]], numeric_df[cols[j]])
pval_matrix.iloc[i, j] = p

# === 第三步:绘图(不使用 annot,而是手动 text)===
fig, ax = plt.subplots(figsize=(10, 8),dpi = 300)
cmap = sns.diverging_palette(120, 0, as_cmap=True)
# cmap = plt.get_cmap('coolwarm')
norm = plt.Normalize(vmin=-1, vmax=1)

# === 只显示白色方格和灰色边框 ===
sns.heatmap(
np.ones_like(corr_matrix), # 全1,配合白色cmap
cmap='Greys',
linewidths=1,
linecolor='grey',
vmin=1, vmax=1, # 保证格子为白色
square=True,
cbar=False,
ax=ax
)

# === 添加气泡 ===
for i in range(len(cols)):
for j in range(len(cols)):
corr_val = corr_matrix.iloc[i, j]
# 只在上三角画气泡
if not np.isnan(corr_val) and i < j:
size = 800 * abs(corr_val)
color = cmap(norm(corr_val))
ax.scatter(j + 0.5, i + 0.5, s=size, color=color, alpha=0.8, edgecolors='grey', linewidth=0.5, zorder=3)

sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax, shrink=0.6, aspect=20, ticks=[-1, -0.5, 0, 0.5, 1])
cbar.set_label("Pearson Correlation", fontsize=14)
cbar.ax.tick_params(labelsize=12)

# === 手动添加文本 ===
for i in range(len(cols)):
for j in range(len(cols)):
corr_val = corr_matrix.iloc[i, j]
p = pval_matrix.iloc[i, j]
# 上三角(右上角):显著性星号
if i < j:
star = ""
if p < 0.001:
star = "***"
elif p < 0.01:
star = "**"
elif p < 0.05:
star = "*"
if star:
ax.text(j + 0.5, i + 0.5, star, ha='center', va='center', color='black', fontsize=13, fontweight='bold', zorder=4)
# 下三角(左下角):相关系数,颜色与色条对应
elif i > j:
color = cmap(norm(corr_val)) if not np.isnan(corr_val) else 'black'
ax.text(j + 0.5, i + 0.5, f"{corr_val:.2f}", ha='center', va='center', color=color, fontsize=12, fontweight='bold', zorder=4)
# 对角线:显示变量名
elif i == j:
ax.text(j + 0.5, i + 0.5, cols[i], ha='center', va='center', fontsize=12, color='black', fontweight='bold', zorder=4)

# === 隐藏xy轴刻度和label ===
ax.set_xticks([])
ax.set_yticks([])
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_xlabel('')
ax.set_ylabel('')
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
plt.title("Correlation Bubble Heatmap", fontsize=14)
plt.tight_layout()
plt.show()

20251109232036158

20. 统计图散点图plus

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import os
# === 数据读取 ===
file_path = "data.xlsx"
df = pd.read_excel(file_path, engine="openpyxl")
# === 清洗数据:去除字符串中的空格并尝试转换为数值 ===
for col in df.columns:
df[col] = pd.to_numeric(df[col].astype(str).str.strip(), errors='coerce')

numeric_df = df.select_dtypes(include=[np.number]) # 只保留数值型变量
cols = numeric_df.columns
n = len(cols)

# === 计算相关系数和 p 值矩阵 ===
corr_matrix = numeric_df.corr()
pval_matrix = pd.DataFrame(np.ones_like(corr_matrix), columns=cols, index=cols)
for i in range(n):
for j in range(n):
if i != j:
corr, p = stats.pearsonr(numeric_df[cols[i]], numeric_df[cols[j]])
pval_matrix.iloc[i, j] = p
# === 开始绘图 ===
fig, axes = plt.subplots(n, n, figsize=(2*n, 2*n), dpi=300)
plt.subplots_adjust(wspace=0.1, hspace=0.1)
cmap = sns.diverging_palette(120, 0, as_cmap=True)
norm = plt.Normalize(vmin=-1, vmax=1)
for i in range(n):
for j in range(n):
ax = axes[i, j]

# === 对角线:绘制变量分布 ===
if i == j:
sns.histplot(numeric_df[cols[i]], kde=True,
ax=ax, color='#54873e', edgecolor=None)


# === 下三角:回归拟合图 ===
elif i > j:
sns.regplot(x=cols[j], y=cols[i], data=numeric_df, ax=ax,
scatter_kws={'s': 10, 'color': '#e8b0c0'},
line_kws={'color': '#d53f6c'}, ci=95)
ax.set_xticks([])
ax.set_yticks([])
# === 上三角:绘制色块 + 相关系数文字 + 显著性 ===
else:
corr_val = corr_matrix.iloc[i, j]
p = pval_matrix.iloc[i, j]
color = cmap(norm(corr_val)) if not np.isnan(corr_val) else 'white'
ax.set_facecolor(color)
# 相关系数文字
ax.text(0.5, 0.6, f"{corr_val:.2f}", ha='center', va='center',
fontsize=14, fontweight='bold', color='black', transform=ax.transAxes)
# 显著性星号
if p < 0.001:
stars = '***'
elif p < 0.01:
stars = '**'
elif p < 0.05:
stars = '*'
else:
stars = ''
ax.text(0.5, 0.35, stars, ha='center', va='center',
fontsize=14, fontweight='bold', color='black', transform=ax.transAxes)
ax.set_xticks([])
ax.set_yticks([])

# === 设置坐标轴标签 ===
for i in range(n):
for j in range(n):
ax = axes[i, j]
# 仅保留必要的刻度线
if i == n-1: # 最底行加 x 标签
ax.set_xlabel(cols[j], fontsize=14, rotation=0,fontweight='bold')
ax.set_xticks([]) # 保持x刻度为空但显示xlabel
else:
ax.set_xticks([])
ax.set_xlabel('')
if j == 0: # 最左列加 y 标签
ax.set_ylabel(cols[i], fontsize=14, rotation=0, labelpad=35,fontweight='bold')
ax.set_yticks([]) # 保持y刻度为空但显示ylabel
else:
ax.set_yticks([])
ax.set_ylabel('')

# 特别处理左上角(i=0,j=0)的ylabel
if i == 0 and j == 0:
ax.set_ylabel(cols[i], fontsize=14, rotation=0, labelpad=35,fontweight='bold')
ax.set_yticks([])

# === 添加 colorbar ===
fig.subplots_adjust(right=0.92)
cbar_ax = fig.add_axes([0.94, 0.25, 0.02, 0.5])
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
cbar = fig.colorbar(sm, cax=cbar_ax,ticks=[-1, -0.5, 0, 0.5, 1])
cbar.ax.tick_params(labelsize=14) # 设置刻度字体大小为 14,可按需调整

cbar.set_label("Pearson Correlation", fontsize=16)
# plt.tight_layout(rect=[0, 0, 0.93, 0.96])
plt.show()

20251109232146489