简体   繁体   English

在使用Altair和Jupyter进行绘图时,如何在Axis标签中显示希腊字母?

[英]How to display Greek letters in Axis labels when plotting with Altair and Jupyter?

How to write greek letters in Altair? 如何在Altair写希腊字母? I need to use some symbols for the axis labels. 我需要为轴标签使用一些符号。 I am using Jupyter notebook 我正在使用Jupyter笔记本

You can display Greek letters in your axis by using the Greek Unicode of the symbols you want in your axis. 您可以使用轴中所需符号的希腊语Unicode在轴上显示希腊字母。

Working example: 工作范例:

import altair as alt
from vega_datasets import data
unicode_gamma = '\u03b3'
# for the notebook only (not for JupyterLab) run this command once per session
alt.renderers.enable('notebook')

iris = data.iris()


alt.Chart(iris).mark_point().encode(
    x=alt.X('petalLength', axis=alt.Axis(title=' Alpha Beta Gamma \u03b1 \u03b2 '+ unicode_gamma)),
    y='petalWidth',
    color='species'
)

which produces: 产生: 带有希腊字符的x轴图

Altair doesn't support Latex math like matplotlib because only Unicode characters are supported in Vega. Altair不像matplotlib那样支持Latex数学,因为Vega只支持Unicode字符。

I encountered the same issue yesterday when I tried to convert my old plots generated using matplotlib to Altair, and the labels/title didn't display properly. 我昨天遇到了同样的问题,当时我试图将使用matplotlib生成的旧图转换为Altair,并且标签/标题显示不正确。 The answer above is great, just want to add that, some common superscripts/subscripts also have their unicodes that can be passed into Altair/Vega. 上面的答案很棒,只是想补充一点,一些常见的上标/下标也有可以传递给Altair / Vega的unicodes。

Check this wonderful answer: How to find the unicode of the subscript alphabet? 检查这个精彩的答案: 如何找到下标字母的unicode?

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

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