python - 我如何像 Altair 这样并排创建条形图?

标签 python visualization altair

Image of What I want to create

我得到了图表的左侧(前三分之一),并尝试创建一个“bars2”和“text2”字段,但没有成功并将其添加到原始的 ranked_movies 字段中,但这一切都很困惑。有没有一种方法可以移动和压缩并添加一整套其他条形图?

tuples = list(zip([names[ep] for ep in episodes],topthird,middlethird))
binranking_per_df = pd.DataFrame(tuples, columns = ['Name', 'Top Third','Middle Third'])
#ranking_per_df


bars = alt.Chart(binranking_per_df).mark_bar(size=20).encode(
    x=alt.X(
        'Top Third',
        axis=None),
    y=alt.Y(
        'Name:N',
         axis=alt.Axis(tickCount=5, title=''),
         sort=names_l
    )
)

bars2 = alt.Chart(binranking_per_df).mark_bar(size=20).encode(
    x=alt.X(
        'Middle Third',
        axis=None),
    y=alt.Y(
        'Name:N',
         axis=alt.Axis(tickCount=5, title=''),
         sort=names_l
    )
)

text = bars.mark_text(
    align='left',
    baseline='middle',
    dx=3  
).encode(
    text=alt.Text('Top Third:Q',format='.0%')
)

text2 = bars.mark_text(
    align='left',
    baseline='middle',
    dx=3  
).encode(
    text=alt.Text('Middle Third:Q',format='.0%')
)

ranked_movies = (text + bars).configure_mark(
    color='#008fd5'
).configure_view(
    strokeWidth=0
).configure_scale(
    bandPaddingInner=0.2
).properties(
    width=500,
    height=180
).properties(
    title="Whats the Best 'Star Wars' Movie?"
)

最佳答案

这个问题(关于相同的图表)之前已经回答过here , 但不幸的是,该问题已被用户删除。

这是我的回复:


Altair 画廊有几个多面条形图示例(例如 this one)对于您想要的图表,您可以通过 faceting 继续一个Layer Chart其中包含一个栏和一个文本层。例如:

import altair as alt
import numpy as np
import pandas as pd

titles = ['The Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith',
          'A New Hope', 'The Empire Strikes Back', 'Return of the Jedi']
categories = ['Top third', 'Middle third', 'Bottom third']
percentages = [
    [0.16, 0.14, 0.13, 0.50, 0.64, 0.43],
    [0.37, 0.29, 0.40, 0.31, 0.22, 0.41],
    [0.46, 0.57, 0.47, 0.19, 0.14, 0.17]
]
titles, categories, percentages = map(
    np.ravel, np.broadcast_arrays(
        titles, np.array(categories)[:, None], percentages))
df = pd.DataFrame({
    'titles': titles,
    'categories': categories,
    'percentages': percentages,
})

base = alt.Chart(df).encode(
    x=alt.X('percentages:Q', axis=None),
    y=alt.Y('titles:N', title=None, sort=titles),
).properties(
    width=70
)

bars = base.mark_bar().encode(
    color=alt.Color('categories:N', legend=None)
)
text = base.mark_text(dx=15).encode(
    text=alt.Text('percentages:Q', format=".0%")
)

(bars + text).facet(
    column=alt.Column('categories:N', title=None, sort=categories)
).configure_view(
    stroke='transparent'
)

enter image description here

关于python - 我如何像 Altair 这样并排创建条形图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68073320/

相关文章:

python __init__ 参数变成一个元组

java - 是否可以使用 vaadin 插件 InvientCharts 动态更改 x 轴和 y 轴标题

altair - 具有独立比例的共享轴标签

python - 使用TensorFlow Dataset API和flat_map的并行线程

python - 模板中的Django外键关系

r - 将 x=y 线添加到 ggplot2 中的 hexplot

apache-spark - Graphx可视化

python - 删除 altair 可视化中标记之间的空间

python - Altair 散点图添加了不需要的线条

java - 在 Python 和 Java/Scala 之间插入 RSA 签名时出现问题