教你怎么用python繪制dotplot
一、前言
R語(yǔ)言不少庫(kù)都可以方便的畫(huà)dotplot,但是低頻使用R這么多年,我依舊覺(jué)得R不是一門(mén)真正的編程語(yǔ)言。目前,在python中繪制dotplot貌似沒(méi)有很輕量、方便的庫(kù),因此工作之余寫(xiě)了這個(gè)python_dotplot包,方便自己也希望能夠方便他人吧。
二、安裝
可以通過(guò)pypi快速安裝:
pip install python_dotplot
該package當(dāng)然可能存在一定的bug,所以也會(huì)處于不斷迭代的過(guò)程中,可以通過(guò)以下方式獲得特定或最新版本
pip install python_dotplot --upgrade pip install python_dotplot==0.0.1b1
如果通過(guò)--upgrade參數(shù)不能獲得最新版本,國(guó)內(nèi)鏡像會(huì)有一定的延遲,可指定pypi官方源指定鏡像:
pip install -i https://pypi.python.org/pypi python_dotplot
三、模塊導(dǎo)入
import dotplot import dotplot.utils import pandas as pd %config InlineBackend.figure_format = 'retina' # 如果你的電腦設(shè)備是視網(wǎng)膜屏,可指定該參數(shù)渲染jupyter圖像,會(huì)超清晰,超好看
包的層級(jí)結(jié)構(gòu)很簡(jiǎn)單,主要包括以下模塊:
dotplot
├── cmap.py # 自定義color map
├── core.py # 實(shí)現(xiàn)了Dotplot類(lèi),用于封裝數(shù)據(jù)以及繪圖
├── hierarchical.py # 實(shí)現(xiàn)了層次聚類(lèi),用于支持dotplot行和列通過(guò)層次聚類(lèi)進(jìn)行自動(dòng)排序
├── __init__.py # 初始化模塊
└── utils.py # 實(shí)用函數(shù),目前是夾帶私貨,我自己用的預(yù)處理函數(shù),也許對(duì)其他人也有用
四、數(shù)據(jù)準(zhǔn)備
我們首先需要準(zhǔn)備一個(gè)數(shù)據(jù),這里要求輸入必須是一個(gè)tidy data格式的pandas Dataframe,簡(jiǎn)而言之,tidy data是指在該數(shù)據(jù)框中每一行是一個(gè)觀測(cè),每一列是一個(gè)屬性,下面以示例數(shù)據(jù)為例:
term_list = ['GO:0002455', 'GO:0006958', 'GO:0006956', 'GO:0038096','GO:0002673',
'GO:0051251', 'GO:0060333', 'GO:0006910','GO:0002483', 'GO:0002440',
'GO:0009141', 'GO:0009123', 'GO:0006119', 'GO:0009260', 'GO:0015985', 'GO:0015986', 'GO:0006260',
'GO:0044843', 'GO:0061621', 'GO:0061718']
up = pd.read_csv('./example_data/group1.csv', header=0, index_col=0)
down = pd.read_csv('./example_data/group2.csv', header=0, index_col=0)
data = dotplot.utils.merge_clusterprofile_results(dataframes=(up, down), groups=['B6_up', 'B6_down'], term_list=term_list)
data.head()
| Description | GeneRatio | BgRatio | pvalue | p.adjust | qvalue | geneID | Count | group | |
| ID | |||||||||
| GO:0002455 | humoral immune response mediated by circulatin... | 22/178 | 150/18670 | 19.365993 | 16.222197 | 16.298589 | HLA-DQB1/CD55/IGHM/PTPRC/TRBC2/IGHG2/IGKV3-20/... | 22 | B6_up |
| GO:0006958 | complement activation, classical pathway | 20/178 | 137/18670 | 17.588789 | 14.989062 | 15.065454 | CD55/IGHM/TRBC2/IGHG2/IGKV3-20/IGHV4-34/IGHV3-... | 20 | B6_up |
| GO:0006956 | complement activation | 20/178 | 175/18670 | 15.453684 | 13.008859 | 13.085251 | CD55/IGHM/TRBC2/IGHG2/IGKV3-20/IGHV4-34/IGHV3-... | 20 | B6_up |
| GO:0038096 | Fc-gamma receptor signaling pathway involved i... | 18/178 | 139/18670 | 14.916693 | 12.675988 | 12.752379 | PTPRC/LYN/IGHG2/IGKV3-20/IGHV4-34/IGHV3-30/IGL... | 18 | B6_up |
| GO:0002673 | regulation of acute inflammatory response | 18/178 | 159/18670 | 13.871614 | 11.817674 | 11.894066 | HLA-E/CD55/IGHG2/IGKV3-20/IGHV4-34/IGHV3-30/IG... | 18 | B6_up |
五、畫(huà)圖
首先我們可以借助 DotPlot的類(lèi)方法parse_from_tidy_data 對(duì)數(shù)據(jù)進(jìn)行封裝,然后直接調(diào)用plot函數(shù)進(jìn)行繪圖。當(dāng)然,你也可以通過(guò)DotPlot的構(gòu)造函數(shù)__init__()來(lái)實(shí)例化DotPlot對(duì)象。
- 一維數(shù)據(jù)展示
new_keys = {'item_key': 'Description','group_key': 'group','sizes_key': 'Count'}
dp = dotplot.DotPlot.parse_from_tidy_data(data, **new_keys)
sct = dp.plot(size_factor=10, cmap='Reds') # 通過(guò)size_factor 調(diào)節(jié)圖中點(diǎn)的大小

