简体   繁体   中英

Altair: change label spacing for axis with ordinal values

I started with this reference point: https://github.com/altair-viz/altair/issues/1576 .

For which I was able to display the x-axis label like so: 在此处输入图像描述

I'm using ordinal axis formatting to display dates (in order to skip weekend dates).

I'm dynamically selecting valid dates within the dataframe's index, and yet, there are values that are not displaying on the chart.

It's not just these two missing values... certain dates just don't want to display (like "Jan 29, 2021" , for which both prior and subsequent dates display fine).

Any ideas why these dates aren't showing up? Is this a datetime parsing issue? As I'm converting from datetime there aren't any excess spaces in my strftime . If I try to display the "Jan 29, 2021" , it still doesn't display.

EDIT: Added example with the problem line of code

import altair as alt
import pandas as pd
from datetime import timedelta

# create dataframe
dates = pd.date_range('1/1/2021', periods=40, freq='B')[1:-1]
count = len(dates)
df = pd.DataFrame({'x':dates, 'y':range(count)})

# identify ordinal date selection for x-axis
x_vals = []
for _ in df['x'][1::int(len(df)/12)]:
    if _ in df['x']:
        x_label = _.strftime('%b %#d, %Y')
        x_vals.append(x_label)
    else:
        d = _ + timedelta(3)
        x_vals.append(d.strftime('%b %#d, %Y'))

# construct chart
line = alt.Chart(df).mark_line().encode(
    alt.X('yearmonthdate(x):O', axis=alt.Axis(values=x_vals)),
    alt.Y('y:Q')
)

line.save('chart.html')

Something might be lost in translation when I created this example. For example, I'm not sure why dates are rotated 90 degrees. Here's image:

在此处输入图像描述

Seems like there are different missing dates here.

EDIT2: Image of production code for reference 在此处输入图像描述 在此处输入图像描述

I don't think there is anything in the external function calls that would affect the chart display. I don't have a labelAngle=270 attribute in the code anywhere.

The dates that are showing up in your example are the only ones in x_vals that are actually contained in your dataframe. You can see this by doing:

[x for x in x_vals if pd.to_datetime(x) in df['x'].to_numpy()]
['Jan 08, 2021',
 'Jan 11, 2021',
 'Jan 21, 2021',
 'Jan 29, 2021',
 'Feb 01, 2021',
 'Feb 11, 2021',
 'Feb 19, 2021',
 'Feb 22, 2021']

This is the expected outcome for ordinals since Altair will not create labels for that do not exist in the data (which is why you can use it to skip weekend dates).

The reason you are seeing the labeled rotated is because you must have used axis=alt.Axis(values=x_vals, labelAngle=270) or similar even if you didn't include it in your example code above. If you don't, some of the labels will be omitted since they would otherwise overlap as you can see in the image below:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM