2

I need to put two words in a figure, using ax.text or fig.text. The problem is that the first word should be written in bold and italic, the second without any particular style. For example

one two

The two words should be written as if they are in the same sentence. I can use two calls to ax.text, but I would like to avoid to set manually the alignment between the two labels. What's the best way to do it?

1 Answer 1

3

Matplotlib allows you to use latex. It is not enabled by default, so you have to activate it first.

Example:

import matplotlib.pyplot as plt # activate usage of latex plt.rcParams['text.usetex'] = True # generate a simple graph fig, ax = plt.subplots(1,1, figsize=(6,3)) ax.text(0.5, 0.5, r"\textit{\textbf{one}} two (on the axes)") fig.text(1., 1., r"\textit{\textbf{one}} two (on the figure)", horizontalalignment='right', verticalalignment='top') plt.show() 

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

@RuggeroTurra does that answer your question?
more or less: is it possible without latex?
Except for the solution with multiple text elements that you proposed in the question, I don't think so. For that matplotlib would need to support some sort of markup text with which you could encode the formatting in the string (basically what latex does).
matplotlib has its own (limited) latex interpreter built-in. Mainly intended to support '$...$' math text stuff, I think it also supports some basic latex notations.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.