简体   繁体   English

创建多列值计数的 Altair 图表

[英]Create Altair Chart of Value Counts of Multiple Columns

Using Altair charting, how can I create a chart of value_counts() of multiple columns?使用 Altair 图表,如何创建多列的 value_counts() 图表? This is easily done by matplotlib. How can the identical chart be created using Altair? matplotlib 很容易做到这一点。如何使用 Altair 创建相同的图表?

import matplotlib.pyplot as plt 
import pandas as pd 

df = pd.DataFrame({'Col1':[0,1,2,3], 
               'Col2':[0,1,2,2], 
               'Col3':[2,3,3,3]}) 

pd.DataFrame({col:df[col].value_counts(normalize=True) for col in df}).plot(kind='bar')

在此处输入图像描述

You could do this:你可以这样做:

import pandas as pd 
import altair as alt


df = pd.DataFrame({
    'Col1':[0,1,2,3], 
    'Col2':[0,1,2,2], 
    'Col3':[2,3,3,3]
}).melt(var_name='column')

alt.Chart(df).mark_bar().encode(
    x='column',
    y='count()',
    column='value:O',
    color='column'
)

在此处输入图像描述

In the next major release of Altair you can use the offset channels instead of faceting as in this example.在 Altair 的下一个主要版本中,您可以使用偏移通道,而不是像本示例中那样进行分面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM