简体   繁体   English

如何将第二个 y 轴添加到条形图? 牵牛星

[英]How to add second y-axis to the bar chart? Altair

I have a DataFrame:我有一个 DataFrame:

  EMOJI   PERCENT_TEXT    PERCENT     combined
0  😂         23.1%        23.1       😂 23.1%
1  🥊         5.7%          5.7       🥊 5.7% 
2  👊         3.5%          3.5       👊 3.5%
3  👏         3.0%          3.0       👏 3.0%
4  💪         2.5%          2.5       💪 2.5%
5  🇮🇪          2.4%          2.4        🇮🇪  2.4%

My code is below:我的代码如下:

bars = alt.Chart(percentages_df).mark_bar(color='orange').encode(
    alt.X('PERCENT:Q',axis=None),
    alt.Y('combined:N', sort='-x', title=None)
).configure_view(strokeOpacity=0).configure_axis(
    # remove axis line
    grid=False, domain=False,ticks=False
)

在此处输入图像描述

But the chart I created is not what I expected.但是我创建的图表不是我所期望的。

I want to create a bar chart by Altair with two y-axis labels, like this:我想用 Altair 创建一个带有两个 y 轴标签的条形图,如下所示: 在此处输入图像描述

Should I use layer to generate this chart?我应该使用层来生成这个图表吗? If so, how to generate layers of two y-axis labels?如果是这样,如何生成两个 y 轴标签的层? If not, what should I use to generate it?如果没有,我应该用什么来生成它? Thanks!谢谢!

I don't know of any easy way to create two distinct sets of axis labels, but you can roughly accomplish the alignment you want by adjusting axis label properties.我不知道有什么简单的方法可以创建两组不同的轴标签,但是您可以通过调整轴 label 属性大致完成您想要的 alignment 。

I also changed the chart to create the combined label using a transform:我还更改了图表以使用转换创建组合 label:

import pandas as pd
import altair as alt

percentages_df = pd.DataFrame({
  'EMOJI': ['😂', '🥊', '👊', '👏', '💪', '🇮🇪'],
  'PERCENT': [23.1, 5.7, 3.5, 3.0, 2.5, 2.4]
})

alt.Chart(percentages_df).transform_calculate(
    combined = alt.datum.EMOJI + '    ' + alt.datum.PERCENT + '%'
).mark_bar(color='orange').encode(
    alt.X('PERCENT:Q',axis=None),
    alt.Y('combined:N', sort='-x', title=None,
          axis=alt.Axis(labelAlign='left', labelPadding=6))
).configure_view(strokeOpacity=0).configure_axis(
    # remove axis line
    grid=False, domain=False,ticks=False
)

图表

Another option is to concatenate two charts, one of which has no x-encoding:另一种选择是连接两个图表,其中一个没有 x 编码:

import pandas as pd
import altair as alt

percentages_df = pd.DataFrame({
  'EMOJI': ['😂', '🥊', '👊', '👏', '💪', '🇮🇪'],
  'PERCENT': [23.1, 5.7, 3.5, 3.0, 2.5, 2.4]
})

right = alt.Chart(percentages_df).mark_bar(color='orange').encode(
    alt.X('PERCENT:Q',axis=None),
    alt.Y('PERCENT:N', sort='-x', title=None)
)

left = alt.Chart(percentages_df).mark_text().encode(
    alt.Y('EMOJI:N', sort=alt.EncodingSortField('PERCENT', order="descending"), title=None)
)

alt.hconcat(
    left, right
).configure_view(strokeOpacity=0).configure_axis(
    # remove axis line
    grid=False, domain=False,ticks=False
)

图2

You could also try padding the string you use for labelling with whitespace via rjust .您也可以尝试通过rjust填充用于用空格标记的字符串。 For this to work, you have to use a monospace font so that all characters are of equal with:为此,您必须使用等宽字体,以便所有字符都等于:

# With your df above
df['LABEL'] = df['EMOJI'] + df['PERCENT_TEXT'].str.rjust(8)

alt.Chart(df).mark_bar(color='orange').encode(
    alt.X('PERCENT:Q',axis=None),
    alt.Y('LABEL:N', sort='-x', title=None, axis=alt.Axis(labelFont='Droid Sans Mono'))
).configure_view(strokeOpacity=0).configure_axis(
    grid=False, domain=False,ticks=False
)

在此处输入图像描述

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

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