[英]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.