Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Visual Style Using Themes

To see the xkcd webcomic and to explore further about concepts covered in this lesson, you can check out the following resources:

00:00 In this lesson, you’ll learn about how you can use themes to change the look and feel of the plots that you’re creating with plotnine. There’s a couple of themes built-in, and you’re going to see two of them in action right now.

00:12 If you head back over to your Jupyter Notebook, you can do these imports. I already wrote them out for you here. It’s going to be the mpg dataset again, and you’re just using ggplot(), aes() (aesthetics), and geom_bar() to create a plot.

00:24 So, you’re doing a bar plot on the class of the cars again. And just to show you, this is how the default theme looks like that plotnine uses. And now you can change these themes by just adding another layer in here.

00:38 So, similar to how you added the layers before, you can say from plotnine import theme_dark, for example. And then you can add the dark theme down here, theme_dark(), and it’ll change the look of your plot.

00:56 There’s a couple of themes that are available like this. You can choose the ones that fit for what you want to display. A fun one I wanted to show you here is theme_xkcd. If you know, xkcd is writing web comics and has a specific style of drawing graphs, and you can simulate that in here as well.

01:14 So if you apply the xkcd theme, then you get a plot that looks like this, which, if you compare it to some of the web comics, this is kind of like the style that this person uses to create graphs.

01:25 I’ll have a link to this website also in the description if you want to get some laughs out of reading over a couple of these pretty good web comics.

01:33 And this is how you can use themes to change the look and feel of your plots. In the next lesson, you’re going to learn how you can export some of these plots that you generated and save them as files.

Dick de Goede on Nov. 24, 2023

Hello, when I import theme_xkcd from plotnine and try to render a graph with

+ theme_xkcd()

I get a list of messages: findfont: Font family 'xkcd' not found.

However I do see the graph with the Humor Sans font. Is there any reason I get this message and is there a way to fix this? I am working within a conda env and further everything works fine.

Thank you,

Dick de Goede

Dick de Goede on Nov. 24, 2023

I just was able to fix it like this:

  • download the xkcd.ttf font and place it in /usr/share/fonts/truetype
  • update font-cache with: sudo fc-cache -fv
  • clear the matplotlib font cache: rm -fr ~/.cache/matplotlib
  • restart the jupyter notebook

Curious if this is indeed the intended solution or that something else is going on.

Become a Member to join the conversation.