简体   繁体   English

Altair- 显示所有轴刻度但仅显示一些刻度标签

[英]Altair- Display All Axis Ticks But Only Some Tick Labels

I'm stumped on how to display ticks for all x-axis values but only labels for some.我很难过如何显示所有 x 轴值的刻度,但只显示一些标签。 The x-axis values in my chart are 0.0 - 3.0 in tenths, whereas I only want to display labels for the whole numbers (0, 1, 2, 3).我的图表中的 x 轴值是 0.0 - 3.0 的十分之一,而我只想显示整数(0、1、2、3)的标签。

Here's the data I'm working with:这是我正在使用的数据:

bin count
0.0 0
0.1 0
0.2 0
0.3 0
0.4 0
0.5 0
0.6 0
0.7 0
0.8 0
0.9 0
1.0 0
1.1 1
1.2 3
1.3 7
1.4 14
1.5 13
1.6 29
1.7 47
1.8 59
1.9 59
2.0 75
2.1 72
2.2 103
2.3 96
2.4 119
2.5 76
2.6 93
2.7 68
2.8 70
2.9 44
3.0 49

The only progress I could manage was setting the data type to Ordinal and increasing the label font size:我能管理的唯一进展是将数据类型设置为 Ordinal 并增加标签字体大小:

alt.Chart(df).mark_bar().encode(x=alt.X('bin:O', axis=alt.Axis(labelFontSize=18, 
                                                  values=[round(i,1) for i in np.arange(0,3.1,.1)])), 
                                y=alt.Y('count:Q'))

This is what the chart looks like: altair bar chart这是图表的样子: altair 条形图

Any suggestions?有什么建议?

You can use the tickCount axis property to specify how many ticks you would like, and the labels are automatically chosen so that they will not overlap:您可以使用tickCount轴属性来指定您想要的刻度数,标签会自动选择,以便它们不会重叠:

import altair as alt
import pandas as pd

df = pd.DataFrame({
    'bin': [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5,
            1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 3.0],
    'count': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 7, 14, 13, 29, 47,
              59, 59, 75, 72, 103, 96, 119, 76, 93, 68, 70, 44, 49]
})

alt.Chart(df).mark_bar().encode(
    x=alt.X('bin:Q', axis=alt.Axis(tickCount=df.shape[0], grid=False)),
    y=alt.Y('count:Q')
)

在此处输入图片说明

If you want to further customize the label locations and text, you can use a labelExpr .如果要进一步自定义标签位置和文本,可以使用labelExpr For example:例如:


alt.Chart(df).mark_bar().encode(
    x=alt.X('bin:Q', axis=alt.Axis(
        tickCount=df.shape[0],
        grid=False,
        labelExpr="datum.value % 1 ? null : datum.label"
    )),
    y=alt.Y('count:Q')
)

在此处输入图片说明

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

相关问题 Python - 删除轴刻度标签,保留刻度和轴标签 - Python - Remove axis tick labels, keeping ticks and axis label 显示仅具有主要刻度标签的小刻度 - Minor ticks with only major tick labels are shown Matplotlib 刻度和刻度标签 position 与轴分开锚定 - Matplotlib ticks and tick labels position anchored separately from axis Altair 交互式图表:显示所有轴标签,而 transform_filter 仅获取部分数据 - Altair interactive chart: Show all axis labels while transform_filter only takes part of the data 在使用Altair和Jupyter进行绘图时,如何在Axis标签中显示希腊字母? - How to display Greek letters in Axis labels when plotting with Altair and Jupyter? 选择在 python 的 altair 图表上显示多少 x 轴标签 - Choosing how many x axis labels display on an altair chart in python 仅在部分子图中删除了刻度和标签 - Ticks and labels removed on only SOME of the subplots Matplotlib:如何为轴刻度和轴刻度标签获取相同的“基本”和“偏移”参数 - Matplotlib: How to get same “base” and “offset” parameters for axis ticks and axis tick labels 如何跳过一些轴标签但在 matplotlib 中保持刻度? - How to skip some axis labels but keep ticks in matplotlib? 在 Altair 中更改 y 轴刻度数 - Changing Number of y-axis Ticks in Altair
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM