简体   繁体   中英

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

How to write greek letters in Altair? I need to use some symbols for the axis labels. I am using Jupyter notebook

You can display Greek letters in your axis by using the Greek Unicode of the symbols you want in your axis.

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.

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. 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.

Check this wonderful answer: How to find the unicode of the subscript alphabet?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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