R語言不少庫都可以方便的畫dotplot,但是低頻使用R這么多年,我依舊覺得R不是一門真正的編程語言。目前,在python中繪制dotplot貌似沒有很輕量、方便的庫,因此工作之余寫了這個(gè)python_dotplot包,方便自己也希望能夠方便他人吧。
可以通過pypi快速安裝:
pip install python_dotplot
該package當(dāng)然可能存在一定的bug,所以也會(huì)處于不斷迭代的過程中,可以通過以下方式獲得特定或最新版本
pip install python_dotplot --upgrade pip install python_dotplot==0.0.1b1
如果通過--upgrade
參數(shù)不能獲得最新版本,國內(nèi)鏡像會(huì)有一定的延遲,可指定pypi官方源指定鏡像:
pip install -i https://pypi.python.org/pypi python_dotplot
import dotplot import dotplot.utils import pandas as pd %config InlineBackend.figure_format = 'retina' # 如果你的電腦設(shè)備是視網(wǎng)膜屏,可指定該參數(shù)渲染jupyter圖像,會(huì)超清晰,超好看
包的層級結(jié)構(gòu)很簡單,主要包括以下模塊:
dotplot
├── cmap.py # 自定義color map
├── core.py # 實(shí)現(xiàn)了Dotplot
類,用于封裝數(shù)據(jù)以及繪圖
├── hierarchical.py # 實(shí)現(xiàn)了層次聚類,用于支持dotplot行和列通過層次聚類進(jìn)行自動(dòng)排序
├── __init__.py
# 初始化模塊
└── utils.py # 實(shí)用函數(shù),目前是夾帶私貨,我自己用的預(yù)處理函數(shù),也許對其他人也有用
我們首先需要準(zhǔn)備一個(gè)數(shù)據(jù),這里要求輸入必須是一個(gè)tidy data格式的pandas Dataframe,簡而言之,tidy data是指在該數(shù)據(jù)框中每一行是一個(gè)觀測,每一列是一個(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 |
首先我們可以借助 DotPlot
的類方法parse_from_tidy_data
對數(shù)據(jù)進(jìn)行封裝,然后直接調(diào)用plot
函數(shù)進(jìn)行繪圖。當(dāng)然,你也可以通過DotPlot
的構(gòu)造函數(shù)__init__()
來實(shí)例化DotPlot
對象。
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') # 通過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語言特性 sct = dp.plot(size_factor=10, cmap='Reds')
我們可以通過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)
可以通過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ù)我們可以通過signature來查看,我對這些參數(shù)都做了類型注釋,應(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ān)鍵字參數(shù)修改圖例中的部分組件:
sct = dp.plot(size_factor=10, cmap='Reds', cluster_row=True, dot_title = 'Count', circle_title='-log10(qvalue)', colorbar_title = '-log10(pvalue)')
dotplot在數(shù)據(jù)可視化中是一個(gè)強(qiáng)有力的展示方式,選擇一個(gè)合適的可視化方式勝過千言萬語
最后,最適合的可視化方式是最直觀、最簡潔的,不是炫技,別被花里胡哨的可視化所迷住雙眼而忽略了信息的傳達(dá)。
到此這篇關(guān)于教你怎么用python繪制dotplot的文章就介紹到這了,更多相關(guān)python繪制dotplot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:臨汾 陽泉 雙鴨山 金華 日照 克拉瑪依 貴州 赤峰
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《教你怎么用python繪制dotplot》,本文關(guān)鍵詞 教你,怎么,用,python,繪制,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。