pythongraphaltair

Inconsistent graphs with Altair despite same package version


I am working on creating graphs using Altair in different environments with different package sets. Despite ensuring the Altair version is consistent across these environments, I am observing significant differences in the graphs generated. Strangely, one set of graphs closely resembles those generated using Pandas plotting functions.

import altair as alt

charts = []
for i in frame["class"].unique():
    chart = (
        alt.Chart(frame.query(f'class == "{i}"'))
        .mark_line()
        .encode(
            x=alt.X("date").title("Date"),
            y=alt.X("value").title(f'{label_map[i]}'),
        )
        .properties(title=titles_map[i], width=600, height=200)
    )
    charts.append(chart)

chart = alt.vconcat(*charts)
chart.save(plot_filename)

Environment A (correct layout, one one of the three graphs):

enter image description here

Environment B (wrong layout, similar to Pandas graph):

enter image description here


Solution

  • I believe the only difference between the two charts is that the encoding type of the x-axis changes from temporal in the top chart to nominal/ordinal in the bottom chart. This could be due to how the data in the data frame is stored (which could be impacted by e.g. different pandas versions or similar), since altair uses the type of the column to infer the encoding type. If you want consistent performance, you can be explicit in which encoding type to use: either 'date:T' for temporal, or 'data:N'/'date:O' for nominal/ordinal.