我想使用LaTeX(不需要unicode,因为我还需要LaTeX用于其他目的),以便在图例文本中包含彩色的amssymb符号(与主图中的标记相同)到我的阴谋的插图。我厌倦了两种不同的方式,我将在下面讨论。
首先,我使用了包xcolor
- 这是一个示例代码
# -*- 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()
然而,正方形和星形在我的情节上显示为黑色,因此没有颜色变化。
然后我尝试使用tikz,添加到序言
r'\usepackage{tikz}'
并将图例中的一个标签更改为插图(使用圆圈以减少杂乱)
ax2.scatter(x, np.tan(x), color = 'gray', marker = "v", s=15, label = r'$\tikz\draw[red,fill=red] (0,0) circle (.5ex);$')
导致以下错误
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
并且没有插图的图例(其他一切都在那里)。
我在LaTeX文档中尝试了两种方法,我可以成功编译符号。