plotnine
CSDN的编辑和上传图片体验太差,更多的例子到plot-example里去看。
https://github.com/has2k1/plotnine-examples
安装
- 官网:https://plotnine.readthedocs.io/en/stable/index.html
- github:https://github.com/has2k1/plotnine
- ggplot2官网:http://ggplot2.tidyverse.org/reference/index.html#section-plot-basics
conda install -c conda-forge plotnine
import pandas as pd
import numpy as np
from plotnine import *
from plotnine.data import *
mpg.head(2)
Unnamed: 0 | manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
1 | 2 | audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
基础图形
参考R for Data Science:http://r4ds.had.co.nz/data-visualisation.html 中的Data visualisation
散点图
# R 写法
# (ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy)))
# plotnine写法
(ggplot(mpg,aes('displ','hwy'))+geom_point())
# ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy, color = class))
(ggplot(mpg,aes('displ','hwy',color='class'))+geom_point())
#ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy, size = class))
(ggplot(mpg,aes('displ','hwy',size='class'))+geom_point())
# 透明度
# ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy, alpha = class)))
(ggplot(mpg,aes('displ','hwy',alpha='class'))+geom_point())
<ggplot: (143520728433)>
# 不同性状
#ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy, shape = class))
(ggplot(mpg,aes('displ','hwy',shape='class'))+geom_point())
<ggplot: (143521318874)>
#ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
(ggplot(mpg,aes('displ','hwy'))
+geom_point(color='blue'))
<ggplot: (-9223371893333956145)>
面板图
#ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy)) +
# facet_wrap(~ class, nrow = 2)
(ggplot(mpg,aes('displ','hwy'))
+geom_point()
+facet_wrap('~ class', nrow = 2))
<ggplot: (-9223371893333857329)>
#ggplot(data = mpg) +
# geom_point(mapping = aes(x = displ, y = hwy)) +
# facet_grid(drv ~ cyl)
(ggplot(mpg,aes('displ','hwy'))
+geom_point()
+facet_wrap('~ drv + cyl'))
<ggplot: (-9223371893334132974)>
拟合曲线
还有一些问题
#ggplot(data = mpg) +
# geom_smooth(mapping = aes(x = displ, y = hwy))
(ggplot(mpg,aes('displ','hwy'))
+geom_point()
+geom_smooth(mapping=aes('displ','hwy')))
D:\Anaconda3\lib\site-packages\plotnine\stats\smoothers.py:150: UserWarning: Confidence intervals are not yet implementedfor lowess smoothings.
warnings.warn("Confidence intervals are not yet implemented"
<ggplot: (143520360694)>
柱状图
#ggplot(data = diamonds) +
# geom_bar(mapping = aes(x = cut))
(ggplot(mpg)
+geom_bar(mapping=aes('displ')))
<ggplot: (-9223371893334758340)>
(ggplot(mpg)
+stat_count(mapping=aes('displ')))
<ggplot: (143521000803)>
(ggplot(data = mpg) +
geom_bar(mapping = aes('displ', fill = 'class'),position='fill'))
D:\Anaconda3\lib\site-packages\plotnine\positions\position.py:194: UserWarning: position_fill requires non-overlapping x intervals
warn(msg.format(cls.__name__))
<ggplot: (-9223371893334407676)>
(ggplot(data = mpg) +
geom_bar(mapping = aes('displ', fill = 'class'),position='dodge'))
D:\Anaconda3\lib\site-packages\plotnine\positions\position.py:194: UserWarning: position_dodge requires non-overlapping x intervals
warn(msg.format(cls.__name__))
<ggplot: (-9223371893333687145)>
(ggplot(mpg) +
geom_point(mapping = aes('displ', 'hwy'), position = "jitter"))
<ggplot: (143521035633)>
箱线图
(ggplot(mpg,aes('class','hwy')) +
geom_boxplot())
<ggplot: (143521206683)>
#ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
# geom_boxplot() +
# coord_flip()
(ggplot(mpg,aes('class','hwy'))
+ geom_boxplot()
+ coord_flip())
<ggplot: (-9223371893333525721)>
# bar <- ggplot(data = diamonds) +
# geom_bar(
# mapping = aes(x = cut, fill = cut),
# show.legend = FALSE,
# width = 1
# ) +
# theme(aspect.ratio = 1) +
# labs(x = NULL, y = NULL)
# bar + coord_flip()
# bar + coord_polar()
(ggplot(mpg)
+ geom_bar(mapping=aes('class',fill='class'))
+ coord_flip())
<ggplot: (-9223371893333735999)>