简体   繁体   中英

Create Altair Chart of Value Counts of Multiple Columns

Using Altair charting, how can I create a chart of value_counts() of multiple columns? This is easily done by matplotlib. How can the identical chart be created using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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