简体   繁体   English

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

[英]How would I create a bar chart side by side like this Altair?

Image of What I want to create我想创造的形象

I got the left hand side of the graph (Top Third), and tried creating a 'bars2' and 'text2' field but that didn't work and adding it into the original ranked_movies field but that was all messy.我得到了图表的左侧(前三分之一),并尝试创建一个 'bars2' 和 'text2' 字段,但这不起作用并将其添加到原始的 ranked_movies 字段中,但这很混乱。 Is there a way to shift and compress and maybe add a whole other set of bar charts?有没有办法移动和压缩并添加一整套其他条形图?

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?"
)

This question (about the identical chart) was previously answered here , but unfortunately the question was deleted by the user.这个问题(关于相同的图表)以前在这里回答,但不幸的是这个问题被用户删除了。

Here was my response:这是我的回应:


The Altair gallery has a few examples of faceted bar charts (eg this one ) For the chart you have in mind, you can proceed by faceting a Layer Chart which contains a bar and a text layer. Altair 画廊有几个分面条形图的示例(例如这个) 对于您想到的图表,您可以通过分一个包含条形图和文本层的图层图表来继续。 For example:例如:

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'
)

在此处输入图片说明

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

相关问题 如何在 Altair 水平条形图中将轴标签移动到另一侧 - How to move axis label to opposite side in Altair horizontal bar chart altair 并排分组条形图而不是单个图表 - altair grouped bar chart side-by-side instead of single chart 如何在 Altair 中创建分组条形图? - How to create a grouped bar chart in Altair? Altair - 如何在不操作 DataFrame 的情况下创建分组条形图 - Altair - How to create a grouped bar Chart without manipulating the DataFrame 如何在前面的条形图中保留较小的值(当我不能并排放置条形图时)? - How do I keep the smaller value in a bar chart in front (when I cannot place the bars side by side)? 在 Altair 中生成“闪避”或“并排”条形图/柱形图? - Produce “dodged” or “side-by-side” bar/column charts in Altair? 如何在 PyGame 中创建侧滚动文本? - How would I create side scrolling text in PyGame? 我需要有关如何在以下条形图中将绿色条移动到蓝色条左侧的指导 - I need guidance on how to move the green bar to the left side of the blue bar in the following bar chart 如何在 Altair 中水平排列两个多面并排图表? - How can I arrange two faceted side-by-side charts horizontally in Altair? Matplotlib:并排条形图中的自动标记 - Matplotlib: Automatic labeling in side by side bar chart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM