Color Schemes for Data Visualization

I use colors a lot in preparing data visualizations.  This post mostly serves to collect together the various resources I use when choosing color schemes for particular visualizations or other tasks.

§ General Considerations

When choosing a color scheme, there are a number of different considerations it’s useful to keep in mind.  Some of the things I consider are given below; this is mostly to provide context for why I like the color schemes I like.

# Type of Color Schemes

Color schemes are generally designed around particular use cases, which are often largely determined by the nature of the data being visualized.

Categorical color schemes are used when the data is represented as several distinct things and you just need to be able to tell those things apart.  One common example is when you have a line graph with several lines representing different trends over time.  You need to be able to tell which line on the graph corresponds to a particular category, so each color should be distinct from all the others.  Another example would be a map where different areas have different characteristics, such as election maps where you want to represent the party of the candidate that won in each district.

Linear color schemes are used when you want to represent data that varies along some numeric continuum.  For example, you might want to color points on a map according to their temperature.  Cooler temperatures would get one color, warmer temperatures would get another, and the temperatures in between would get colors in between.

Diverging color schemes are similar to linear schemes, but they’re used when there’s some central numerical value for the data and you want to represent deviations from that central value.  For example, if you had a map showing an election between two candidates, you could use a diverging color scheme to show the strength of each district’s support for its winning candidate.  A district that went strongly for one candidate would get one color.  A district that went strongly for the other candidate would get a different color.  A district that was evenly split would get the central, neutral color.

Circular color schemes are used for data that either cycles around or repeats itself.  For example, wind direction can be considered circular; the wind might come from a direction anywhere from 0° to 359°, but 0° and 359° are very close to each other and should have colors that are similarly very close to each other.

Linear, diverging, and circular schemes can be either continuous or segmented.  When used continuously, every separate data point gets its own, specific color.  A value in a linear color scheme 26.5% of the way between the two endpoints will get a color 26.5% of the way between the two endpoints’ colors.  If the color scheme is segmented, the data is divided into separate groups (often called bins or buckets) and each group gets its own color along the gradient.  For example, if you used a 10-bin division of the data space, the 26.5% value would get the same color as every value between 20% and 30% (and that color would probably be something around 25% of the way between the two endpoints’ colors).

# Things to Care About in Color Scheme Design

There are several things that are important to consider when choosing a good color scheme.  Different use cases will have different criteria; characteristics that are important for a categorical map will be different from those important to an elevation model.

It should be easy to identify distinct states in the color scheme.  For a categorical scheme, every color should be distinct from all the others.  For linear schemes, at least the endpoints should be distinct from each other.  For a diverging scheme, the two endpoints and the midpoint should all be distinct from each other.

In some cases, there is semantic meaning to particular colors as they relate to the data being displayed.  You should choose colors with meaning congruent to the data or, at least, avoid colors that would be counterintuitive.  For example, you probably shouldn’t use a scheme that uses red for safe values and green for dangerous values.

You should take into account the potential limitations of people viewing your visualization.  For example, many people have red-green color blindness; a color scheme that goes only from red to green hues will likely not be very useful to such people.

You should also take into account limitations of your medium.  If your visualization will be printed on black and white printers, for instance, you will need to make sure the colors are distinct even if there’s no hue, just lightness and darkness.

Color schemes should be perceptually uniform.  The human eye does not perceive colors uniformly.  For instance, green light often appears brighter to us than blue light of the same physical brightness.  In computer terms, the RGB color #00FF00 feels much brighter then #0000FF.  Many color schemes are carefully designed so that as you move along the scheme’s color gradient, it feels like the brightness changes in proportion to the numeric difference.  If a color scheme has not been designed with human perception in mind, the same numerical difference can feel different depending on which part of the gradient they span.

Non-categorical color schemes should feel like there’s a natural order to them.  If you take two colors from anywhere on the gradient, the one that feels like it’s “more” than the other should always point in the same direction.

§ Resources

With the considerations for color choice out of the way, we can now move on to the various resources I use when choosing colors to use for a project.

# ColorCET

URL: https://colorcet.com/
Author: Peter Kovesi.

ColorCET consists of a large number of perceptually uniform gradients for a variety of different scenarios.  There are linear, diverging, and circular gradients with several different hue transitions.  There are several schemes designed for more specific purposes, like ones for geographic elevation mapping, or the set of red, green, and blue gradients intended for putting three separate data channels into a single RGB image.

Although the schemes are all designed to be perceptually uniform, only a small subset are designed around broadly-applicable utility for color blindness.

I tend to default to ColorCET because the variety of schemes means there’s usually something available that has a combination of hues particularly suited to the use case I have in mind.

# Scientific Colour Maps

URL: https://www.fabiocrameri.ch/colourmaps/
Author: Fabio Crameri

Fabio Crameri’s scientific color maps are all developed with color blindness in mind and should be accessible across a wide array of vision differences.  There are linear, diverging, and circular gradients, as well as adaptations of the color schemes to categorical use.  There are also some “double diverging” color schemes; I can’t comment too much on them, since I’ve never found a need for that sort of scheme myself.

The color schemes are all use fairly muted colors.  The intent is that these would be used for visualizations in published papers or as part of some other, larger work.  The colors are designed to blend in with surrounding text.  Crameri argues that many other color schemes draw too much attention to the visualizations that use them because their colors are more vivid than the text they’re supporting.

There are several different color schemes here.  When I use something from here, I mostly just use “batlow” or one of its variants; none of the other schemes feel distinct enough to warrant me choosing them over that default.

# cmocean

URL: https://matplotlib.org/cmocean/
Author: Kristen Thyng

The cmocean color schemes are, in many cases, based on Matplotlib colormaps, but they’ve been adjusted to be perceptually uniform.  The various colormaps are chosen and named according to common color schemes for particular oceanographic visualizations.  For instance, the “thermal” map has colors commonly used in oceanic temperature visualizations.

# Paul Tol’s Colour Schemes

URL: https://sronpersonalpages.nl/~pault/
Author: Paul Tol

Paul Tol works on visualization of geodata from satellite observations.  He’s developed a number of different color schemes for use in websites and papers.  These color schemes are perceptually uniform and color blind safe.

A number of the schemes include a separate color specifically intended to represent points with no data available.

I rather like these schemes.  There aren’t too many to choose from, which avoids some of the decision paralysis some other collections engender.  The colors are also relatively simple; the schemes feel more natural to me than, say, Fabio Crameri’s colors.  I’ve been making more use of these colors over time, especially for categorical data.

# ColorBrewer

URL: https://colorbrewer2.org/
Author: Cynthia Brewer

ColorBrewer is a classic of the genre, if you will.  It was originally developed in the very early 2000s to provide a number of color schemes intended for coloring map areas.

The color design assumes quantized data buckets.  None of the color schemes are continuous; their maximum number of distinct colors ranges from nine to twelve (and, frankly, the twelve-color schemes are pushing the boundaries of being visually distinct).  The linear color maps are perceptually uniform.  Not all of the schemes are color blind or black-and-white printing safe, but the website makes it pretty easy to select the ones that are.

I used to use this site all the time.  Now I’m more likely to use ColorCET or Paul Tol’s colors than ColorBrewer, but I do occasionally come back here, particularly for map-related visualizations.

# Viridis

URL: https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html
Authors: Bob Rudis, Noam Ross, and Simon Garnier

Viridis is both a collection of color maps and the name of the principal map in the collection.  All of the color schemes in the collection are perceptually uniform and color blind safe.

The viridis colormap is the default in Matplotlib.  (It was made the default in the Matplotlib 2.0 release).

The cvidis colormap is designed for functionality across a wide range of vision color deficiencies.

The turbo colormap is a rainbow colormap aimed at being a perceptually-uniform alternative to Jet.  It covers all the hues of a rainbow, with greater lightness in the center and lower lightness at each of the two ends.  It’s intended for uses where you don’t necessarily care about linear color ordering so much as identifying parts of the image with the same values as each other.  It’s not strictly color blind safe, but it’s better than some alternative rainbow schemes.

I have on occasion used the turbo colormap for visualizing depth maps.

# 20 Simple, Distinct Colors

URL: https://sashamaps.net/docs/resources/20-colors/
Author: Sasha Trubetskoy

