简体   繁体   English

Altair:使用序数值更改轴的 label 间距

[英]Altair: change label spacing for axis with ordinal values

I started with this reference point: https://github.com/altair-viz/altair/issues/1576 .我从这个参考点开始: https://github.com/altair-viz/altair/issues/1576

For which I was able to display the x-axis label like so:为此,我能够像这样显示 x 轴 label: 在此处输入图像描述

I'm using ordinal axis formatting to display dates (in order to skip weekend dates).我正在使用ordinal轴格式来显示日期(为了跳过周末日期)。

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).不仅仅是这两个缺失值......某些日期只是不想显示(例如"Jan 29, 2021" ,之前和之后的日期都可以正常显示)。

Any ideas why these dates aren't showing up?任何想法为什么这些日期没有出现? Is this a datetime parsing issue?这是datetime解析问题吗? As I'm converting from datetime there aren't any excess spaces in my strftime .当我从datetime转换时,我的strftime中没有任何多余的空格。 If I try to display the "Jan 29, 2021" , it still doesn't display.如果我尝试显示"Jan 29, 2021" ,它仍然不显示。

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.例如,我不确定为什么日期会旋转 90 度。 Here's image:这是图像:

在此处输入图像描述

Seems like there are different missing dates here.似乎这里有不同的缺失日期。

EDIT2: Image of production code for reference EDIT2:生产代码的图像供参考在此处输入图像描述 在此处输入图像描述

I don't think there is anything in the external function calls that would affect the chart display.我认为外部 function 调用中没有任何会影响图表显示的内容。 I don't have a labelAngle=270 attribute in the code anywhere.我在任何地方的代码中都没有labelAngle=270属性。

The dates that are showing up in your example are the only ones in x_vals that are actually contained in your dataframe.您的示例中显示的日期是x_vals中唯一实际包含在 Z6A8064B5DF4794555500553C47C55057DZ 中的日期。 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).这是序数的预期结果,因为 Altair 不会为数据中不存在的标签创建标签(这就是您可以使用它来跳过周末日期的原因)。

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.您看到标签旋转的原因是因为您必须使用axis=alt.Axis(values=x_vals, labelAngle=270)或类似的东西,即使您没有在上面的示例代码中包含它。 If you don't, some of the labels will be omitted since they would otherwise overlap as you can see in the image below:如果你不这样做,一些标签将被省略,因为它们会重叠,如下图所示:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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