dp = dotplot.DotPlot.parse_from_tidy_data(data, item_key='Description', group_key='group', sizes_key='Count') # 該效果完全同上,這是python語(yǔ)言特性 sct = dp.plot(size_factor=10, cmap='Reds')

- 二維數(shù)據(jù)展示
我們可以通過(guò)color_key指定data中的列做顏色映射。
new_keys = {'item_key': 'Description','group_key': 'group','sizes_key': 'Count','color_key': 'pvalue'}
dp = dotplot.DotPlot.parse_from_tidy_data(data, **new_keys)
sct = dp.plot(size_factor=10, cmap='Reds', cluster_row=True)

- 三維數(shù)據(jù)展示
可以通過(guò)circle_key增加一列作為虛線圓圈的映射。
DEFAULT_CLUSTERPROFILE_KEYS = {
'item_key': 'Description', 'group_key': 'group',
'sizes_key': 'Count', 'color_key': 'pvalue',
'circle_key': 'qvalue'
}
dp = dotplot.DotPlot.parse_from_tidy_data(data, **DEFAULT_CLUSTERPROFILE_KEYS)
sct = dp.plot(size_factor=10, cmap='Reds', cluster_row=True)

當(dāng)然,更多的參數(shù)我們可以通過(guò)signature來(lái)查看,我對(duì)這些參數(shù)都做了類(lèi)型注釋?zhuān)瑧?yīng)該是通俗易懂的:
?dp.plot
Signature:
dp.plot(
size_factor:float=15,
vmin:float=0,
vmax:float=None,
path:Union[os.PathLike, NoneType]=None,
cmap:Union[str, matplotlib.colors.Colormap]='Reds',
cluster_row:bool=False,
cluster_col:bool=False,
cluster_kws:Union[Dict, NoneType]=None,
**kwargs,
)
Docstring:
:param size_factor: `size factor` * `value` for the actually representation of scatter size in the final figure
:param vmin: `vmin` in `matplotlib.pyplot.scatter`
:param vmax: `vmax` in `matplotlib.pyplot.scatter`
:param path: path to save the figure
:param cmap: color map supported by matplotlib
:param kwargs: dot_title, circle_title, colorbar_title, dot_color, circle_color
other kwargs are passed to `matplotlib.Axes.scatter`
:param cluster_row, whether to cluster the row
:param cluster_col, whether to cluster the col
:param cluster_kws, key args for cluster, including `cluster_method`, `cluster_metric`, 'cluster_n'
:return:
因此,我們可以通過(guò)關(guān)鍵字參數(shù)修改圖例中的部分組件:
sct = dp.plot(size_factor=10, cmap='Reds', cluster_row=True, dot_title = 'Count', circle_title='-log10(qvalue)', colorbar_title = '-log10(pvalue)')

六、寫(xiě)在篇末
dotplot在數(shù)據(jù)可視化中是一個(gè)強(qiáng)有力的展示方式,選擇一個(gè)合適的可視化方式勝過(guò)千言萬(wàn)語(yǔ)
最后,最適合的可視化方式是最直觀、最簡(jiǎn)潔的,不是炫技,別被花里胡哨的可視化所迷住雙眼而忽略了信息的傳達(dá)。
到此這篇關(guān)于教你怎么用python繪制dotplot的文章就介紹到這了,更多相關(guān)python繪制dotplot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
六種酷炫Python運(yùn)行進(jìn)度條效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了六種酷炫Python運(yùn)行進(jìn)度條的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Python?Flask框架實(shí)現(xiàn)小紅書(shū)圖片無(wú)水印解析下載
這篇文章主要為大家介紹了Python?Flask框架實(shí)現(xiàn)小紅書(shū)圖片無(wú)水印解析下載,需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Python連接SQLite數(shù)據(jù)庫(kù)并進(jìn)行增冊(cè)改查操作方法詳解
這篇文章主要介紹了Python對(duì)SQLite數(shù)據(jù)庫(kù)進(jìn)行增冊(cè)改查操作方法詳解,需要的朋友可以參考下2020-02-02
Linux下通過(guò)python獲取本機(jī)ip方法示例
這篇文章主要給大家介紹了關(guān)于在Linux下通過(guò)python獲取本機(jī)ip的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Pandas數(shù)據(jù)類(lèi)型自行變換及數(shù)據(jù)類(lèi)型轉(zhuǎn)換失敗問(wèn)題分析與解決
這篇文章主要介紹了Pandas數(shù)據(jù)類(lèi)型自行變換及數(shù)據(jù)類(lèi)型轉(zhuǎn)換失敗問(wèn)題分析與解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Python實(shí)現(xiàn)的生成自我描述腳本分享(很有意思的程序)
這篇文章主要介紹了Python實(shí)現(xiàn)的生成自我描述腳本分享,很有意思的程序,繞的人有點(diǎn)頭暈,需要的朋友參考下吧2014-07-07
利用python進(jìn)行接口測(cè)試及類(lèi)型介紹
這篇文章主要介紹了利用python進(jìn)行接口測(cè)試詳情,文章基于python展開(kāi)對(duì)接口測(cè)試的詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05

