python 可視化包-plotnine

plotnine

CSDN的編輯和上傳圖片體驗太差,更多的例子到plot-example裏去看。

https://github.com/has2k1/plotnine-examples

安裝

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())

png

<ggplot: (143520728433)>
# 不同性狀
#ggplot(data = mpg) + 
#  geom_point(mapping = aes(x = displ, y = hwy, shape = class))
(ggplot(mpg,aes('displ','hwy',shape='class'))+geom_point())

png

<ggplot: (143521318874)>
#ggplot(data = mpg) + 
#  geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
(ggplot(mpg,aes('displ','hwy'))
 +geom_point(color='blue'))

png

<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))

png

<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'))

png

<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"

png

<ggplot: (143520360694)>

柱狀圖

#ggplot(data = diamonds) + 
#  geom_bar(mapping = aes(x = cut))
(ggplot(mpg)
 +geom_bar(mapping=aes('displ')))

png

<ggplot: (-9223371893334758340)>
(ggplot(mpg)
 +stat_count(mapping=aes('displ')))

png

<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__))

png

<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__))

png

<ggplot: (-9223371893333687145)>
(ggplot(mpg) + 
  geom_point(mapping = aes('displ', 'hwy'), position = "jitter"))

png

<ggplot: (143521035633)>

箱線圖

(ggplot(mpg,aes('class','hwy')) + 
  geom_boxplot())

png

<ggplot: (143521206683)>
#ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
#  geom_boxplot() +
#  coord_flip()

(ggplot(mpg,aes('class','hwy')) 
 + geom_boxplot()
 + coord_flip())

png

<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())

png

<ggplot: (-9223371893333735999)>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章