【问题标题】:Problem plotting data in altair, data is swapped in pyramid graph在 altair 中绘制数据时出现问题,数据在金字塔图中交换
【发布时间】:2022-06-16 15:54:59
【问题描述】:

我正在尝试绘制我的 pd 文件中的数据,该文件包含有关不同功能级别的男人和女人的数据。然而,在绘制金字塔 df 时,数据被交换。博士和助理互换,助理和博士后。但是我找不到问题或错误。

import altair as alt
from vega_datasets import data
import pandas as pd

df_natuur_vrouw = df_natuur[df_natuur['geslacht'] == 'V']
df_natuur_man = df_natuur[df_natuur['geslacht'] == 'M']

df_techniek_vrouw = df_techniek[df_techniek['geslacht'] == 'V']
df_techniek_man = df_techniek[df_techniek['geslacht'] == 'M']

slider = alt.binding_range(min=2011, max=2020, step=1)
select_year = alt.selection_single(name='year', fields=['year'],
                                   bind=slider, init={'year': 2020})

base_vrouw = alt.Chart(df_natuur_vrouw).add_selection(
    select_year
).transform_filter(
    select_year
).properties(
    width=250
)

base_man = alt.Chart(df_natuur_man).add_selection(
    select_year
).transform_filter(
    select_year
).properties(
    width=250
)

color_scale = alt.Scale(domain=['M', 'V'],
                        range=['#003865', '#ee7203'])

left = base_vrouw.encode(
    y=alt.Y('functieniveau:O', axis=None),
    x=alt.X('percentage_afgerond:Q',
            title='percentage',
            scale=alt.Scale(domain=[0, 100], reverse=True)),
    color=alt.Color('geslacht:N', scale=color_scale, legend=None)
).mark_bar().properties(title='Female')

middle = base_vrouw.encode(
    y=alt.Y('functieniveau:O', axis=None, sort=['Professor', 'Associate Professor', 'Assistant Professor', 'Postdoc', 'PhD']),
    text=alt.Text('functieniveau:O'),
).mark_text().properties(width=110)

right = base_man.encode(
    y=alt.Y('functieniveau:O', axis=None),
    x=alt.X('percentage_afgerond:Q', title='percentage', scale=alt.Scale(domain=[0, 100])),
    color=alt.Color('geslacht:N', scale=color_scale, legend=None)
).mark_bar().properties(title='Male')

alt.concat(left, middle, right, spacing=5, title='Percentage male and female employees per academic level in nature sector 2011-2020')

The data I want to show, however the values for PHD and assistant are swapped and so are the values for associate professor and postdoc

【问题讨论】:

    标签: python pandas dataframe altair


    【解决方案1】:

    如果没有能够运行代码的数据样本,这有点难以判断,但问题可能是您正在对中间图进行排序,而没有对左右图进行排序。尝试对条形图应用与文本相同的 Y 排序顺序,看看是否可行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多