gpt4 book ai didi

python - 为什么 Altair 在使用对数刻度时返回空图表?

转载 作者:行者123 更新时间:2023-12-02 01:58:31 25 4
gpt4 key购买 nike

在 JupyterLab 中,我使用 altair python 库创建一个条形图,其中 x 轴是对数刻度,但它仅返回一个空图表。

绘制常规条形图可以按预期工作,不同的比例类型也可以工作。

我已查看 https://altair-viz.github.io/user_guide/troubleshooting.html#display-troubleshooting 上的故障排除文档,尝试了不同版本的 JupyterLab,并仔细检查了我的代码,但一直无法解决。

以下是我正在使用的版本:

Python 3.7.4(默认,2019 年 8 月 9 日,18:34:13)[MSC v.1915 64 位 (AMD64)]JupyterLab 1.1.3牛郎星 3.2.0 Pandas 0.25.1

这是我的代码:

import altair as alt
import pandas as pd

df = pd.DataFrame(
[['L1', 2000],
['L2', 0],
['L3', 0],
['L4', 3000],
['L5', 101],
['L6', 100],
['L7', 99],
['L8', 250],
['L9', 770000]],
columns=['group', 'foos'])

chart = alt.Chart(df)

alt.Chart(df).mark_bar().encode(
alt.X('foos', scale=alt.Scale(type='log')),
y='group')

image of output

最佳答案

零的对数是负无穷大,这对于显示来说是有问题的。渲染器会生成有关此问题的警告,您可以在渲染图表时在 JavaScript 错误日志中看到这些警告:

> WARN A log scale is used to encode bar's x. This can be misleading as the width of the bar can be arbitrary based on the scale domain. You may want to use point mark instead.
> WARN Log scale domain includes zero: [0,770000]

解决这个问题的一种方法是从图表中过滤掉非正值;例如

alt.Chart(df).transform_filter(
alt.datum.foos > 0
).mark_bar().encode(
alt.X('foos', scale=alt.Scale(type='log')),
y='group'
)

enter image description here

但即便如此,当与条形图一起使用时,对数刻度也会产生误导,因为条形图意味着条形和基线之间的线性比例是任意的。我建议使用线性刻度或不同类型的标记以使您的图表更加清晰。

关于python - 为什么 Altair 在使用对数刻度时返回空图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58032074/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com