具有多个条件的 Altair 分组条形图
Altair Grouped Bar Chart With Multiple Conditions
我有一个名为 table
的 DataFrame:
TERM Bitcoin S&P500 Real Estate Gold
0 High-Inflation/ Short term 3097.94 -3700.78 761.23 6512.71
1 High-Inflation/ Mid term — -3080.01 -8434.66 3242.40
2 High-Inflation/ Long term — -2089.25 -9117.96 8174.43
3 Low-Inflation/ Short term 780200.00 -273.71 1824.72 2214.51
4 Low-Inflation/ Mid term 21013600.00 5331.40 35810.58 -2879.37
5 Low-Inflation/ Long term 978017143.00. 15045.41 35895.81 861.90
我想制作一个分组(或堆叠)条形图,根据 TERM
列区分每个资产的 return 投资。我试过这个:
alt.Chart(table).transform_fold(
["Bitcoin", "S&P500", "Real Estate", "Gold"], as_=["key", "value"]
).mark_bar().encode(
x="key:N",
y="value:Q",
color="key:N",
column="TERM",
)
但这不起作用。
它似乎工作正常,唯一的问题是这些值的比例差异很大,只有最大的才会出现在线性比例上。您可以通过切换到 symlog
比例来解决这个问题。例如:
import pandas as pd
import io
import altair as alt
table = pd.read_csv(io.StringIO("""\
row TERM Bitcoin S&P500 "Real Estate" Gold
0 "High-Inflation/ Short term" 3097.94 -3700.78 761.23 6512.71
1 "High-Inflation/ Mid term" — -3080.01 -8434.66 3242.40
2 "High-Inflation/ Long term" — -2089.25 -9117.96 8174.43
3 "Low-Inflation/ Short term" 780200.00 -273.71 1824.72 2214.51
4 "Low-Inflation/ Mid term" 21013600.00 5331.40 35810.58 -2879.37
5 "Low-Inflation/ Long term" 978017143.00. 15045.41 35895.81 861.90
"""), delim_whitespace=True)
alt.Chart(table).transform_fold(
["Bitcoin", "S&P500", "Real Estate", "Gold"], as_=["key", "value"]
).mark_bar().encode(
x="key:N",
y=alt.Y("value:Q", scale=alt.Scale(type='symlog')),
color="key:N",
column="TERM",
)
我有一个名为 table
的 DataFrame:
TERM Bitcoin S&P500 Real Estate Gold
0 High-Inflation/ Short term 3097.94 -3700.78 761.23 6512.71
1 High-Inflation/ Mid term — -3080.01 -8434.66 3242.40
2 High-Inflation/ Long term — -2089.25 -9117.96 8174.43
3 Low-Inflation/ Short term 780200.00 -273.71 1824.72 2214.51
4 Low-Inflation/ Mid term 21013600.00 5331.40 35810.58 -2879.37
5 Low-Inflation/ Long term 978017143.00. 15045.41 35895.81 861.90
我想制作一个分组(或堆叠)条形图,根据 TERM
列区分每个资产的 return 投资。我试过这个:
alt.Chart(table).transform_fold(
["Bitcoin", "S&P500", "Real Estate", "Gold"], as_=["key", "value"]
).mark_bar().encode(
x="key:N",
y="value:Q",
color="key:N",
column="TERM",
)
但这不起作用。
它似乎工作正常,唯一的问题是这些值的比例差异很大,只有最大的才会出现在线性比例上。您可以通过切换到 symlog
比例来解决这个问题。例如:
import pandas as pd
import io
import altair as alt
table = pd.read_csv(io.StringIO("""\
row TERM Bitcoin S&P500 "Real Estate" Gold
0 "High-Inflation/ Short term" 3097.94 -3700.78 761.23 6512.71
1 "High-Inflation/ Mid term" — -3080.01 -8434.66 3242.40
2 "High-Inflation/ Long term" — -2089.25 -9117.96 8174.43
3 "Low-Inflation/ Short term" 780200.00 -273.71 1824.72 2214.51
4 "Low-Inflation/ Mid term" 21013600.00 5331.40 35810.58 -2879.37
5 "Low-Inflation/ Long term" 978017143.00. 15045.41 35895.81 861.90
"""), delim_whitespace=True)
alt.Chart(table).transform_fold(
["Bitcoin", "S&P500", "Real Estate", "Gold"], as_=["key", "value"]
).mark_bar().encode(
x="key:N",
y=alt.Y("value:Q", scale=alt.Scale(type='symlog')),
color="key:N",
column="TERM",
)