[英]How to sort Y-axis labels different for each row in my plot?
我希望每個子圖根據定義條形大小的值對標簽進行排序。
見示例圖片:
data = {'label': ['A','A','B','B'], 'variable': ['x', 'y', 'x', 'y'], 'value':[2,4,3,1]}
df = pd.DataFrame.from_dict(data)
selector = alt.selection_single(empty='all', fields=['label'])
bar = alt.Chart(df,title='My Plot').mark_bar().encode(
alt.Y('label', sort=alt.EncodingSortField(field="value", op="mean", order='ascending'), axis=alt.Axis(title='Label')),
alt.X('value:Q', axis=alt.Axis(format='%', title='Value')),
alt.Row('variable', title='Variable'),
color=alt.condition(selector, alt.value('orange'), alt.value('lightgray')),
tooltip=[alt.Tooltip('label', title='Label'),
alt.Tooltip('value:Q', format='.2%', title='Value'),]
).add_selection(selector)
chart = (bar).properties(width=700, height=300)
display(chart)
在示例中,標簽(A,B)現在根據這些標簽的所有值的平均值進行排序。 我希望訂單為標簽X的BA和標簽Y的AB(因此,根據Altair圖的行中顯示的標簽值降序)。
我嘗試使用facet但是沒有解決問題。 我在這里提供代碼,因為它可以激發解決方案:
import altair as alt
import pandas as pd
df = pd.DataFrame({'label': ['A','A','B','B'],
'variable': ['x', 'y', 'x', 'y'],
'value':[2,4,3,1]})
bar = alt.Chart(df,title='My Plot').mark_bar().encode(
alt.Y('label', axis=alt.Axis(title='Label'),
sort=alt.EncodingSortField(field="value", op="values", order='descending')),
alt.X('value:Q', axis=alt.Axis(format='d', title='Value')),
tooltip=[alt.Tooltip('label', title='Label'),
alt.Tooltip('value:Q', format='d', title='Value'),]
).facet(
row='variable:O'
)
bar
對於變量x和y,行順序仍為['B','A']。 我希望變量y條形圖有['A','B]。
進一步詳細闡述了jakevdp提供的解決方案,我得到了這個:
selector = alt.selection_single(empty='all', fields=['label'])
base = alt.Chart(df, title='My Plot').mark_bar().encode(
alt.Y('label', axis=alt.Axis(title='Label'), sort=alt.EncodingSortField(field="value", op="sum", order='descending')),
alt.X('value:Q', axis=alt.Axis(format='d', title='Value')),
color=alt.condition(selector, alt.value('orange'), alt.value('lightgray')),
tooltip=[alt.Tooltip('label', title='Label'),
alt.Tooltip('value:Q', format='d', title='Value'),]
).add_selection(selector)
bar = alt.vconcat(title='My Chart')
for v in df['variable'].unique():
bar &= base.transform_filter(f"datum.variable == '{v}'").properties(title=f"'{v}'")
bar
通過設計構面圖共享它們的軸,這意味着當您對列進行排序時,您將按整個數據集對兩個軸進行排序。
如果您希望每個圖表單獨對其軸進行排序,我認為唯一的方法是手動過濾數據集並連接圖表。 這是你可以這樣做的一種方式:
import altair as alt
import pandas as pd
df = pd.DataFrame({'label': ['A','A','B','B'],
'variable': ['x', 'y', 'x', 'y'],
'value':[2,4,3,1]})
base = alt.Chart(df).mark_bar().encode(
alt.Y('label', axis=alt.Axis(title='Label'),
sort=alt.EncodingSortField(field="value", op="sum", order='descending')),
alt.X('value:Q', axis=alt.Axis(format='d', title='Value')),
tooltip=[alt.Tooltip('label', title='Label'),
alt.Tooltip('value:Q', format='d', title='Value'),],
)
alt.vconcat(
base.transform_filter("datum.variable == 'x'").properties(title='x'),
base.transform_filter("datum.variable == 'y'").properties(title='y'),
title='My Chart'
)
默認情況下,Facet具有共享比例,但您可以使用resolve屬性覆蓋比例分辨率:
import altair as alt
import pandas as pd
df = pd.DataFrame({'label': ['A','A','B','B'],
'variable': ['x', 'y', 'x', 'y'],
'value':[2,4,3,1]})
alt.Chart(df,title='My Plot').mark_bar().encode(
alt.Y('label', sort=alt.EncodingSortField(field="value", op="mean", order='descending'), axis=alt.Axis(title='Label')),
alt.X('value:Q', axis=alt.Axis(format='%', title='Value'))
).facet(
alt.Row('variable', title='Variable'),
resolve={"scale": {"y": "independent"}}
)
請注意,您不能再在Altair 2中使用行編碼速記,因為Altair 2(和Vega-Lite 2)中具有行/列的單元規范沒有resolve
屬性。 我們現在為Vega-Lite 3添加了resolve
,因此我認為一旦Altair 3出局,您應該能夠執行以下操作:
df = pd.DataFrame({'label': ['A','A','B','B'],
'variable': ['x', 'y', 'x', 'y'],
'value':[2,4,3,1]})
alt.Chart(df,title='My Plot', resolve={"scale": {"y": "independent"}}).mark_bar().encode(
alt.Y('label', sort=alt.EncodingSortField(field="value", op="mean", order='descending'), axis=alt.Axis(title='Label')),
alt.X('value:Q', axis=alt.Axis(format='%', title='Value')),
alt.Row('variable', title='Variable')
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.