Skip to main content

首页 / Posts

Python Code for Drawing a Heatmap

Example Python code for drawing a correlation heatmap for data analysis and chart presentation.

Rosetears·
··236 words·2 mins

Python Code for Drawing a Heatmap
#

 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
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.font_manager as fm

# Create data
data = {
    "Performance expectancy": [1, 0.712, 0.687, 0.651, 0.451, 0.743, 0.646, 0.696, 0.684],
    "Effort expectancy": [0.712, 1, 0.745, 0.731, 0.499, 0.701, 0.618, 0.745, 0.665],
    "Facilitating conditions": [0.687, 0.745, 1, 0.817, 0.544, 0.781, 0.766, 0.713, 0.801],
    "Social influence": [0.651, 0.731, 0.817, 1, 0.602, 0.757, 0.691, 0.744, 0.759],
    "Perceived risk": [0.451, 0.499, 0.544, 0.602, 1, 0.57, 0.593, 0.589, 0.576],
    "Hedonic motivation": [0.743, 0.701, 0.781, 0.757, 0.57, 1, 0.752, 0.806, 0.782],
    "Price value": [0.646, 0.618, 0.766, 0.691, 0.593, 0.752, 1, 0.716, 0.834],
    "Personal innovativeness": [0.696, 0.745, 0.713, 0.744, 0.589, 0.806, 0.716, 1, 0.779],
    "Behavioral intention": [0.684, 0.665, 0.801, 0.759, 0.576, 0.782, 0.834, 0.779, 1]
}

# Convert the data to a DataFrame
df = pd.DataFrame(data, index=["Performance expectancy", "Effort expectancy", "Facilitating conditions", "Social influence", "Perceived risk", "Hedonic motivation", "Price value", "Personal innovativeness", "Behavioral intention"])

# Set the font
plt.rcParams['font.family'] = 'Microsoft YaHei'

# Set the plot size
plt.figure(figsize=(10, 8))

# Draw the heatmap
sns.heatmap(df, annot=True, cmap='coolwarm', center=0, linewidths=0.5, fmt=".3f", cbar_kws={'label': 'Correlation'})

# Set the title
plt.title("Correlation Analysis Heatmap")

# Display the heatmap
plt.show()

Related

【SPSS】Correlation Analysis

··499 words·3 mins
This article introduces how to use SPSS for correlation analysis and how to interpret correlation matrices and heatmaps, helping readers quickly master correlation-analysis methods and result interpretation among scale dimensions.

【SPSS】Difference Analysis

··1475 words·7 mins
This article explains independent-samples t tests and one-way analysis of variance (ANOVA) in difference analysis, including SPSS operation steps, homogeneity-of-variance testing, t-value analysis, and result interpretation, helping readers understand how to compare mean differences between groups in data analysis.

【SPSS】Normality Test

··268 words·2 mins
This article introduces how to use SPSS for descriptive statistical analysis, focusing on how to test normal distribution with histograms and Q-Q plots, helping users better understand data distributions and the analysis process.

【SPSS】Validity Analysis

··1081 words·6 mins
This article introduces common SPSS validity-analysis methods, including exploratory factor analysis, principal component analysis, and confirmatory factor analysis. It explains how to interpret the KMO test and factor loadings to help readers understand validity assessment in data analysis.

【SPSS】Descriptive Statistics

··3196 words·16 mins
SPSS descriptive statistics: including single-choice questions, multiple-choice questions, and crosstabs.