小编vg4*_*425的帖子

在matplotlib图中渲染Pandas'to_latex'方法的输出

Python Pandas DataFrame有一个to_latex方法:

import pandas as pd
from numpy import arange

a = pd.DataFrame(arange(4))
a.to_latex()
Run Code Online (Sandbox Code Playgroud)

输出:

'\ begin {tabular} {| l | c | c |} \n\hline \n {}&0 \\\n\hline \n0&0 \\\n1&1 \\\n2&2 \\\n3&3 \\\n\hline \n\end {tabular} \n'

我想在matplotlib图上叠加这个表:

import pylab as plt
import matplotlib as mpl

mpl.rc('text', usetex=True)
plt.figure()
ax=plt.gca()

plt.text(9,3.4,a.to_latex(),size=12)
plt.plot(y)
plt.show()
Run Code Online (Sandbox Code Playgroud)

但是,我收到此错误:

RuntimeError:LaTeX无法处理以下字符串:'\ begin {tabular} {| l | c | c |}'

我的问题是:

如何在matplotlib图中渲染Pandas'to_latex'方法的输出?

python latex matplotlib pandas

4
推荐指数
1
解决办法
1154
查看次数

Matplotlib set_major_formatter AttributeError

我正在尝试使用set_major_formatter在matplotlib图上格式化yaxis。该图已正确生成,但ax.yaxis.set_major_formatter()引发了一些奇怪的错误。

格式化程序:

def mjrFormatter(x):
    return "{0:.0f}%".format(x * 100)
Run Code Online (Sandbox Code Playgroud)

使用格式化程序的代码:

 ...
 ax.yaxis.set_major_formatter(mjrFormatter)
 ...
Run Code Online (Sandbox Code Playgroud)

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-108-b436fe657a8b> in <module>()
----> 1 plot_func(data = data, figsize=(20,10), fig_title = 'title')

<ipython-input-107-d60ffc010a75> in plot_percent_moc(data, figsize, fig_title)
     16         _ = data2[col].plot()
     17 
---> 18     ax.yaxis.set_major_formatter(mjrFormatter)
     19 
     20     fig.suptitle(fig_title, fontsize = 14)

C:\Python27\lib\site-packages\matplotlib\axis.pyc in set_major_formatter(self, formatter)
   1396         self.isDefault_majfmt = False
   1397         self.major.formatter = formatter
-> 1398         formatter.set_axis(self)
   1399 
   1400     def set_minor_formatter(self, formatter):

AttributeError: 'function' object has no attribute 'set_axis'

---------------------------------------------------------------------------
AttributeError …
Run Code Online (Sandbox Code Playgroud)

matplotlib formatter attributeerror

2
推荐指数
1
解决办法
5618
查看次数

标签 统计

matplotlib ×2

attributeerror ×1

formatter ×1

latex ×1

pandas ×1

python ×1