在 Plotnine 官方网站中,提供了一些比较经典的案例,如果能够将这些案例分析清楚,对提高 Plotnine 的应用技能大有裨益。本课就选择两个案例抛砖引玉,供参考。
3.3.1 柱形图及坐标轴设置
本案例是关于柱形图和对其坐标轴设置,案例中核心类为 plotnine.geoms.geom_col(点击这里详见官方地址)。
还是先创建数据集:
import pandas as pd
import numpy as np
from plotnine import *
df = pd.DataFrame({
'variable': ['gender', 'gender', 'age', 'age', 'age', 'income', 'income', 'income', 'income'],
'category': ['Female', 'Male', '1-24', '25-54', '55+', 'Lo', 'Lo-Med', 'Med', 'High'],
'value': [60, 40, 50, 30, 20, 10, 25, 25, 40],
})
df['variable'] = pd.Categorical(df['variable'], categories=[&