This is a set of twenty colors designed to be visually distinct from each other while covering most of the useful hues of the rainbow.  There are options for limiting the selection to color blind safe palettes, though you end up with fewer usable colors.

These are only really useful for categorical data.  They’re not perceptually uniform.  The author doesn’t even use them for their original purpose, having found a better approach that used fewer colors overall.

In general, I tend to default to Paul Tol’s color schemes for categorical data before I reach for these.  I do, however, like Sasha’s color names.  When I need to pick colors that correspond to a number of specific names, I find this is a useful resource.

# Reasonable Colors

URL: https://www.reasonable.work/colors/
Author: Matthew Howell

Reasonable Colors comprises a set of colors designed principally for websites, through they’re applicable more broadly, too.  It consists of a set of twenty-four hues evenly distributed across a perceptual color space, plus gray.  Each color has six shades.  The shades are perceptually matched, so the, say, third shades of two different colors will have the same lightness (or darkness) levels.

I’ve used these colors for website design, including for choosing some of the colors for this website.  I also sometimes use these when matching named colors, though my usual resource for those is Sasha Trubetskoy’s color collection, mentioned above.

# Color Map Advice for Scientific Visualization

URL: https://www.kennethmoreland.com/color-advice/
Author: Kenneth Moreland

This page discusses a number of different colormaps, but the main reason I want to link to it is its Fast color scheme.  The Fast scheme provides a blue-white-red diverging colormap where the white in the middle covers a fairly narrow band of data.  It’s very useful for visually locating the transition bands between the two halves of the colormap.

The Fast map in particular is designed to be useful when applied to a 3D surface.  I have yet to use any colormap in that manner, but I appreciate some of the other qualities of the scheme.

# Tree Colors: Color Schemes for Tree-Structured Data

URL: https://doi.org/10.1109/TVCG.2014.2346277
Authors: Martijn Tennekes and Edwin de Jonge

This isn’t a color scheme, but a coloring algorithm designed to make coherent but visually distinctive categorical palettes for hierarchical tree-structured data.  I have used this algorithm for coloring treemaps.

Broadly speaking, the algorithm assigns different hues to different branches of the tree, and chooses the hues so adjacent branches will have distinct colors.  The color saturation increases from the root out to the leaf nodes, so it’s an indicator of the depth of a given node.


What to Know About Trans People

If there’s one thing I want everyone to understand about trans people, it’s this:

Trans people live—or strive to live—their lives as themselves.

Trans women live as women. Trans men live as men. Nonbinary people cover a broad spectrum, but they all just want to be able to be themselves in every facet of their lives.

There is no “pretending” here. There is no “lying”. Sometimes there is experimenting or trying things out to help figure out who they really are, but that’s still experimentation, not falsehood.

There’s this narrative from a lot of places, not least of which being our current federal administration, that trans people are acting out some false identity. That narrative is the exact opposite of reality.

Trans people are trying to be themselves in the face of a world that has told them, since birth, who it thinks they should be. In many cases, trans people’s understanding of themselves is very hard-won. It can take years or even decades to reconcile the internal feeling of who you are with the external pressures to be someone else.

It is a testament to the truth of trans identities that so many people have stood up to that world and said, “I am who I know myself to be, not who you say I am, and in spite of you saying it to me throughout my entire life.” It is a shadow over our society that many other trans people have been driven to suppress that identity—to lie about themselves in order to fit into the world—or to end their lives rather than have to choose between the lie of conformity and a truth that would leave them ostracized and isolated.

Trans people just want to live the truth of their own identities.


safe-cp: A safer file copy script

A while back I wrote a shell script to do a “safe” file copy on a Linux system.  I use it somewhat frequently for managing servers.  It basically copies a file in a completely atomic way.  Programs accessing the file always see either the complete original file or the complete new file; never anything else.

Because it’s a little involved, I though it would be nice to share it publicly, so here it is.

The script is in safe-cp.sh.  You copy a file with safe-cp.sh source-file destination-file.  More details are available by running safe-cp.sh --help.

