python - 重复图表
问题描述
我正在尝试制作一个情节:
import altair as alt
from vega_datasets import data
movies = data.movies.url
base = alt.Chart(movies).mark_bar().encode(
alt.Y('count()')).properties(
width=200,
height=150
)
chart = alt.vconcat()
for x_encoding in ['IMDB_Rating:Q', 'IMDB_Votes:Q']:
row = alt.hconcat()
for maxbins_encoding in [10, 50]:
row |= base.encode(alt.X(x_encoding,
type='quantitative',
bin=Bin(maxbins=maxbins_encoding)))
chart &= row
chart
这行得通。然后我尝试使用alt.repeat()
:
alt.Chart(vega_datasets.data.movies.url).mark_bar().encode(
alt.X(alt.repeat("row"), type='quantitative',
bin=Bin(maxbins=alt.repeat('column'))),
alt.Y('count()')
).properties(
width=200,
height=150
).repeat(
row=['IMDB_Rating', 'IMDB_Votes'],
column=[10, 50]
)
它给了我这个错误信息:
SchemaValidationError: Invalid specification
altair.vegalite.v3.schema.core.BinParams->maxbins, validating 'type'
{'repeat': 'column'} is not of type 'number'
所以我一定错过了什么。除了直接repeat()
在参数中使用之外,它是否与在参数中使用有关?bin=Bin()
encode()
解决方案
不幸的是,重复条目不能用于 bin 参数。在 Vega-Lite 中使用 repeat 的唯一参数是传递给编码的列名,因此您最初的循环方法可能是最好的。
如果您想利用 x 编码的重复,您可以执行以下操作:
def make_column(maxbins):
return alt.Chart(movies).mark_bar().encode(
alt.X(alt.repeat("row"), type='quantitative',
bin=alt.Bin(maxbins=maxbins)),
alt.Y('count()')
).properties(
width=200,
height=150
).repeat(
row=['IMDB_Rating', 'IMDB_Votes'],
)
make_column(10) | make_column(50)
推荐阅读
- css - 在 Blogger 上添加边距
- hibernate - 使用 jpa、hibernate 的以数据为中心的应用程序的最佳实践/常见模式
- javascript - JavaScript“不是函数”
- java - 由于授权无法访问 API
- dart - 从元素中取消事件侦听器
- sqlite - SQLite 在一个范围内选择日期,但没有星期天和一些假期
- python - 使用更多功能时,功能选择中的错误栏会增加?
- html - 在“媒体”属性中组合逻辑 OR 和逻辑 AND
- r - 从 sapply 中的 strsplit 获取最后一个元素
- apache-spark - Spark 序列化:Tungsten 和 Kryo 如何协同工作?