plotnine

文章目录

  • 一、基础
    • 1. 基本语法
    • 2. 各种图
  • 二、参数
    • 1. 常用参数
    • 2. 文本
      • 标签与标题
      • 数据标签
      • 图例
      • 标注
    • 3. 坐标轴
      • 坐标范围
      • 离散刻度
      • 连续刻度
      • 时间刻度
      • 数学刻度
      • 坐标旋转
    • 4. 多图
      • grid
      • wrap
    • 5. 主题
      • 参数
      • 预设主题
  • 三、导出
    • 1. 单张导出
    • 2. 多张导出




一、基础

1. 基本语法

ggplot() + geom(data, aes()) + labels()
geom为几何对象,一个图中可添加多个几何对象。
aes控制视觉表现,将数据映射到坐标轴或颜色等。

from plotnine import *
from plotnine.data import *

p = (ggplot() +
     geom_point(mtcars, aes(x='mpg', y='wt', color='factor(cyl)')) +
     ggtitle(r'$Title$'))

print(p)

2. 各种图

# 折线图 geom_line
p = (ggplot(mtcars, aes(x='hp', y='mpg')) +
     geom_line())
print(p)

# 散点图 geom_point
p = (ggplot(mtcars, aes(x='hp', y='mpg')) +
     geom_point())
print(p)

# 点线图 point + line
p = (ggplot(mtcars, aes(x='hp', y='mpg', group='factor(gear)')) +
     geom_line() +
     geom_point())
print(p)

# 带平滑线的散点图 point + smooth
p = (ggplot(mtcars, aes(x='hp', y='mpg')) +
     geom_point() +
     stat_smooth(se=True, fill='red', method='lm'))
print(p)

# 条形图 geom_bar
# stat = count identity
# count进行统计,需要一个参数
# identity使用原始数据值,需要两个参数
p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(stat="count", position='stack'))
print(p)
# position = stack dodge fill
p = (ggplot(mtcars, aes(x='gear', y='hp', fill='factor(cyl)')) +
     geom_bar(stat="identity", width=0.5, position='dodge'))
print(p)

# 直方图 geom_histogram
p = (ggplot(mtcars, aes(x='mpg')) +
     geom_histogram(bins=15))
print(p)

# 密度图 geom_density
p = (ggplot(mtcars, aes(x='mpg')) +
     geom_density())
print(p)

# 箱线图 geom_boxplot
p = (ggplot(mtcars, aes(x='gear', y='hp', fill='factor(cyl)')) +
     geom_boxplot())
print(p)




二、参数

1. 常用参数

# 颜色 大小 形状 透明度
p = (ggplot() +
     geom_point(mtcars, aes(x='wt', y='mpg'), color='b', size=2) +
     geom_point(mtcars, aes(x='wt', y='cyl'), shape='s', alpha=0.5))
print(p)

# 填充
p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(position='dodge'))
print(p)

# 分组
p = (ggplot(mtcars, aes(x='gear', group='cyl')) +
     geom_bar(position='dodge'))
print(p)

2. 文本

标签与标题

p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(position='dodge') +
     xlab('X') +
     ylab('Y') +
     ggtitle('Title'))
print(p)

数据标签

median_age_dict = {
     'Country': ['New Zealand', 'Spain', 'Ireland', 'Israel'],
                   'Age': [39.0, 37.0, 35.0, 34.0]}
median_age = pd.DataFrame(median_age_dict)

p = (ggplot(median_age, aes(x='Country', y='Age')) +
     geom_bar(stat='identity') +
     geom_text(aes(x='Country', y='Age', label='Age')))
     
print(p)

图例

# 图例颜色
p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(stat="count", position='dodge') +
     scale_fill_manual(values=("#56B4E9", "#F4B5E3", "#A2A4E5")))
print(p)

# 图例隐藏(1)
p = (ggplot(mtcars, aes(x='hp', y='mpg', fill='gear')) +
     geom_point(show_legend=False))
print(p)
# 图例隐藏(2)
p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(stat="count", position='dodge') +
     theme(legend_position='none'))
