Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 372
Yiti Avatar asked Nov 16 '17 20:11

Yiti


People also ask

How do you get Greek letters in Jupyter notebook?

To use them you simply type \Alpha then hit tab and there you have an α character. This is pretty simple, and not only does it work in markdown cells but it also works within code cells as well. The complete Greek alphabet can be seen here, along with a little history about it.

How do you print Greek letters in python?

To print any character in the Python interpreter, use a \u to denote a unicode character and then follow with the character code. For instance, the code for β is 03B2, so to print β the command is print('\u03B2') .

Can you use Greek letters as variables in Python?

You can already use Greek letter variable names in Python: https://stackoverflow... | Hacker News.


1 Answers

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: plot with x-axis having Greek characters

like image 77
Ram Narasimhan Avatar answered Sep 29 '22 05:09

Ram Narasimhan