春江暮客

春江暮客的个人学习分享网站

seaborn画小提琴图(violin plot)

2019-01-04 技术
seaborn画小提琴图(violin plot)

简介

小提琴图是用来展示多组数据的分布状态以及概率密度。跟箱线图类似,但是可以密度层面展示更好。在数据量非常大不方便一个一个展示的时候小提琴图特别适用。而python里面的seaborn包可以很方便的画出小提琴图。

如果你只想看中位数、四分位数,箱线图通常就够了;如果你还想同时看出数据分布是否偏斜、是否有双峰趋势,小提琴图会更直观。

参数

《seaborn画小提琴图(violin plot)》

小提琴图各位置对应参数,中间一条就是箱线图数据,25%,50%,75%位置,细线区间为95%置信区间。

seaborn实现画小提琴图

单变量数据

如果只有一个变量的话使用箱线图就好了,但是使用小提琴图当然也是可以的

    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set(color_codes=True)
    sns.set_style("white")
    df = sns.load_dataset('iris')
    sns.violinplot( y=df["sepal_length"] )
    plt.show()

《seaborn画小提琴图(violin plot)》

单变量数据多组

当数据有多个组的时候,使用分组来比较

    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set(color_codes=True)
    sns.set_style("white")
    df = sns.load_dataset('iris')
    sns.violinplot( x=df["species"], y=df["sepal_length"] )
    plt.show()

《seaborn画小提琴图(violin plot)》

多变量比较

小提琴图比较多个变量也是非常方便的,这里使用前4个变量比较。

    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set(color_codes=True)
    sns.set_style("white")
    df = sns.load_dataset('iris')
    sns.violinplot(data=df.iloc[:,0:4])
    plt.show()

《seaborn画小提琴图(violin plot)》

使用时的小提示

  • 分组特别多时,小提琴图会变得拥挤,这时可以考虑旋转坐标轴标签或改用箱线图
  • 如果样本量太小,小提琴图展示出来的密度形状可能会误导判断
  • 最稳妥的做法通常是把小提琴图和箱线图、散点图结合起来看

总结

到此已经把python使用seaborn包话小提琴图(violin plot)都介绍完了,如果想要更详细的文档,请参考https://seaborn.pydata.org/generated/seaborn.violinplot.html

标签

1024 12306 2fa ablang adsense agents.md ai ai-agent ai-agents ai-seo algorithm amp antibodies apparmor argparse automation backup bash batch-processing binarycif bioinformatics biopython blockchain boltz bootstrapping boxes bubblewrap c-index cca cdn chatgpt checkpoint cli cloudflare codex cofoldarena concurrency configparser configuration copy cprofile cpu监控 cron csv cuda curl data-leakage data-processing data-quality data-validation datascience datavisualization datetime debugging decimal deployment desktop-app devtools difflib disk-space disown dns docker dovecot download electron encoding esm esm2 esm3 esmc esmfold2 etag evaluation faceswap fasta fastmcp faulthandler ffmpeg file-descriptors file-io file-management filecmp filenames files flashppi flask folium frontend game generator git github-actions google google-research gpt-6.1-sol grep gzip harness heapq hls html http hugo imports indexnow ipaddress jailbreak javascript jev json jsonl just k-means kaggle langfuse leecode linux list litellm llm llms.txt logging logs lollipop lsof m3u8 machine-learning macos manacher matplotlib mcp memory mirror mmcif model-evaluation monitoring mp3 mp4 mpnn multiomics multiprocessing mutation mysql nanobert nanobodies networking networkx nginx normalize numpy ollama omegatherm pandas password pathlib pdb pep-723 performance permissions phaser pillow pip postfix preprocessing print prompt-engineering protein-alignment protein-design protein-embeddings protein-interactions protein-language-models protein-stability protein-structure proxy pydantic pyecharts pyqt python python3 r raincloud reproducibility requests reservoir-sampling rfoptimization rg ripgrep rosettafold3 roundcube rrsi rsi rsync s-tui sampling scale scikit-learn scrapy screen seaborn security selenium seo sequence-identity sha256 shell signals sitemap sklearn socket solana somaticsignatures sorting spl sqlite ssh standardize static-site subprocess symlink sysadmin system-one tarfile tcp tempfile tensorflow tkinter tls toml totp tracemalloc tron tronpy troubleshooting turtle typesafe-ai ubuntu unicode urllib usdt utf-8 uv venv vhhbert vite web-tools webp wordcloud wordpress workflow xml yaml zipfile zoneinfo 后台 寓言 概率 经济 贸易 迅雷解析 钱包

友情链接

其它