Changing the colour of amssymb symbols inside a matplotlib legend

616 views Asked by At

I would like to use LaTeX (don't want unicode, as I need LaTeX for other purposes as well) in order to include coloured amssymb symbols (the same as the markers in my main plot) in the text of the legend to the inset of my plot. I tired two different ways that I will discuss bellow.

First, I went with the package xcolor - here is an example code

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid.inset_locator import (inset_axes, InsetPosition, mark_inset)
from matplotlib import rc

matplotlib.rcParams['text.usetex'] = True
matplotlib.rcParams['text.latex.preamble'] = [
    r'\usepackage{amssymb}',
    r'\usepackage{amsmath}',
    r'\usepackage{xcolor}']
matplotlib.rcParams['font.family'] = 'serif'
matplotlib.rcParams['font.serif'] = 'Computer Modern'

N = 50
x = np.random.rand(N)
y = np.random.rand(N)

fig, ax1 = plt.subplots()

ax1.scatter(x, np.cos(x), color = 'blue', marker = "D", s=47.5, label = r'$\cos$')
ax1.scatter(y, np.sin(y), color = 'cyan', marker = "*", alpha = 0.85, label = r'$\sin$')
ax1box = ax1.get_position()
x_value = -0.13
y_value = 0.675
legend=ax1.legend(loc = (ax1box.x0 + x_value, ax1box.y0 + y_value), handletextpad=0.1, prop={'size':8})
frame = legend.get_frame()
frame.set_alpha(0)
frame.set_linewidth(0)

ax2 = plt.axes([0.5,0.5,0.5,0.5])
ip = InsetPosition(ax1, [0.55,0,0.45,0.40])
ax2.set_axes_locator(ip)
ax2.scatter(x, np.tan(x), color = 'gray', marker = "v", s=15, label = r'$\textcolor{blue}{\blacksquare}$')
ax2.scatter(y, np.log(y)+1, color = 'brown', marker = "4", s=15, label = r'$\textcolor{cyan}{\star}$')
ax2.xaxis.tick_top()
ax2.xaxis.set_label_position('top')
ax2.yaxis.tick_right()
legend2=ax2.legend(loc = 'lower right', handletextpad=0.1, prop={'size':7})
frame2 = legend2.get_frame()
frame2.set_alpha(0)
frame2.set_linewidth(0)

plt.show()

However, the square and the star appear black on my plot, so no colour change.

Then I tried with tikz, by adding to the preamble

r'\usepackage{tikz}'

and changing one of the labels in the legend to the inset to (using circle for less clutter)

ax2.scatter(x, np.tan(x), color = 'gray', marker = "v", s=15, label = r'$\tikz\draw[red,fill=red] (0,0) circle (.5ex);$')

which leads to the following error

dvipng warning: PostScript environment contains DVI commands dvipng warning: PostScript environment contains DVI commands dvipng warning: PostScript environment contains DVI commands dvipng warning: PostScript environment contains DVI commands

and a plot without a legend to the inset (everything else is there).

I have tried both methods in a LaTeX document and I can compile the symbols successfully.

0

There are 0 answers