2017-09-03 1 views
2

인서트가있는 플롯이 있습니다. 후자에는 그의 텍스트에서 matplotlib scatter markers를 사용하고자하는 전설이 있습니다. 나는 또한 모든 레이블과 전설에 LaTeX을 동시에 사용할 수 있기를 원합니다. 내가 이것을 언급하는 이유는 this thread을보고 그 솔루션이 저에게 맞지 않기 때문입니다. 나는 왜 우는 소리를 설명 할 것이다. 내가 LaTeX의에 대한 here의 조언 가서라텍스와 함께 범례 텍스트에 matplotlib 분산 마커 사용

으로 시도입니다 경우 여기

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

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'Insert blue square marker here') 
ax2.scatter(y, np.log(y)+1, color = 'brown', marker = "4", s=15, label = r'Insert cyan star marker here') 
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() 

(아무것도 생각하지 못했습니다, 내가하려하고있는 기능에 대한 죄송) 예제 코드

ax2.scatter(x, np.tan(x), color = 'gray', marker = "v", s=15, label = r'$\square$') 

플롯에 삽입물에 범례가 표시되지 않습니다.

대신에, 나는

ax2.scatter(x, np.tan(x), color = 'gray', marker = "v", s=15, label = '●') 

가 나는 것처럼 라텍스를 사용하고 있다는 사실에서 오는 것 같다 다음과 같은 오류

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__ 
    return self.func(*args) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 280, in resize 
    self.show() 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 351, in draw 
    FigureCanvasAgg.draw(self) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 464, in draw 
    self.figure.draw(self.renderer) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py", line 1144, in draw 
    renderer, self, dsu, self.suppressComposite) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images 
    a.draw(renderer) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/axes/_base.py", line 2426, in draw 
    mimage._draw_list_compositing_images(renderer, self, dsu) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 139, in _draw_list_compositing_images 
    a.draw(renderer) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 63, in draw_wrapper 
    draw(artist, renderer, *args, **kwargs) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/legend.py", line 471, in draw 
    bbox = self._legend_box.get_window_extent(renderer) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 269, in get_window_extent 
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 391, in get_extent_offsets 
    for c in self.get_visible_children()] 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 391, in <listcomp> 
    for c in self.get_visible_children()] 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 262, in get_extent 
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 462, in get_extent_offsets 
    for c in self.get_visible_children()] 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 462, in <listcomp> 
    for c in self.get_visible_children()] 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 262, in get_extent 
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 391, in get_extent_offsets 
    for c in self.get_visible_children()] 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 391, in <listcomp> 
    for c in self.get_visible_children()] 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 262, in get_extent 
    w, h, xd, yd, offsets = self.get_extent_offsets(renderer) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 462, in get_extent_offsets 
    for c in self.get_visible_children()] 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 462, in <listcomp> 
    for c in self.get_visible_children()] 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/offsetbox.py", line 835, in get_extent 
    bbox, info, d = self._text._get_layout(renderer) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/text.py", line 362, in _get_layout 
    ismath=ismath) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 230, in get_text_width_height_descent 
    renderer=self) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/texmanager.py", line 676, in get_text_width_height_descent 
    dvifile = self.make_dvi(tex, fontsize) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/texmanager.py", line 399, in make_dvi 
    texfile = self.make_tex(tex, fontsize) 
    File "/usr/local/lib/python3.5/dist-packages/matplotlib/texmanager.py", line 314, in make_tex 
    fh.write(s.encode('ascii')) 
UnicodeEncodeError: 'ascii' codec can't encode character '\u25cf' in position 226: ordinal not in range(128) 

을 얻을 유니 코드 솔루션을 구현하기로 결정하는 경우 I 덧글 사용

# from matplotlib import rc 
# rc('text', usetex=True) 

그렇다면 LaTeX가 없어도 작동합니다. (글쎄, 내가 얻은 원은 검정색이다. 아직 색을 바꿀 수 있는지 여부를 조사하지 않았다). 나는 LaTeX를 원한다. 그러나 이것은 나에게 좋지 않다.

답변

3

The Great, Big List of LaTeX Symbols에 따르면, \square은 AMS 기호입니다. 위해 그것을 사용에 따라서, 당신은 함께 할 수있는 프리앰블에 \usepackage{amssymb}을 넣어야 할

rc('text', usetex=True) 
rc('text.latex', preamble=r'\usepackage{amssymb}') 
+0

난 여전히 다소 긴 내용의 오류를 얻을, 그래서 당신이해야합니까 질문 – ThunderBiggi

+0

에 게시 할 예정입니다 'amssymb'가 LaTeX 배포판에 설치되어 있습니까? '\ square'를 사용하여 LaTeX 문서를 성공적으로 컴파일 할 수 있습니까? –

+0

예, 가지고 있습니다 (필자는 물리학 논문을 쓰고 수년간 사용해 왔습니다). 방금 시도하고 성공적으로 LaTeX 문서를'\ square'로 컴파일했습니다. – ThunderBiggi