如何绘制带有标签的柱状图

huangapple go评论67阅读模式
英文:

How to draw bar charts with labels

问题

我正在尝试执行官方文档中的代码(带标签的柱状图)并使用完全相同的行:

import altair as alt
from vega_datasets import data


source = data.wheat()

base = alt.Chart(source).encode(
    x='wheat',
    y="year:O",
    text='wheat'
)
base.mark_bar() + base.mark_text(align='left', dx=2)

但是当我添加以下代码以将图表保存为PNG文件时:

base.save('/path/to/chart.png')

我得到了以下错误:

altair.utils.schemapi.SchemaValidationError: 'data': {'name': 'data-76d1ce26ea5761007c35827e1564d86c'}, 'encoding': {'text': {'field': 'wheat', 'type': 'quantitative'}, 'x': {'field': 'wheat', 'type': 'quantitative'}, 'y': {'field': 'year', 'type': 'ordinal'}} 是一个无效的值。

'mark' 是一个必需的属性。

这是一个错误吗?还是我做错了什么?

英文:

I'm trying to execute the code from official documentation (Bar Chart with Labels) and use exactly the same lines:

import altair as alt
from vega_datasets import data


source = data.wheat()

base = alt.Chart(source).encode(
    x='wheat',
    y="year:O",
    text='wheat'
)
base.mark_bar() + base.mark_text(align='left', dx=2)

but when I add

base.save('/path/to/chart.png')

to save chart as PNG file I get

> altair.utils.schemapi.SchemaValidationError: '{'data': {'name':
> 'data-76d1ce26ea5761007c35827e1564d86c'}, 'encoding': {'text':
> {'field': 'wheat', 'type': 'quantitative'}, 'x': {'field': 'wheat',
> 'type': 'quantitative'}, 'y': {'field': 'year', 'type': 'ordinal'}}}'
> is an invalid value.
>
> 'mark' is a required property

Is it a bug or I'm doing something wrong?

答案1

得分: 0

这段代码对我有效:

source = data.wheat()
base = alt.Chart(source).mark_bar().encode(x='wheat', y='year:O', text='wheat')
base += alt.Chart(source).mark_text(align='left', dx=2).encode(x='wheat', y='year:O', text='wheat')
base.save('base.png')

我可以使用示例中的代码绘制图表,但当我尝试使用base.save('name.png')保存时,我收到相同的错误。

英文:

This works for me:

source = data.wheat()
base = alt.Chart(source).mark_bar().encode(x='wheat',y="year:O",text='wheat')
base+= alt.Chart(source).mark_text(align='left', dx=2).encode(x='wheat',y="year:O",text='wheat')
base.save('base.png')

I can plot the graph with the code of the example, but I received the same error when I tried to save with base.save('name.png')

答案2

得分: 0

这与保存图表无关,如果您尝试单独显示 base,您将看到相同的问题。Altair 图表需要具有标记,此检查在显示图表或保存到文件时执行。在您的示例中,当您叠加柱形和文本标记时,这些标记被添加,而这个带有标记的叠加图表是所显示的内容。如果您想保存这个带有标记的叠加图表(而不是基本图表),您可以这样做,没有问题:

chart = base.mark_bar() + base.mark_text(align='left', dx=2)
chart.save('chart.png')
英文:

This is not related to saving the chart, you would see the same issue if you tried to display base by itself. Altair charts need to have marks and this check is performed when the chart is rendered for display or saved to a file. In your example, the marks are added when you layer the bar and text mark and this layered chart (with marks) is what is displayed. If you want to save this layered chart (rather than the base chart) you could do it without issues:

chart = base.mark_bar() + base.mark_text(align='left', dx=2)
chart.save('chart.png')

huangapple
  • 本文由 发表于 2023年7月20日 14:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727384.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定