Python模仿ggplot2的绘图包:plotnine

Python中有一个模仿R ggplot2语法创建的绘图包,plotnine.官网在这:

https://plotnine.readthedocs.io/en/stable/

1 安装

pip install plotinie

2 使用

from plotnine import *
from plotnine.data import mtcars
mtcars.head()
##                 name   mpg  cyl   disp   hp  ...   qsec  vs  am  gear  carb
## 0          Mazda RX4  21.0    6  160.0  110  ...  16.46   0   1     4     4
## 1      Mazda RX4 Wag  21.0    6  160.0  110  ...  17.02   0   1     4     4
## 2         Datsun 710  22.8    4  108.0   93  ...  18.61   1   1     4     1
## 3     Hornet 4 Drive  21.4    6  258.0  110  ...  19.44   1   0     3     1
## 4  Hornet Sportabout  18.7    8  360.0  175  ...  17.02   0   0     3     2
## 
## [5 rows x 12 columns]

需要注意的是,在python中,+要放在每一句的前面而不是后面.

(ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)'))
 + geom_point()
 + stat_smooth(method='lm')
 + facet_wrap('~gear'))
## <ggplot: (-9223372036762410300)>
## 
## D:\software\python\lib\site-packages\plotnine\scales\scale.py:93: MatplotlibDeprecationWarning: 
## The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
##   if cbook.iterable(self.breaks) and cbook.iterable(self.labels):
## D:\software\python\lib\site-packages\numpy\core\fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
##   return ptp(axis=axis, out=out, **kwargs)
## D:\software\python\lib\site-packages\numpy\core\fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
##   return ptp(axis=axis, out=out, **kwargs)
## D:\software\python\lib\site-packages\numpy\core\fromnumeric.py:2389: FutureWarning: Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.
##   return ptp(axis=axis, out=out, **kwargs)
## D:\software\python\lib\site-packages\plotnine\utils.py:553: MatplotlibDeprecationWarning: 
## The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
##   return cbook.iterable(var) and not is_string(var)

可以看到跟ggplot2语法几乎一致,非常方便,大大减小了学习成本.

当然也可以将生成的图片赋值给变量.

plot = (ggplot(mtcars)
+ geom_point(aes(x = "wt", y = "mpg", colour = "gear"))
+ theme_bw()
)
plot
## <ggplot: (-9223372036754654152)>
## 
## D:\software\python\lib\site-packages\plotnine\scales\scale.py:93: MatplotlibDeprecationWarning: 
## The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
##   if cbook.iterable(self.breaks) and cbook.iterable(self.labels):
## D:\software\python\lib\site-packages\plotnine\utils.py:553: MatplotlibDeprecationWarning: 
## The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
##   return cbook.iterable(var) and not is_string(var)

但是需要注意:

ggplot开始,到绘图代码结束,都需要放在一个小括号里面.

语法跟ggplot2几乎是相同的,真的是非常方便了!!

当然,问题也是有的,就是没有像R中一样的,ggplot2的扩展包了,比如ggraph等.

Avatar
Xiaotao Shen
Postdoctoral Research Fellow

Metabolomics, Multi-omics, Bioinformatics, Systems Biology.

Related

Next
Previous
comments powered by Disqus