The program ensures three things:

  1. If the destination file existed before running the program, a file always exists with that name.  There is never a time when the old file has been removed but the new one has not yet been created.

  2. The destination file is always complete and whole.  Either the entirety of the old version of the file is available or the entirety of the new version of the file is available.  There is never a time when the destination file is either empty or contains an incomplete copy of either the old or new data.

  3. The contents of the original file always exist in the filesystem.  After the copy, the old data exists—with the same inode—in a copy of the original file with a different name.  This ensures that if a program has an open filehandle to the file, its view of the data is unchanged.  The program won’t see the new data unless it closes and reopens the file, but it will never see inconsistent data.

You’d think the guarantees in the last point would be a given because of how Unix handles unlinking files with open filehandles, but I’ve seen NFS mounts that didn’t behave correctly in this regard.

Fundamentally, the script works by making sure a copy of the new file exists in the same filesystem as the destination, hard-linking the old file to a new filename, then renaming the copy of the new file to the destination name.  That final rename is atomic, while the hardlinking ensures the original file and inode are preserved.

You can read through the script for the precise details, of course.


Cairo and Pango in Python

Cairo is a pretty useful vector-based graphics library.  I usually use it from Python.  But Cairo’s built-in text handling can be a little limited; their official stance on the subject is if you want more functionality you should use an external library “like pangocairo that is part of the Pango text layout and rendering library.”  Here’s how to use Pango with Cairo in Python.

This guide takes the form of building a simple Python program that demonstrates a few basic aspects of Pango use.  The full program is in pango_test.py.

There used to be native Python modules for Pango, but they seem to be defunct now.  There are some current modules based on CFFI, but they’re incomplete and I found them difficult to work with.  The approach I like the best is to use the PyGObject module to access Pango, just as a GTK program would.  You’ll also need Pycairo installed, of course.

§ Module Imports

So let’s set up the imports for our program.

import math

import cairo

