yhat ggplot facet_wrap 类型错误

yhat ggplot facet_wrap type error

我有一个 pandas dataframe 是我通过 txt 文件读入的:

training = pd.read_csv('training_data.txt')

这些是列:

>> print training.columns.values`  
['segment' 'cookie_id' 'num_visits' 'num_page_views']

我对按段显示 num_page_views 密度的图表感兴趣,如下所示:

plot = ggplot(training, aes(x='num_visits')) + geom_density()
        + xlim(0,20) + facet_wrap( ~ 'segment')  
print plot

它给出以下 错误:

TypeError Traceback (most recent call last) in () 1 ----> 2 plot = ggplot(training, aes(x='num_visits')) + geom_density() +xlim(0,20) +facet_wrap( ~ 'segment') 3 print plot

TypeError: bad operand type for unary ~: 'str'

在yhat ggplot中,facet选项不带“~”。您可以在 documentation 中找到它(为方便起见在此处复制):

import pandas as pd

meat_lng = pd.melt(meat, id_vars=['date'])

p = ggplot(aes(x='date', y='value'), data=meat_lng)
p + geom_point() + \
    stat_smooth(colour="red") + \
    facet_wrap("variable")

p + geom_hist() + facet_wrap("color")

p = ggplot(diamonds, aes(x='price'))
p + geom_density() + \
    facet_grid("cut", "clarity")

p = ggplot(diamonds, aes(x='carat', y='price'))
p + geom_point(alpha=0.25) + \
    facet_grid("cut", "clarity")