Li's Bioinfo-Blog
  • |
  • 主页
  • 分类
  • 标签
  • 归档
  • 关于
  • 搜索
Home » Tags

Python

Python-可视化-statannotations包为sns绘图注释显著性

参考 https://github.com/trevismd/statannotations/blob/master/usage/example.ipynb https://github.com/trevismd/statannotations/tree/master 1 2 3 4 5 6 7 8 9 10 11 12 import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # Box plots -- sns.boxplot() # Bar plots -- sns.barplot() # Swarm plots -- sns.swarmplot() # Strip plots -- sns.stripplot() # Violin plots -- sns.violinplot() # Supporting FacetGrid -- sns.catplot(col=..., row=...) from statannotations.Annotator import Annotator 1. Basic use 1 2 3 4 5 6 7 8 9 10 11 x = "day" y = "total_bill" order = ['Sun', 'Thur', 'Fri', 'Sat'] ax = sns.boxplot(data=df, x=x, y=y, order=order) pairs = [("Thur", "Fri"), ("Thur", "Sat"), ("Fri", "Sun")] annot = Annotator(ax, pairs, data=df, x=x, y=y, order=order) annot.configure(test='t-test_ind', text_format='star', loc='outside', verbose=2) # annot.apply_test() # ax, test_results = annot.annotate() ax, test_results = annot.apply_and_annotate() Tips: sns.plot的data, x, y, order等绘图参数需要与Annotator的保持一致。 ...

Create:&nbsp;<span title='2025-07-14 00:00:00 +0000 UTC'>2025-07-14</span>&nbsp;|&nbsp;Update:&nbsp;2025-07-14&nbsp;|&nbsp;Words:&nbsp;1165&nbsp;|&nbsp;3 min&nbsp;|&nbsp;Lishensuo

Python-可视化-sns绘制热图&聚类热图

1 2 3 4 import matplotlib.pyplot as plt import seaborn as sns import numpy as np import pandas as pd 1. sns.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 # glue = sns.load_dataset("glue").pivot(index="Model", columns="Task", values="Score") # https://github.com/mwaskom/seaborn-data/blob/master/glue.csv glue = pd.read_csv("./data/glue.csv").pivot(index="Model", columns="Task", values="Score") glue.head() # Task CoLA MNLI MRPC QNLI QQP RTE SST-2 STS-B # Model # BERT 60.5 86.7 89.3 92.7 72.1 70.1 94.9 87.6 # BiLSTM 11.6 65.6 81.8 74.6 62.5 57.4 82.8 70.3 # BiLSTM+Attn 18.6 67.6 83.9 74.3 60.1 58.4 83.0 72.8 # BiLSTM+CoVe 18.5 65.4 78.7 70.8 60.6 52.7 81.9 64.4 # BiLSTM+ELMo 32.1 67.2 84.7 75.5 61.1 57.4 89.3 70.3 # 1) plt.figure(figsize=(4, 3)) sns.heatmap(glue) plt.show() # 2) 为单元格添加注释数据 plt.figure(figsize=(4, 3)) sns.heatmap(glue, annot=True, # fmt=".1f" # 可以设置数据格式 # annot=glue.rank(axis=1) # 也可以自定注释内容 ) 1 2 3 4 5 6 7 8 9 # 3) 设置边框 plt.figure(figsize=(4, 3)) sns.heatmap(glue, annot=True, linewidth=.5, linecolor="white", square=True) # 4) 指定色域以及映射范围 plt.figure(figsize=(4, 3)) sns.heatmap(glue, cmap="crest", vmin=50, vmax=100) # 最后也可以通过cbar系列参数调整颜色条 ...

Create:&nbsp;<span title='2025-07-28 00:00:00 +0000 UTC'>2025-07-28</span>&nbsp;|&nbsp;Update:&nbsp;2025-07-28&nbsp;|&nbsp;Words:&nbsp;1238&nbsp;|&nbsp;3 min&nbsp;|&nbsp;Lishensuo

Python-可视化-sns绘制回归点图

1 2 3 4 5 import seaborn as sns import matplotlib.pyplot as plt import numpy as np import pandas as pd 1. sns.regplot 适合展示简单的两组关系 1 2 3 4 5 6 mpg = sns.load_dataset('mpg') # 1) 一般用法 sns.regplot(data=mpg, x="weight", y="acceleration") # sns.regplot(data=mpg, x="weight", y="acceleration", ci=None) # 不显示置信区间 # sns.regplot(data=mpg, x="weight", y="acceleration", ci=99) # 自定义置信区间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 # 2) 修改scatter点与line线的显示细节 sns.regplot( data=mpg, x="weight", y="acceleration", scatter_kws={ 'color': 'blue', # 散点颜色 's': 50, # 点的大小(面积) 'alpha': 0.6, # 透明度 'marker': 'o', # 点的样式,比如 'o', 'x', '^', etc. 'linewidths': 0 # 点的轮廓线 } ) sns.regplot( data=mpg, x="weight", y="acceleration", line_kws={ 'color': 'red', # 回归线颜色 'linewidth': 2, # 线宽 'linestyle': '--', # 线型:'-'、'--'、':'、'-.' 等 'alpha': 0.9 # 回归线透明度 } ) 1 2 3 4 5 6 7 # 3) 对具有"离散"性质的变量(数据类型仍是float/int), 为避免重叠可以设置抖动 sns.regplot(data=mpg, x="cylinders", y="weight", x_jitter=.15) # 4) 线的拟合方式 sns.regplot(data=mpg, x="weight", y="mpg", order=2) # 拟合高阶函数 # sns.regplot(data=mpg, x="horsepower", y="weight", robust=True) # 不受离群点影响 # sns.regplot(data=mpg, x="horsepower", y="mpg", lowess=True) # 拟合平滑曲线 2. sns.lmplot 适用于分组线性回归, 即分面绘图 1 2 3 tips = sns.load_dataset("tips") sns.lmplot(x="total_bill", y="tip", hue="sex", data=tips) 1 2 sns.lmplot(x="total_bill", y="tip", col="day", data=tips, facet_kws=dict(sharex=False, sharey=False),) ...

Create:&nbsp;<span title='2025-08-04 00:00:00 +0000 UTC'>2025-08-04</span>&nbsp;|&nbsp;Update:&nbsp;2025-08-04&nbsp;|&nbsp;Words:&nbsp;620&nbsp;|&nbsp;2 min&nbsp;|&nbsp;Lishensuo

Shell_Python_R脚本基础

1、shell脚本 1.1 脚本执行 (1)命令行直接执行 如下示例:脚本第一行设置脚本解释器的路径,如写错或没写,系统会调用默认解释器执行。然后在运行脚本时,需要先赋予可执行权限。(下同) ...

Create:&nbsp;<span title='2023-03-25 00:00:00 +0000 UTC'>2023-03-25</span>&nbsp;|&nbsp;Update:&nbsp;2023-03-25&nbsp;|&nbsp;Words:&nbsp;734&nbsp;|&nbsp;2 min&nbsp;|&nbsp;Lishensuo
« Prev Page
© 2025 Li's Bioinfo-Blog Powered by Hugo & PaperMod
您是本站第 位访问者,总浏览量为 次