- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
全部
我正在尝试在 Matplotlib 上绘制一些数据并在标题上使用 Latex。我以前做过这些,从未遇到过这个问题。我使用以下代码来绘制:
plt.subplot(1,2,1)
plt.hist(t_single, bins, rwidth= 0.8, normed= True)
plt.plot(supp, gaussian, 'red')
plt.title(r'Single-selection Estimator ($c_y = $' + str(cy) + ', $c_d = $' + str (cd) + ', $\alpha_0 = $' + str(alpha_0) + ')', fontsize = 16)
plt.legend(['Gaussian', 'Estimator'], loc = 'best', fontsize = 14)
plt.ylabel('Frequency', fontsize = 14)
plt.xlabel('t-statistic', fontsize = 14)
但是,我收到以下错误:
ParseException Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/matplotlib/mathtext.py in parse(self, s, fonts_object, fontsize, dpi)
2515 try:
-> 2516 result = self._expression.parseString(s)
2517 except ParseBaseException as err:
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in parseString(self, instring, parseAll)
1631 # catch and re-raise exception from here, clears out pyparsing internal stack trace
-> 1632 raise exc
1633 else:
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in parseString(self, instring, parseAll)
1621 try:
-> 1622 loc, tokens = self._parse( instring, 0 )
1623 if parseAll:
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
1528 try:
-> 1529 value = self._parseNoCache(instring, loc, doActions, callPreParse)
1530 except ParseBaseException as pe:
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
1378 try:
-> 1379 loc,tokens = self.parseImpl( instring, preloc, doActions )
1380 except IndexError:
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
3716 if self.expr is not None:
-> 3717 return self.expr._parse( instring, loc, doActions, callPreParse=False )
3718 else:
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
1528 try:
-> 1529 value = self._parseNoCache(instring, loc, doActions, callPreParse)
1530 except ParseBaseException as pe:
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
1378 try:
-> 1379 loc,tokens = self.parseImpl( instring, preloc, doActions )
1380 except IndexError:
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
3394 else:
-> 3395 loc, exprtokens = e._parse( instring, loc, doActions )
3396 if exprtokens or exprtokens.haskeys():
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in _parseCache(self, instring, loc, doActions, callPreParse)
1528 try:
-> 1529 value = self._parseNoCache(instring, loc, doActions, callPreParse)
1530 except ParseBaseException as pe:
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in _parseNoCache(self, instring, loc, doActions, callPreParse)
1382 else:
-> 1383 loc,tokens = self.parseImpl( instring, preloc, doActions )
1384
~/anaconda3/lib/python3.6/site-packages/pyparsing.py in parseImpl(self, instring, loc, doActions)
3182 if loc < len(instring):
-> 3183 raise ParseException(instring, loc, self.errmsg, self)
3184 elif loc == len(instring):
ParseException: Expected end of text (at char 50), (line:1, col:51)
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
~/Google Drive/Mestrado/Tese/Code/Belloni_simulations/sim_plotter.py in <module>()
59 folder = 'alpha_' + str(alpha_0) + '/'
60 figname = 'single_vs_double_cy_' + str(cy) + '_cd_' + str(cd) + '_alpha' + str(alpha_0) + '.png'
---> 61 plt.savefig(folder + figname, bbox_inches = 'tight', dpi = 300)
62
63
~/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)
699 def savefig(*args, **kwargs):
700 fig = gcf()
--> 701 res = fig.savefig(*args, **kwargs)
702 fig.canvas.draw_idle() # need this if 'transparent=True' to reset colors
703 return res
~/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py in savefig(self, fname, **kwargs)
1832 self.set_frameon(frameon)
1833
-> 1834 self.canvas.print_figure(fname, **kwargs)
1835
1836 if frameon:
~/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
2214 orientation=orientation,
2215 dryrun=True,
-> 2216 **kwargs)
2217 renderer = self.figure._cachedRenderer
2218 bbox_inches = self.figure.get_tightbbox(renderer)
~/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
505
506 def print_png(self, filename_or_obj, *args, **kwargs):
--> 507 FigureCanvasAgg.draw(self)
508 renderer = self.get_renderer()
509 original_dpi = renderer.dpi
~/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in draw(self)
428 # if toolbar:
429 # toolbar.set_cursor(cursors.WAIT)
--> 430 self.figure.draw(self.renderer)
431 finally:
432 # if toolbar:
~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
53 renderer.start_filter()
54
---> 55 return draw(artist, renderer, *args, **kwargs)
56 finally:
57 if artist.get_agg_filter() is not None:
~/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py in draw(self, renderer)
1297
1298 mimage._draw_list_compositing_images(
-> 1299 renderer, self, artists, self.suppressComposite)
1300
1301 renderer.close_group('figure')
~/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
136 if not_composite or not has_images:
137 for a in artists:
--> 138 a.draw(renderer)
139 else:
140 # Composite any adjacent images together
~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
53 renderer.start_filter()
54
---> 55 return draw(artist, renderer, *args, **kwargs)
56 finally:
57 if artist.get_agg_filter() is not None:
~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
2435 renderer.stop_rasterizing()
2436
-> 2437 mimage._draw_list_compositing_images(renderer, self, artists)
2438
2439 renderer.close_group('axes')
~/anaconda3/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, artists, suppress_composite)
136 if not_composite or not has_images:
137 for a in artists:
--> 138 a.draw(renderer)
139 else:
140 # Composite any adjacent images together
~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
53 renderer.start_filter()
54
---> 55 return draw(artist, renderer, *args, **kwargs)
56 finally:
57 if artist.get_agg_filter() is not None:
~/anaconda3/lib/python3.6/site-packages/matplotlib/text.py in draw(self, renderer)
713
714 with _wrap_text(self) as textobj:
--> 715 bbox, info, descent = textobj._get_layout(renderer)
716 trans = textobj.get_transform()
717
~/anaconda3/lib/python3.6/site-packages/matplotlib/text.py in _get_layout(self, renderer)
315 w, h, d = renderer.get_text_width_height_descent(clean_line,
316 self._fontproperties,
--> 317 ismath=ismath)
318 else:
319 w, h, d = 0, 0, 0
~/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py in get_text_width_height_descent(self, s, prop, ismath)
229 if ismath:
230 ox, oy, width, height, descent, fonts, used_characters = \
--> 231 self.mathtext_parser.parse(s, self.dpi, prop)
232 return width, height, descent
233
~/anaconda3/lib/python3.6/site-packages/matplotlib/mathtext.py in parse(self, s, dpi, prop)
3301 self.__class__._parser = Parser()
3302
-> 3303 box = self._parser.parse(s, font_output, fontsize, dpi)
3304 font_output.set_canvas_size(box.width, box.height, box.depth)
3305 result = font_output.get_results(box)
~/anaconda3/lib/python3.6/site-packages/matplotlib/mathtext.py in parse(self, s, fonts_object, fontsize, dpi)
2520 err.line,
2521 " " * (err.column - 1) + "^",
-> 2522 six.text_type(err)]))
2523 self._state_stack = None
2524 self._em_width_cache = {}
ValueError:
Single-selection Estimator ($c_y = $1, $c_d = $1, $lpha_0 = $0.5)
^
Expected end of text (at char 50), (line:1, col:51)
我正在使用 Visual Studio Code,实际上,\alpha_0''' 上的 \a''' 部分以不同的方式突出显示,因为它将其识别为另一个命令,而不是作为将被解释为 Latex 命令的字符串的一部分。
有解决办法吗? Latex 会让我的标题变得非常整洁!
提前非常感谢!
最佳答案
当您在 matplotlib 中连接包含 Latex 的字符串时,仅仅将 'r'
放在第一个字符串的开头是不够的,它需要位于每个被连接的字符串之前(那些包含 Latex 的字符串)例如,命令 - '$c_d = $'
不应要求此)。改变
plt.title(r'Single-selection Estimator ($c_y = $' + str(cy) + ', $c_d = $' +
str (cd) + ', $\alpha_0 = $' + str(alpha_0) + ')', fontsize = 16)
至
plt.title(r'Single-selection Estimator ($c_y = $' + str(cy) + ', $c_d = $' +
str (cd) + r', $\alpha_0 = $' + str(alpha_0) + ')', fontsize = 16)
问题应该得到解决。这是因为只有第一个字符串被解释为原始字符串,您连接的所有后续字符串都不会被解释为原始字符串。
关于python - Matplotlib 上的 Latex 未被识别为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54466488/
我无法在此图中定位轴标签。我喜欢放置顶部标签,使管道与网格对齐,并放置左右标签,以便它们不接触绘图。 我试过了 ax.tick_params(axis='both', which='both'
我使用的是 python 2,下面的代码只是使用了一些示例数据,我的实际数据可能有不同的长度,并且可能不是很细。 import numpy as np import datetime i
给定坐标 [1,5,7,3,5,10,3,6,8]为 matplotlib.pyplot ,如何突出显示或着色线条的不同部分。例如,列表中的坐标 1-3 ( [1,5,7,3] ) 表示属性 a .我
我正在matplotlib中绘制以下图像。 我的问题是,图像看起来像这样,但是,我想使背景变暗,因为当我打印该图像时,灰度部分不会出现在打印物中。有人可以告诉我API进行此更改吗? 我使用简单的API
这是关于matplotlib的一个非常基本的问题,但是我不知道该怎么做: 我想绘制多个图形,并使用绘制窗口中的箭头从一个移到另一个。 目前,我只知道如何创建多个图并将其绘制在不同的窗口中,如下所示:
在 matplotlib 中绘制小块对象时,由于显示分辨率而引入了伪影。使用抗锯齿并不能解决问题。 这个问题有解决方案吗? import matplotlib.pyplot as plt impo
对于直方图,有一个简单的内置选项 histtype='step' .如何制作相同风格的条形图? 最佳答案 [阅读评论后添加答案] 将可选关键字设置为 fill=False对于条形图: import m
我正在尝试在 (6X3) 网格上创建子图。我对图例的位置有疑问。图例对所有子图都是通用的。 lgend 现在与 y 轴标签重叠 我尝试删除 constrained_layout=True 选项。但这在
我有一个带有一些线段( LineCollection )和一些点的图表。这些线和点有一些与它们相关的值,但没有绘制出来。我希望能够添加鼠标悬停工具提示或其他方法来轻松找到点和线的关联值。这对于点或线段
我想创建一个带有对齐不同曲线文本的图例的图。这是一个最小的工作示例: import matplotlib.pyplot as plt import numpy as np x=np.linspace(
可以说我正在用matplotlib绘制一条线并添加一个图例。 在图例中,其显示为------ Label。当绘制较小的图形尺寸以进行打印时,我发现该行的默认水平长度太长。 是否存在将------ La
我正在使用 matplotlib 构建一个 3D 散点图,但无法使生成的图形具有所有 3 个轴的共同原点。我怎样才能做到这一点? 我的代码(到目前为止),我还没有为轴规范实现任何定义,因为我对 Pyt
我有一个我想使用的绘图布局,其中 9 个不同的数据簇被布置在一个方形网格上。网格中的每个框都包含 3 个并排布置的箱线图。 我最初的想法是这将适合 3x3 子图布局,每个单独的子图本身被划分为 3x1
我的图形从y=-1变为y=10 我想在任意位置写一小段文字,例如x=2000,y=5: ax.annotate('MgII', xy=(2000.0, 5.0), xycoords='data')
我想使用LateX格式来构建一个表达式,其中出现一些数字,但这些数字是用LateX表达式中的变量表示的。 实际的目标是在axes.annotate()方法中使用它,但是为了讨论起见,这里是一个原理代码
我需要比较两组的二维分布。 当我使用 matplotlib.pyplot.contourf并覆盖图,每个等高线图的背景颜色填充整个图空间。有没有办法让每个等高线图的最低等高线级别透明,以便更容易看到每
在R中,有一个locator函数,类似于Matlab的ginput,您可以用鼠标单击图形并选择任何x,y坐标。此外,还有一个名为identify(x,y)的函数,如果您给它绘制了一组绘制的点x,y,然
我想用matplotlib生成矢量图。我尽力了-但输出是光栅图像。这是我使用的: import matplotlib matplotlib.use('Agg') import matplotlib.p
我正在尝试使用 matplotlib 制作具有非常小的灰点的散点图。由于点密度的原因,点需要很小。问题是 scatter() 函数的标记似乎既有线条又有填充。当标记很小时,只有线条可见,而看不到填充,
我不太明白为什么我无法在指定的限制内创建水平和垂直线。我想用这个框绑定(bind)数据。然而,双方似乎并没有遵守我的指示。为什么是这样? # CREATING A BOUNDING BOX # BOT
我是一名优秀的程序员,十分优秀!