print(p)

标注

# 本文标注
p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(stat="count", position='dodge') +
     annotate("text", x=4, y=10, label="Annotate"))
print(p)

# 图形标注
p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(stat="count", position='dodge') +
     annotate("rect", xmin=5, xmax=6, ymin=6, ymax=7, alpha=0.1, colour="blue"))
print(p)

3. 坐标轴

坐标范围

p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(stat="count", position='dodge') +
     xlim(2, 6) +
     ylim(0, 15))
print(p)

离散刻度

median_age_dict = {
     'Country': ['New Zealand', 'Spain', 'Ireland', 'Israel'],
                   'Age': [39.0, 37.0, 35.0, 34.0]}
median_age = pd.DataFrame(median_age_dict)

# 刻度顺序和名字
p = (ggplot(median_age, aes(x='Country', y='Age')) +
     geom_bar(stat='identity') +
     scale_x_discrete(limits=('Spain', 'Ireland', 'New Zealand', 'Israel'),
                      labels=('City1', 'City 2', 'City 3', 'City 4')))

print(p)

连续刻度

p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(stat="count", position='dodge') +
     scale_y_continuous(limits=(0, 15), breaks=np.linspace(0, 15, 6)))
print(p)

时间刻度

Date = {
     'year': ['20200101', '20200201', '20200301', '20200401'],
        'number': [4, 5, 6, 5]}
Date = pd.DataFrame(Date)

# breaks = 7 days, 3 months, 4 weeks, 1 years
p = (ggplot(Date) +
     geom_point(aes(x='year', y='number')) +
     scale_x_date(breaks='1 months', date_labels='%Y-%m-%d'))

print(p)

数学刻度

p = (ggplot(mtcars, aes(x='hp', y='mpg')) +
     geom_point() +
     scale_x_log10() +
     scale_y_log10())
print(p)

坐标旋转

p = (ggplot(mtcars, aes(x='gear', fill='factor(cyl)')) +
     geom_bar(stat="count", position='dodge') +
     coord_flip())
print(p)

4. 多图

grid

# ~cyl 以cyl分列
# cyl~ 以cyl分行
# gear~cyl 组合分行列
p = (ggplot(mtcars, aes(x='cyl', fill='factor(gear)')) +
     geom_bar() +
     facet_grid('gear~cyl'))
print(p)

wrap

# ncol nrow
p = (ggplot(mtcars, aes(x='cyl', fill='factor(gear)')) +
     geom_bar() +
     facet_wrap('~gear+cyl', ncol=4))
print(p)

5. 主题

参数

# 常用参数
p = (ggplot(mtcars, aes(x='hp', y='mpg')) +
     geom_point() +
     theme(legend_position='none',
           axis_text_x=element_text(color='darkred', size=12),
           axis_text_y=element_text(color='darkred', size=12),
           axis_title=element_blank(),
           dpi= 150,
           figure_size=(8, 8)))

print(p)

预设主题

分类 主题 描述
白色 theme_void() 白,无轴,无线
  theme_minimal() 白,无轴,灰线
  theme_light() 白,浅灰轴,灰线
  theme_bw() 白,深灰轴,灰线
  theme_linedraw() 白,黑轴,灰线
  theme_classic() 白,黑xy轴,无线
灰色 theme_gray() 浅灰,无轴,白线
  theme_seaborn() 浅蓝,无轴,白线
  theme_dark() 深灰,无轴,灰线
漫画 theme_xkcd() 白,黑轴,无线




三、导出

1. 单张导出

base_plot = (ggplot(mtcars, aes(x='hp', y='mpg')) +
             geom_point())

base_plot.save('test.pdf', width=20, height=15, dpi = 300)

2. 多张导出

base_plot = (ggplot(mtcars, aes(x='hp', y='mpg')) +
             geom_point())

plots = [base_plot + ggtitle('%d of 3' % i) for i in range(1, 4)]
save_as_pdf_pages(filename='test.pdf', plots=plots)

你可能感兴趣的:(Python)