繁体   English   中英

如何在 Python 中为 Altair 应用每个 y 轴 label 的边框

[英]How to apply borders around each y axis label for Altair in Python

我有一个我在 Altair 中制作的可视化,我想在 y 轴上的每个 label 周围放置边框(对不起,如果我解释不正确)将它们分开。 这是我到目前为止的代码:

alt.Chart(q4df).transform_fold(
  rosspaints,
  as_=['column', 'value']
).mark_circle().encode(
  x = alt.X('column:N', axis=None),
  y = alt.Y('TITLE', title=None),
  size = alt.Size('value:Q', legend=None),
  color=alt.Color('column:N', legend=None,
                   scale=alt.Scale(
            domain=['alizarin crimson','bright red','burnt umber','cadmium yellow','dark sienna', 
              'indian yellow','indian red','liquid black','liquid clear','black gesso',
              'midnight black','phthalo blue','phthalo green','prussian blue','sap green',
              'titanium white','van dyke brown','yellow ochre'],
            range=['#94261f','#c06341','#614f4b','#f8ed57','#5c2f08','#e6ba25','#cd5c5c',
                '#000000','#ffffff','#000000','#36373c','#2a64ad','#215c2c','#325fa3',
                '#364e00','#f9f7eb','#2d1a0c','#b28426']))

  
    
).properties(
    width=400,
    height=700
    
).configure_axis(grid=False, labelFontWeight= 'bold', labelColor='black')

这是我目前的 output:

在此处输入图像描述

这是我想要的 output:

在此处输入图像描述

您可以像这样为每个 y 轴刻度设置网格线:

import altair as alt
from vega_datasets import data


source = data.barley()

alt.Chart(source).mark_point().encode(
    alt.X('yield:Q', axis=alt.Axis(grid=False)),
    alt.Y('variety:N', axis=alt.Axis(grid=True)),
    color='year:N'
).configure_view(
    stroke=None
)

在此处输入图像描述

或者根据您在 y 轴上编码的相同变量使用 facet,同时解析 y 比例,以便在每个 plot 中仅显示带有数据点的 y 轴条目:

alt.Chart(source).mark_point().encode(
    alt.X('yield:Q', axis=alt.Axis(grid=False)),
    alt.Y('variety:N', title=''),
    color='year:N',
).facet(
    row=alt.Facet('variety:N', title='', header=alt.Header(labels=False))
).resolve_scale(
    y='independent'
)

在此处输入图像描述

暂无
暂无

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

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