import gi
gi.require_version('Pango', '1.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Pango, PangoCairo

We’ll need the math module for some minor things later.  The cairo module should be obvious.

The gi module is the entry point to PyGObject.  You can check the documentation for more details, but basically you tell it which object versions you want to use, and then import specific libraries’ object definitions.  If you don’t specify the versions, PyGObject will use the latest version available and will print a warning to the console about it (since APIs are not guaranteed to be identical across different GObject release versions).

§ Cairo Surface Creation

Next, let’s set up a Cairo SVG surface to work with.  This should be routine for anyone familiar with Cairo.  We’ll make a surface and draw a white rectangle on it to serve as a background.

surface = cairo.SVGSurface('test.svg', 480, 480)
ctx = cairo.Context(surface)
ctx.set_source_rgb(1, 1, 1)
ctx.rectangle(0, 0, 480, 480)
ctx.fill()

§ PangoCairo Layout Creation

Now we get into the first Pango-specific thing.  The Pango library does text layout and rendering, and there’s a PangoCairo library that serves to translate Pango’s rendering into something Cairo can understand.  (Under the hood, it uses Cairo’s low-level glyphs API, but you don’t really need to worry about that.)

PangoCairo uses a “layout” to manage text rendering.  Roughly speaking, you establish a layout, tell it how to render text (what font, how to justify the text, how to space characters and lines, and so on) and what text to render, and then it gives you the rendered result.

So we start by creating a layout to use.  It takes the current Cairo context as a parameter.

layout = PangoCairo.create_layout(ctx)

§ Font Loading

Next, we’ll load a font.  Fonts are a little complicated in Pango.  The library uses a description of a font as a reference for locating the font on your system.  The description consists of information like the font name, its style (bold, italic, thin, etc.), and its size.  The easiest way to load a font is via Pango.font_description_from_string().  It takes as its parameter a string describing a font.  It will try to match the description given; if it can’t, it’ll fall back to something it thinks is a suitable substitute.

The description string has a syntax of its own, described in the documentation.  You can generally assume that you can just put the font name, style, and a number for the point size in the string and it should work.  I’ve found that sometimes adding the optional comma after the font name can help, as in the example below.  We’re going to use Times New Roman for the font.  But “Roman” is also a style keyword for Pango, so the string “Times New Roman 45” will be interpreted as a request for the Roman style of a font named “Times New”.  Putting in the comma, as in “Times New Roman, 45” lets Pango parse things correctly.

Linux users without the Microsoft fonts installed might want to use Liberation Serif instead of Times New Roman.

Note that the size given is relative to the base unit of the Cairo surface.  For a standard SVG surface, as we’re using, the size will be in points.  If we were using an ImageSurface, the below code would load Times New Roman at a size of 45 pixels tall.

After loading the font description, we tell the layout to use the font we’ve described.

font_description = Pango.font_description_from_string('Times New Roman, 45')
layout.set_font_description(font_description)

We’ll go over an alternate mechanism for loading fonts later.

§ Context Translation

Just to illustrate something, we’ll translate the Cairo context to change where the origin is.  This isn’t strictly necessary—you can just call Context.move_to() before outputting the text—but this demonstrates how you need to handle context translations when working with Pango.

Internally, the PangoCairo layout keeps a PangoContext—which, as it might sound, is the Pango version of a Cairo context.  If the transformation matrix of the Cairo context changes, you need to call PangoCairo.update_layout() to keep the PangoContext in sync.  You also need to call PangoCairo.update_layout() if the context’s target surface changes.

ctx.translate(16, 16)
PangoCairo.update_layout(ctx, layout)

§ Text-drawing Function

We’ll want to render a few strings in the program, and there are a few things we want to do for each string.  So we’ll make a function to render a given bit of text.  The function needs the Cairo context, the PangoCairo layout, and, of course, the text to render.

def draw_text_and_bounds(ctx, layout, text): 

The function will also return some information about the bounds of the text it rendered, but we’ll get to that shortly.

§ Set the Text to Render

Telling the layout what text it needs to render is a simple function call.

    layout.set_text(text)

§ Text Bounds

Example of Text Bounds

Pango gives you a few different measures of what the bounds of the text are.  In Cairo’s toy text API, the point of origin for rendering text is the left side of the text, on the text baseline.  In contrast, Pango’s point of origin is the upper left corner of the box defining the text’s logical extents.

The logical extents of the text are the full area the text occupies, based on the font.  The extents include vertical whitespace to encompass a full line of text, which means having room for ascenders and descenders (whether or not the text being rendered has them) and extra vertical room the font might include (some fonts add vertical whitespace above—and occasionally below—all of the characters).  The width of the logical extents more or less corresponds to Cairo’s TextExtents’ x_advance.  In the image to the right, the logical extents of each string are shown in red.  Note that the first string has two spaces at the end.

Pango also has ink extents for a given piece of text.  The ink extents are the area covered by the specific glyphs being rendered.  This more or less corresponds to the area covered by Cairo’s TextExtents’ x_bearing, y_bearing, width, and height.  In the image to the right, the ink extents are shown in blue.

The function Layout.get_extents() returns two values: the ink extents and the logical extents, in that order.  Each value is a PangoRectangle, which has x, y, width, and height members.  The coordinates are all with respect to the upper left corner of the logical extents.  That means the x and y values for the logical rectangle should always both be zero.  The y value for the ink rectangle will almost always be positive.  The x value for the ink rectangle might be zero, might be positive, and might be negative.  (In the example on the right, the second line has a slightly-negative ink x value, while the third line has a positive x value.)

The values in the rectangles returned by Layout.get_extents() are in Pango units.  You must divide them by Pango.SCALE to get the equivalent Cairo units.  (While Cairo works with floats, Pango works with integers.  In order to get a reasonable amount of precision, Pango scales up its coordinates by a factor of Pango.SCALE in order to facilitate meaningful subpixel operations.)

In Cairo, you always know where the text’s baseline is, because that’s where Cairo starts from when drawing text.  With Pango, you call Layout.get_baseline() to get the distance from the top of the logical extents to the baseline.  As with other Pango functions, the result is in Pango units and must be divided by Pango.SCALE to get Cairo units.  The baseline is shown in orange in the example on the right.

With that in mind, the following code will draw boxes around the text’s various extents, as well as put a small dot at the text’s origin (the upper left corner of the logical extents).

    ink_rect, logical_rect = layout.get_extents()
    baseline = layout.get_baseline()

    ctx.set_line_width(2)

    # Baseline in orange.  Offset to account for stroke thickness. 
    ctx.set_source_rgb(1, 0.5, 0.25)
    ctx.move_to(-8, baseline / Pango.SCALE + 1)
    ctx.line_to((logical_rect.x + logical_rect.width) / Pango.SCALE + 8,
                baseline / Pango.SCALE + 1)
    ctx.stroke()

    # logical rect in red
    ctx.set_source_rgb(0.75, 0, 0)
    ctx.rectangle(logical_rect.x / Pango.SCALE - 1,
                  logical_rect.y / Pango.SCALE - 1,
                  logical_rect.width / Pango.SCALE + 2,
                  logical_rect.height / Pango.SCALE + 2)
    ctx.stroke()

    # ink rect in blue
    ctx.set_source_rgb(0, 0, 0.75)
    ctx.rectangle(ink_rect.x / Pango.SCALE - 1,
                  ink_rect.y / Pango.SCALE - 1,
                  ink_rect.width / Pango.SCALE + 2,
                  ink_rect.height / Pango.SCALE + 2)
    ctx.stroke()

    # Baseline in orange
    ctx.set_source_rgb(1, 0.5, 0.25)
    ctx.move_to(-8, baseline / Pango.SCALE)
    ctx.line_to((logical_rect.x + logical_rect.width) / Pango.SCALE + 8,
                baseline / Pango.SCALE)
    ctx.stroke()
    
    # Origin in dark blue
    ctx.set_source_rgb(0, 0, 0.5)
    ctx.arc(0, 0, 2 * math.sqrt(2), 0, 2 * math.pi)
    ctx.fill()

§ Render the Text

The PangoCairo.show_layout() function is what takes care of outputting the rendered text to the Cairo surface, by way of the Cairo context.  As noted previously, PangoCairo uses the Cairo context’s current position as the upper left corner of the text’s logical bounds.  Since this program relies on using Context.translate() to shift the origin to the appropriate rendering location, we’ll simply draw the text at that (translated) origin.

    ctx.set_source_rgb(0, 0, 0)
    ctx.move_to(0, 0)
    PangoCairo.show_layout(ctx, layout)

§ Return the Logical Bounds

To facilitate text positioning, we’ll have the function return the logical bounds of the text having been rendered.  Keep in mind that the bounds are in Pango units, not Cairo units!

    return logical_rect

§ Actually Render Some Text

Now that the function is set up, let’s draw some text.  This will draw the text “Aa Ee Rr”, followed by two spaces, in the previously-loaded font (45 point Times New Roman).

logical_rect = draw_text_and_bounds(ctx, layout, 'Aa Ee Rr  ')

Following that, we’ll shift the origin down by the logical height of the rendered text, plus a bit of a buffer.  We’ll then render the text “Bb Gg Jj” (which has some descenders) in italic Times New Roman.

ctx.translate(0, 16 + logical_rect.height / Pango.SCALE)
PangoCairo.update_layout(ctx, layout)
font_description = Pango.font_description_from_string('Times New Roman, Italic 45')
layout.set_font_description(font_description)
logical_rect = draw_text_and_bounds(ctx, layout, 'Bb Gg Jj')

Next, we’ll shift the origin down a little farther for the next line of text.

ctx.translate(0, 16 + logical_rect.height / Pango.SCALE)
PangoCairo.update_layout(ctx, layout)

§ Alternate Font Loading Method

When you use Pango.font_description_from_string(), you’re not necessarily guaranteed to get the font you asked for.  If Pango can’t satisfy the request literally, it’ll use a fallback font instead.  (In my experience, the fallback font ignores all additional font characteristics like point size and font weight, which can give substantially less-than-ideal results.)

An alternate approach involves going through PangoFontMap.  You can load a list of all of the fonts on the system with PangoCairo.font_map_get_default(), which returns a PangoFontMap object.  From there, you can use PangoFontMap.get_family() to retrieve a font family by name (as a PangoFontFamily object) and then use PangoFontFamily.get_face() to retrieve a specific font variant within the family.  Face names will be things like “Regular”, “Bold”, “Bold Italic”, and so on.  You can call PangoFontFamily.list_faces() to return a list of all of the faces available for a family.

If you use this approach and the requested family or face is unavailable, the relevant function will return None instead of an object.  You can use this to be precise about your font loading rather than relying on Pango’s fallback mechanism.

Once you have a font face, you get its description with PangoFontFace.describe(), which returns a PangoFontDescription object.  After setting the font description size (in Pango units, so you’ll probably need to multiply by Pango.SCALE), you can set the font on a layout using Layout.set_font_description(), just as when we loaded a font description from a string earlier.

Here we’ll load 45 point italic Times New Roman using this alternate method.

font_map = PangoCairo.font_map_get_default()
font_family = font_map.get_family('Times New Roman')
font_face = font_family.get_face('Italic')
font_description = font_face.describe()
font_description.set_size(45 * Pango.SCALE)
layout.set_font_description(font_description)

Note that the final program, linked above and below, contains some extra error handling here in case the specified font family or face is not available.

Also note that the font description being loaded is still a Pango-created summary of the font.  In my experience, this approach can be a bit more precise than Pango.font_description_from_string(), but I’ve still run into problems.  I have one font family on my system where the specific font names are a little messed up and several different weights of the italic faces have the same name.  I can select the exactly-correct PangoFontFace object using the above process, but the summary it generates as a PangoFontDescription leads to the wrong weight being loaded.

Anyway, now the font’s been loaded, let’s render some more text.  Just for fun, we’ll do Hebrew, which is written right-to-left.

logical_rect = draw_text_and_bounds(ctx, layout, 'אָלֶף־בֵּית עִבְרִי')

Note that some of the diacritics here are actually a little outside the logical text bounds (unless you’ve changed to a font that handles them differently).

Note also that even though the text runs right-to-left, Pango still uses the left side of the rendered text as its origin.  This ensure consistent placement regardless of the text direction.  (And, indeed, you can mix together text with different writing directions and Pango will lay everything out in a reasonable way.)  If you were aligning several lines of right-to-left text (and didn’t want to just pass a string with newlines to Pango and let the library figure it out), you would need to use the text extents to determine how to position Pango’s logical origin to the left of the text’s right margin.

§ Conclusion

The completed program, with some comments and a little error handling, is at pango_test.py.

If you’re used to Cairo’s toy text API, Pango isn’t too dissimilar, although there is another object to keep track of, and font loading is a bit different.  The main thing I had to get used to—and it didn’t take too long—was the different origin for text rendering and the different way Pango has that it thinks about text extents.


The “Pip! is Nonbinary” FAQ

Over the past year or so, I’ve come to an understanding of myself having a nonbinary gender identity.  This is a set of frequently asked questions (with answers) I’ve fielded about that transition.

§ What does “nonbinary gender identity” mean?

It means I don’t feel I fully belong to the group of people labeled “men” and I also don’t feel I fully belong to the group of people labeled “women”.  That puts me outside of the traditional gender binary, or “nonbinary” for short.

Nonbinary is a pretty broad label.  It encompasses a diversity of gender identities, including people whose gender identity fluctuates over time (genderfluid, bigender), people who don’t feel any gender identity applies to them (agender), people who feel like a blend of gender identities (androgynous, among others, and note that not everyone has a 50%/50% blend), and many others.  For me, it’s more of a default setting, defined by the absence of a strong male or female identity.

§ Does that mean you’re transgender?

Yes.  My gender identity as I currently understand it is different from the gender identity I wore as I was growing up.  (The latter is sometimes called AGAB, short for “assigned gender at birth”.)  That makes me transgender, or trans for short.  (Note that “trans” is an adjective, not a noun.  I am trans, and I am a trans person.  It is incorrect—both grammatically and in a dehumanizing way—to say that anyone is “a trans.”)

Recall from the previous question that “nonbinary” encompasses many different gender identities.  Some nonbinary people do not see themselves as trans, though any nonbinary person may if they so choose.  I am one of those who do.

§ Is your name changing?

Yes.  My name is now Piper.  I also go by Pip.

My old names aren’t terribly antithetical to me.  I used them for many years.  They’ll always be part of my past identity.  But I feel a bit of distance now between my old name and how I see myself.  Just as I expected people to adjust when I started going by a nickname at age 15, and when I changed my name after I got married, I hope that people can now adjust to me going by Piper or Pip.

§ How should I refer to you when talking about you with someone who might not know your current name?

Use my current name, but you can add some clarification on the first use.  For example:

“I was talking with Piper the other day (who you might know as ‘old name here’, but they go by Piper now).”

§ Are your pronouns changing?

Yes and no.  The short answer is that you can use whatever pronouns you feel best fit me, but if you’re not sure what to use, “they/them” is a good default.

The longer answer is that I don’t feel a strong enough affinity to any particular gender identity to actively claim one set of pronouns to the exclusion of others.  Using a singular “they” for a specific person still feels a bit unusual to me, even though I’ve been doing it for a while now, and even though the singular “they” predates not just Shakespeare but the singular “you”.  As a result, I currently prefer to let people choose what pronouns they feel comfortable using for me.  (But, again, if you’re not sure, give “they/them” a try.)

This can result in some unavoidable confusion when two or more people are talking about me and each uses a different set of pronouns.  I’m sorry about that, but this still seems to be the best approach for me at the moment.

§ What honorific should people use for you instead of “Mister”?

This doesn’t come up very often, but please use “Mx.” (pronounced “mix”), as in “Mx. Piper” or “Mx. Gold”.

§ What if I accidentally use the wrong name or pronouns for you?

If you’re trying to adapt to this change but you use my old name out of habit, don’t stress about it.  Just briefly correct yourself—“Sorry; I meant Piper.”—and move on.  I don’t expect anyone to instantly change they way they refer to me after years of using the old way.  The new things will come with practice, and practice includes sometimes making mistakes, learning from them, and continuing on.

Also, as noted above, you can use whatever pronouns you feel best fit me, so you can’t even use the wrong ones by my definition.

§ Is your email address changing?

Yes.  The old one will continue to work (indefinitely, or as long as that’s feasible), but I’m now using .

It might be some time before I have my email address (and displayed name and account name) updated everywhere I have an account online.

§ Are you going to look different than you used to?

Yes.  I’m wearing more overtly-feminine clothes than I used to, and I’ve adopted some other forms of presentation that are more socially associated with women than with men.

This is all a process of finding which things feel comfortable for me, which feel uncomfortable, and which don’t feel relevant one way or another.

§ Why are you doing this now (as opposed to earlier in your life)?

Although I was raised as a boy and have lived for many years as a man, I’ve felt drawn to more feminine aspects of gender identity and presentation for most of my life.  But my discomfort at being confined to a male identity (also known as gender dysphoria) was never strong enough to outweigh the social pressures against coming out as a trans woman.

The biggest thing that changed was a greater awareness, on both my part and society’s, of nonbinary gender identities.  Just having nonbinary as an option felt incredibly freeing to me.  It gave me a space to explore my gender identity and presentation without feeling I had to stay in a 100% male role or transition to a 100% female identity.

(I do still have to sort myself into binary categories on occasion, like when a public facility doesn’t have gender-neutral or single-occupancy bathrooms.  I usually take an ad hoc approach and try to pick the option that I think will confuse the fewest number of people.)

§ Why are you doing this now (as opposed to never)?

As I noted above, I’ve always felt drawn to more of a female identity than I felt I was allowed to express.  Having to live with that was a persistent, low-grade pressure at the back of my mind.  I was constantly trying to decide how much I could subtly step into feminine things without explicitly claiming anything other than a male identity.

Once I started exploring a nonbinary identity, I found that I was much more comfortable and happy with the way I lived and presented myself.  And once I started feeling happier with nonbinary expressions, the times when I was restricted to male-only presentation felt more and more uncomfortable.

In short, I’m happier now, even with all of the uncertainty and opposition in some parts of society to trans people.  Staying closeted (or going back to being closeted) would be tantamount to saying, “Well, I guess I’ll just live with being unhappy for the rest of my life.”  I’m not going to do that.

§ Other questions?

I’m happy to answer or discuss most honest, respectful questions.  Feel free to reach out to me.  Friends and family should have my contact information already.  For everyone else, my email address is linked at the bottom of this page.  Be aware that it does often take me a while to get around to responding to emails, even from people I know.