我有以下数据框,并想altair在Python中使用库来绘制我的条形图。但是,我不知道如何扩展每个小节的宽度(与中的width=0.5参数相同matplotlib)。
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'])
Run Code Online (Sandbox Code Playgroud)
这是使用的绘图功能 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()
Run Code Online (Sandbox Code Playgroud)
该图看起来像下面的脚本所示:

@Nipun的答案不再起作用。经过一些试验,我可以使用size代替获得所需的结果barSize。(我仅在Jupyter Lab中对其进行了测试。)
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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1536 次 |
| 最近记录: |