轴没有标签,只生成一个绘图,而不是使用matplotlib和LateX生成多个绘图

2024-04-24 01:17:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试一个python代码,它使用matplotlib和LateX从数据库结果生成一些绘图。问题是,我只得到一个情节,它是没有标签。下面是错误和生成错误的代码行(如果需要,我可以发布整个方法)?你知道吗

if fileName is not None:
    if verbose:
        print("Saving file")
    from matplotlib.backends.backend_pdf import PdfPages
    with PdfPages(fileName) as pdf:
        for fig in figures:
            for ax in fig.axes:
                __add_legend(ax, outside=legend_outside)
            pdf.savefig(fig)
return figures

添加图例如下所示

   def __add_legend(ax, handles=None, labels=None, alpha=0.5,
             outside=None, loc='best', *args, **kwargs):
         if not handles:
             handles, labels = ax.get_legend_handles_labels()
         if not handles:
             return
         if outside is not None and len(handles) >= outside:
    # Shrink current axis by 20%
              box = ax.get_position()
              ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
              ax.legend(handles, labels, loc='center left', fancybox=False,
              frameon=False, shadow=False,
              bbox_to_anchor=(1, 0.5)).draggable(True)
         else:
              ax.legend(handles, labels, loc=loc, fancybox=True,
              shadow=True).draggable(True)

错误消息

  Traceback (most recent call last):
  File "../plot_prog.py", line 5, in <module>
  run_program()
  File "/home/hammouc/Documents/mimclib-last/mimclib/plot.py", line 1364, in run_program
else filteritr_convergent)
File "/home/hammouc/Documents/mimclib-last/mimclib/plot.py", line 1285, in genPDFBooklet
pdf.savefig(fig)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends /backend_pdf.py", line 2473, in savefig
 figure.savefig(self, format='pdf', **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1563, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backend_bases.py", line 2232, in print_figure
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 2536, in print_pdf
self.figure.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 62, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py", line 1159, in draw
func(*args)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 62, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 2319, in draw
a.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 62, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/axis.py", line 1113, in draw
tick.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 62, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/axis.py", line 254, in draw
self.label1.draw(renderer)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/artist.py", line 62, in draw_wrapper
draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/text.py", line 792, in draw
mtext=mtext)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 1912, in draw_tex
self._setup_textpos(curx, cury, angle, oldx, oldy)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 1770, in _setup_textpos
self.file.output(x - oldx, y - oldy, Op.textpos)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 589, in output
self.write(fill([pdfRepr(x) for x in data]))
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_pdf.py", line 149, in pdfRepr
raise ValueError("Can only output finite numbers in PDF")
ValueError: Can only output finite numbers in PDF

我刚才在python中使用了pdb命令pdf.savefigs()我得到了这个

   > /home/hammouc/Documents/mimclib-last/mimclib   /plot.py(1287)genPDFBooklet()
   -> pdf.savefig(fig)
   (Pdb) 
  Traceback (most recent call last):
  File "../plot_prog.py", line 5, in <module>
   run_program()
   File "/home/hammouc/Documents/mimclib-last/mimclib/plot.py", line 1367, in run_program
   else filteritr_convergent)
    File "/home/hammouc/Documents/mimclib-last/mimclib/plot.py", line 1287, in genPDFBooklet
   pdf.savefig(fig)
   File "/home/hammouc/Documents/mimclib-last/mimclib/plot.py", line 1287, in genPDFBooklet
  pdf.savefig(fig)
  File "/usr/lib/python2.7/bdb.py", line 48, in trace_dispatch
  return self.dispatch_line(frame)
  File "/usr/lib/python2.7/bdb.py", line 67, in dispatch_line
  if self.quitting: raise BdbQuit bdb.BdbQuit

Tags: inpypdfartistmatplotliblibpackagesusr