[英]Render output of Pandas 'to_latex' method in a matplotlib plot
Python Pandas DataFrame有一個to_latex
方法:
import pandas as pd
from numpy import arange
a = pd.DataFrame(arange(4))
a.to_latex()
輸出:
'\\ 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()
但是,我收到此錯誤:
RuntimeError:LaTeX無法處理以下字符串:'\\ begin {tabular} {| l | c | c |}'
我的問題是:
如何在matplotlib圖中渲染Pandas'to_latex'方法的輸出?
問題是matplotlib不能很好地處理多行乳膠串。 修復它的一種方法是用空格替換latex字符串中的換行符。 也就是說,做這樣的事情:
ltx = a.to_latex().replace('\n', ' ')
plt.text(9, 3.4, ltx, size=12)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.