{
"data": {"values": [{"a": 1.01,"b": 1.01,"m_teps": 859}]},
"encoding": {
"column": {"axis": {"format": "d"},"field": "b","type": "O"},
"row": {"axis": {"format": "d"},"field": "a","type": "O"},
"text": {"field": "m_teps","type": "Q", "format": "d"}
},
"mark": "text"
}

Any, you know, gross temporary hack you could suggest that we could make in our vl sources? 'Cause we're about to submit in a few days a paper with the following plots.

I think the only option right now is rounding the numbers before you use them in altair. That should do the trick for your paper.
As far as I can see there is no way to format the axis labels in a facet. Correct me if I'm wrong @kanitw @arvind.
Good call, thanks @domoritz. This appears to be working fairly well (for posterity):
def roundSig(column, significant_figures=1):
# http://stackoverflow.com/questions/3410976/how-to-round-a-number-to-significant-figures-in-python
def roundSigFn(x, sig):
return round(x, sig - int(math.floor(math.log10(x))) - 1)
def fn(df):
df[column] = df[column].apply(
lambda x: roundSigFn(x, significant_figures))
return df
return fn

Seems to be a vega issue. Let's see what happens to this https://github.com/vega/vega/issues/745 and then we will reopen if issue persists
Okay, the fact that axis labels cannot be formatted is by design (https://github.com/vega/vega/issues/745#issuecomment-283156390). Two ways forward are: format numbers before we group them, or file an issue with Vega to support. The problem with the first approach is that it doesn't work well if the same field is used for different guides. However, I'm okay with not supporting that.
I think users can use calculate and Vega expression to reformat it?
(We could throw warning that format doesn't work for ordinal scales though.)
That might not work well if you use the same fields in different guides.
That might not work well if you use the same fields in different guides.
Yeah, but they can derive field a1 from a and use a1 only then they want to format?
Yes.
On Tue, Feb 28, 2017 at 2:43 PM Kanit Wongsuphasawat <
[email protected]> wrote:
That might not work well if you use the same fields in different guides.
Yeah, but they can derive field a1 from a and use a1 only then they want
to format?—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/vega/vega-lite/issues/1763#issuecomment-283185418,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAj86i8TmhdQbeFZymtGZF-713gvOrUxks5rhKMMgaJpZM4LFBJc
.
Can y'all add an example of this (Vega expression to reformat) (and @jakevdp, perhaps an example of how Altair would target the necessary Vega expression)?
Not pretty but here is one (note that this is vl 2)
{
"data": {"values": [{"a": 1.01,"b": 1.01,"m_teps": 859}]},
"transform": {
"calculate": [
{
"as": "fa",
"expr": "format(datum.a, ',.1f')"
},
{
"as": "fb",
"expr": "format(datum.b, ',.1f')"
}
]
},
"encoding": {
"column": {"field": "fb","type": "O"},
"row": {"field": "fa","type": "O"},
"text": {"field": "m_teps","type": "Q", "format": "d"}
},
"mark": "text"
}
@NeelMohapatra will add a warning to vega-lite when you try to format an ordinal guide label and link to an example like the one above.
Here's the equivalent Altair command to what @domoritz wrote, I think:
from altair import Chart, expr, Axis, Column, Row
data = pd.DataFrame({"a": [1.01],"b": [1.01],"m_teps": [859]})
df = expr.DataFrame(data)
df['fa'] = expr.format(df['a'], ',.1f')
df['fb'] = expr.format(df['b'], ',.1f')
Chart(df).mark_text().encode(
Row('fa:O'),
Column('fb:O'),
text='m_teps'
)
As of #2254, we can format the row if we use type = quantitative
{
"data": {"values": [{"a": 1.01,"b": 1.01,"m_teps": 859}]},
"encoding": {
"column": {"header": {"format": "d"}, "field": "b","type": "quantitative"},
"row": {"header": {"format": "d"} ,"field": "a","type": "quantitative"},
"text": {"field": "m_teps","type": "Q", "format": "d"}
},
"mark": "text"
}
The question is whether we need to support for ordinal / nominal. If so, how do we know whether the format is a number or time format in such cases.
@NeelMohapatra We can actually allow formatting for nominal and ordinal fields.
[x] Add formatType: 'number' | 'time' | 'utc' to Axis, Legend, and TextFieldDef
[x] For text, tooltips and row/column headers, check if format is explicitly specified in formatSignalRef in compile/common.ts. If so, apply the right format function (based on formatType) to the output signal. If formatType is not specified, use 'number'.
[x] For axis and legend, in compile/axis/encode.ts and compile/legend/encode.ts, check if fieldDef.type is ordinal / nominal and if format is explicitly specified. If so, apply the right format to labels's text.
Note: see links for specific lines.
What's the status of this issue @NeelMohapatra?
I'm working on it. Will probably be done with the PR tonight
row and column no longer use axes. Thus we might not need this anymore, at least for this specific case.
{
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"description": "A simple bar chart with embedded data.",
"data": {
"values": [
{"a": 12.4,"b": 28}, {"a": 32.2,"b": 55}
]
},
"mark": "bar",
"encoding": {
"column": {"field": "a", "type": "quantitative", "header": {
"format": ".2f"
}},
"y": {"field": "b", "type": "quantitative"}
}
}
Works but I think we should be able to do this correctly even if the type is ordinal, no?
I also don't think formatType works for headers.
Most helpful comment
I'm working on it. Will probably be done with the PR tonight