Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Altair returning an empty chart when using log scale?

Tags:

python

altair

In JupyterLab, I'm using the altair python library to create a bar graph where the x-axis is a log scale, but it's only returning an empty chart.

Plotting a regular bar graph works as expected and different scale types also work.

I've reviewed the troubleshooting doc at https://altair-viz.github.io/user_guide/troubleshooting.html#display-troubleshooting, tried different versions of JupyterLab, and double-checked my code, but haven't been able to work it out.

Here are the versions I am using:

Python 3.7.4 (default, Aug 9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] JupyterLab 1.1.3 Altair 3.2.0 Pandas 0.25.1

Here's my code:

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

like image 283
Jason Avatar asked Sep 20 '19 16:09

Jason


2 Answers

The logarithm of zero is negative infinity, which is problematic for display. The renderer produces warnings about this, which you can see in the javascript error log when your chart is rendered:

> 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]

One way around this would be to filter non-positive values out of the chart; e.g.

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

But even then, logarithmic scales are misleading when used with bar charts, because bar charts imply a linear proportionality between the bars and the baseline is arbitrary. I'd suggest either using a linear scale, or a different type of mark to make your chart more clear.

like image 159
jakevdp Avatar answered Sep 30 '22 13:09

jakevdp


Try to use symlog (instead log).

like image 45
Pedro T Jr Avatar answered Sep 30 '22 13:09

Pedro T Jr