如何使用Altair显示百分比直方图而不是计数 [英] How to show a histogram of percentages instead of counts using Altair

查看:154
本文介绍了如何使用Altair显示百分比直方图而不是计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Altair和Pandas获取总百分比的直方图,而不是计数的直方图?



我现在有这个:





我这样做的目的是

  d = {'age':['12','32','43','54','32','32', '12']} 
dfTest = pd.DataFrame(data = d)

alt.Chart(dfTest).mark_bar()。encode(
alt.X( age :Q,bin = True),
y ='count()',


解决方案

您可以使用






编辑:这是我最初的答案,则要复杂得多:



这并不完全简单,因为它需要手动指定当前编码所隐含的bin和合计转换,然后再进行一次计算转换来计算百分比。下面是一个示例:

 将熊猫作为pd 
导入altair作为alt

来源= pd.DataFrame({'age':['12','32','43','54','32','32','12']})

alt .chart(source).transform_bin(
['age_min','age_max'],
field ='age',
).transform_aggregate(
count ='count() ',
groupby = ['age_min','age_max']
).transform_joinaggregate(
total ='sum(count)'
).transform_calculate(
pct ='datum.count / datum.total'
).mark_bar()。encode(
alt.X( age_min:Q,bin ='binned'),
x2 =' age_max',
y = alt.Y('pct:Q',axis = alt.Axis(format ='%'))



我希望我们能将来可以简化转换API。


How do I get a histogram of percentages of total instead of a histogram of count using Altair and Pandas?

I have this at the moment:

Which I got by doing this:

d = {'age': ['12', '32', '43', '54', '32', '32', '12']}
dfTest = pd.DataFrame(data=d)

alt.Chart(dfTest).mark_bar().encode(
    alt.X("age:Q", bin=True),
    y='count()',
)

解决方案

You can do this with a Join Aggregate transform followed by a Calculate transform:

import pandas as pd
import altair as alt

source = pd.DataFrame({'age': ['12', '32', '43', '54', '32', '32', '12']})

alt.Chart(source).transform_joinaggregate(
    total='count(*)'
).transform_calculate(
    pct='1 / datum.total'
).mark_bar().encode(
    alt.X('age:Q', bin=True),
    alt.Y('sum(pct):Q', axis=alt.Axis(format='%'))
)


Edit: this was my initial answer, which is much more complicated:

It's not entirely straightforward, because it requires manually specifying the bin and aggregate transforms currently implied by your encoding, followed by a calculate transform to compute the percentages. Here is an example:

import pandas as pd
import altair as alt

source = pd.DataFrame({'age': ['12', '32', '43', '54', '32', '32', '12']})

alt.Chart(source).transform_bin(
    ['age_min', 'age_max'],
    field='age',
).transform_aggregate(
    count='count()',
    groupby=['age_min', 'age_max']
).transform_joinaggregate(
    total='sum(count)'  
).transform_calculate(
    pct='datum.count / datum.total'  
).mark_bar().encode(
    alt.X("age_min:Q", bin='binned'),
    x2='age_max',
    y=alt.Y('pct:Q', axis=alt.Axis(format='%'))
)

I'm hoping that we'll be able to streamline the transform API in the future.

这篇关于如何使用Altair显示百分比直方图而不是计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