Altair: Stack bar graph - issues with x axis range

Created on 20 Nov 2018  路  6Comments  路  Source: altair-viz/altair

Hello

I am using a stack bar chart and I am observing some strange behaviour. I must say that I started with Altair yesterday. I am using Jupyter notebook, Python 3.

Ok one chart looks just how it should. The years which are shown match my dataframe, and the labels on the x axis (years) looks right.
image

Ok, then I created a new dataframe, selecting years between 1980 to 2018. I display it the same way as the one above. No matter what I do, the years labels on the x axis are overshooting, and it does not look nice. Plus the labels are skipping, and suddenly they are turned the wrong way and show a comma to separate the thousands.
image

Here the csv file with the data which is in my dataframe for the second bar graph:
wells_all_explo_extract_col_1980_2018.txt

I have been fidgeting hours trying to find out what is happening, re designing the graph in a clean jupyter notebook, etc.

Could you please help me with that?

Let me know if you need more info,

Many thanks in advance,
Cheers
Anne

question

All 6 comments

Hi Anne,

Try adding the N so that Altair interprets your year column as a nominal variable:

x='wlbEntryYear:N'

More information on data types and how they interact with colors and scales can be found here

Ahhh thanks a lot, it was just that!

Ok, I am glad I mentioned that I started with Altair only yesterday...

I saw these letters (':O', ':P', etc), but I didn't find mention about it in the altair documentation.

Do you have a link where these letters are explained?

Grazie mille :-)
Cheers
Anne

image

Prego! The link to the documentation is in my initial reply.

Hi Anne,
Encoding data types are discussed in the documentation at https://altair-viz.github.io/user_guide/encoding.html#encoding-data-types

Encoding types are perhaps a bit counterintuitive if you're used to a more imperative approach to visualization. If you want to hear more about the philosophy behind it, I'd recommend the PyCon tutorial from last year: https://www.youtube.com/watch?v=ms29ZPUKxbU

Thanks Jake,

That's very helpful. Well I guess if I would use ':T' for my well entry date, I would have to have it defined as a date (or time?) in my pandas dataframe?

Aha, yes I saw this vid on the pycon 2018 youtube channel, but haven't watched it yet, so I know what I have to do, and it's you talking, that's cool :-)

Fantastic, a lot of progress done since yesterday!

Cheers from Norway
Anne

I guess if I would use ':T' for my well entry date, I would have to have it defined as a date (or time?) in my pandas dataframe?

Sort of. When you use a temporal encoding (what :T is a shorthand for) you are telling Altair to interpret your input as a date. Because Altair data is encoded in JSON, and JSON has no native date format, all dates are encoded as strings... and so Pandas datetime columns will be serialized as strings when they are passed to the Altair chart.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SuperShinyEyes picture SuperShinyEyes  路  3Comments

tonylee3399 picture tonylee3399  路  3Comments

morberg picture morberg  路  3Comments

fischcheng picture fischcheng  路  4Comments

firasm picture firasm  路  3Comments