生成差异分析的标准可视化图表:
import matplotlib.pyplot as plt
import seaborn as sns
fig, ax = plt.subplots(figsize=(10, 6))
res['significant'] = (res['padj'] < 0.05) & (abs(res['log2FoldChange']) > 1)
sns.scatterplot(
data=res,
x='log2FoldChange',
y='-np.log10(padj)',
hue='significant',
palette={False: 'gray', True: '#8B5CF6'},
ax=ax
)
top_genes = sig_genes.nsmallest(10, 'padj')
for _, gene in top_genes.iterrows():
ax.annotate(gene.name, (gene['log2FoldChange'], -np.log10(gene['padj'])))
ax.set_xlabel('Log2 Fold Change')
ax.set_ylabel('-Log10(Adjusted P-value)')
ax.set_title('Volcano Plot')
plt.savefig('volcano_plot.png', dpi=300, bbox_inches='tight')
fig, ax = plt.subplots(figsize=(10, 6))
sns.scatterplot(
data=res,
x='baseMean',
y='log2FoldChange',
hue='significant',
palette={False: 'gray', True: '#8B5CF6'},
ax=ax
)
ax.axhline(y=0, color='red', linestyle='--')
ax.set_xlabel('Mean of Normalized Counts')
ax.set_ylabel('Log2 Fold Change')
ax.set_title('MA Plot')
plt.savefig('ma_plot.png', dpi=300, bbox_inches='tight')
normalized_counts = dds.normalized_counts
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca_result = pca.fit_transform(normalized_counts.T)
fig, ax = plt.subplots(figsize=(8, 6))
sns.scatterplot(
x=pca_result[:, 0],
y=pca_result[:, 1],
hue=sample_info['condition'],
palette={'control': '#3B82F6', 'treated': '#8B5CF6'},
ax=ax
)
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_title('PCA Plot')
plt.savefig('pca_plot.png', dpi=300, bbox_inches='tight')
print("可视化图表已保存!")