简体   繁体   English

使用Altair增加条形图中的条宽

[英]Increasing bar width in bar chart using Altair

I have the following dataframe and want to use altair library in Python to plot my bar chart. 我有以下数据altair并希望在Python中使用altair库来绘制我的条形图。 However, I couldn't figure out how to expand the width of each bar (same as width=0.5 parameter in matplotlib ). 但是,我无法弄清楚如何扩展每个条的宽度(与matplotlib width=0.5参数相同)。

import pandas as pd 
from altair import * 

ls = [[   1,  734], [   2, 1705], [   3, 2309],
      [   4, 2404], [   5, 2022], [   6, 1538],
      [   7, 1095], [   8,  770], [   9,  485],
      [  10,  312], [  11,  237], [  12,  153],
      [  13,  103], [  14,   69], [  15,   47],
      [  16,   39], [  17,   43], [  18,   28],
      [  19,   18]]
df = pd.DataFrame(ls, columns=['n_authors', 'n_posters'])

Here is plotting function using altair 这是使用altair绘图功能

bar_chart = Chart(df).mark_bar().encode(
    x=X('n_authors', title='Number of Authors in a Poster'), 
    y=Y('n_posters', title='Number of Posters'),
).configure_facet_cell(
    strokeWidth=1.0,
    height=200,
    width=300
 )
bar_chart.display()

The plot looks like below from the script: 该剧情如下图所示:

Imgur

The answer of @Nipun doesn't work any more. @Nipun的答案不再适用。 After some experimenting, I could obtain the desired result with size instead of barSize . 经过一些实验,我可以用size而不是barSize获得所需的结果。 (I only tested it in Jupyter Lab.) (我只在Jupyter实验室测试过。)

from pandas import *
from altair import *

ls = [[   1,  734], [   2, 1705], [   3, 2309],
      [   4, 2404], [   5, 2022], [   6, 1538],
      [   7, 1095], [   8,  770], [   9,  485],]
df = DataFrame(ls, columns=['X', 'Y'])

Chart(df).mark_bar(size=20).encode(x='X', y='Y')

You need to use the barSize argument. 您需要使用barSize参数。 See more here for Mark Configuration . 有关标记配置,请参阅此处

Solution

bar_chart = Chart(df).mark_bar(barSize=20).encode(
    x=X('n_authors', title='Number of Authors in a Poster'), 
    y=Y('n_posters', title='Number of Posters'),
).configure_facet_cell(
    strokeWidth=1.0,
    height=200,
    width=300
 )
bar_chart.display()

Output 产量

在此输入图像描述

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

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