Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将matplotlib图形与LaTeX中的文字对齐_Python_Matplotlib_Latex - Fatal编程技术网

Python 将matplotlib图形与LaTeX中的文字对齐

Python 将matplotlib图形与LaTeX中的文字对齐,python,matplotlib,latex,Python,Matplotlib,Latex,我试图用LaTeX创建一个matplotlib图形,该图形与文本完全对齐。这是我从中获取的代码 我调整了一些参数,但概念保持不变 Python中的: import pylab from pylab import arange,pi,sin,cos,sqrt fig_width_pt = 430.00462 #246.0 # Get this from LaTeX using \showthe\columnwidth inches_per_pt = 1.0/72.27

我试图用LaTeX创建一个matplotlib图形,该图形与文本完全对齐。这是我从中获取的代码

我调整了一些参数,但概念保持不变

Python中的

import pylab
from pylab import arange,pi,sin,cos,sqrt
fig_width_pt = 430.00462 #246.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27               # Convert pt to inch
golden_mean = (sqrt(5)-1.0)/2.0         # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt  # width in inches
fig_height = fig_width*golden_mean      # height in inches
fig_size =  [fig_width,fig_height]
params = {'backend': 'ps',
          'axes.labelsize': 12, # 10
          'text.fontsize': 12, # 10
          'legend.fontsize': 12, # 10
          'xtick.labelsize': 8, # 8
          'ytick.labelsize': 8, # 8
          'text.usetex': True,
          'figure.figsize': fig_size}
pylab.rcParams.update(params)
# Generate data
x = pylab.arange(-2*pi,2*pi,0.01)
y1 = sin(x)
y2 = cos(x)
# Plot data
pylab.figure(1)
pylab.clf()
pylab.axes([0.125,0.2,0.95-0.125,0.95-0.2])
pylab.plot(x,y1,'g:',label='$\sin(x)$')
pylab.plot(x,y2,'-b',label='$\cos(x)$')
pylab.xlabel('$x$ (radians)')
pylab.ylabel('$y$')
pylab.legend()
pylab.savefig('fig1.pdf')
\begin{figure}
\begin{center}
  \includegraphics[width=\columnwidth]{fig1.pdf}
\end{center}
\end{figure}
乳胶:

import pylab
from pylab import arange,pi,sin,cos,sqrt
fig_width_pt = 430.00462 #246.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27               # Convert pt to inch
golden_mean = (sqrt(5)-1.0)/2.0         # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt  # width in inches
fig_height = fig_width*golden_mean      # height in inches
fig_size =  [fig_width,fig_height]
params = {'backend': 'ps',
          'axes.labelsize': 12, # 10
          'text.fontsize': 12, # 10
          'legend.fontsize': 12, # 10
          'xtick.labelsize': 8, # 8
          'ytick.labelsize': 8, # 8
          'text.usetex': True,
          'figure.figsize': fig_size}
pylab.rcParams.update(params)
# Generate data
x = pylab.arange(-2*pi,2*pi,0.01)
y1 = sin(x)
y2 = cos(x)
# Plot data
pylab.figure(1)
pylab.clf()
pylab.axes([0.125,0.2,0.95-0.125,0.95-0.2])
pylab.plot(x,y1,'g:',label='$\sin(x)$')
pylab.plot(x,y2,'-b',label='$\cos(x)$')
pylab.xlabel('$x$ (radians)')
pylab.ylabel('$y$')
pylab.legend()
pylab.savefig('fig1.pdf')
\begin{figure}
\begin{center}
  \includegraphics[width=\columnwidth]{fig1.pdf}
\end{center}
\end{figure}
我的LaTeX文件然后呈现如下内容:

import pylab
from pylab import arange,pi,sin,cos,sqrt
fig_width_pt = 430.00462 #246.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27               # Convert pt to inch
golden_mean = (sqrt(5)-1.0)/2.0         # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt  # width in inches
fig_height = fig_width*golden_mean      # height in inches
fig_size =  [fig_width,fig_height]
params = {'backend': 'ps',
          'axes.labelsize': 12, # 10
          'text.fontsize': 12, # 10
          'legend.fontsize': 12, # 10
          'xtick.labelsize': 8, # 8
          'ytick.labelsize': 8, # 8
          'text.usetex': True,
          'figure.figsize': fig_size}
pylab.rcParams.update(params)
# Generate data
x = pylab.arange(-2*pi,2*pi,0.01)
y1 = sin(x)
y2 = cos(x)
# Plot data
pylab.figure(1)
pylab.clf()
pylab.axes([0.125,0.2,0.95-0.125,0.95-0.2])
pylab.plot(x,y1,'g:',label='$\sin(x)$')
pylab.plot(x,y2,'-b',label='$\cos(x)$')
pylab.xlabel('$x$ (radians)')
pylab.ylabel('$y$')
pylab.legend()
pylab.savefig('fig1.pdf')
\begin{figure}
\begin{center}
  \includegraphics[width=\columnwidth]{fig1.pdf}
\end{center}
\end{figure}

现在,实际上要完美地对齐它,在我的理解中,它应该更像这样:

import pylab
from pylab import arange,pi,sin,cos,sqrt
fig_width_pt = 430.00462 #246.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27               # Convert pt to inch
golden_mean = (sqrt(5)-1.0)/2.0         # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt  # width in inches
fig_height = fig_width*golden_mean      # height in inches
fig_size =  [fig_width,fig_height]
params = {'backend': 'ps',
          'axes.labelsize': 12, # 10
          'text.fontsize': 12, # 10
          'legend.fontsize': 12, # 10
          'xtick.labelsize': 8, # 8
          'ytick.labelsize': 8, # 8
          'text.usetex': True,
          'figure.figsize': fig_size}
pylab.rcParams.update(params)
# Generate data
x = pylab.arange(-2*pi,2*pi,0.01)
y1 = sin(x)
y2 = cos(x)
# Plot data
pylab.figure(1)
pylab.clf()
pylab.axes([0.125,0.2,0.95-0.125,0.95-0.2])
pylab.plot(x,y1,'g:',label='$\sin(x)$')
pylab.plot(x,y2,'-b',label='$\cos(x)$')
pylab.xlabel('$x$ (radians)')
pylab.ylabel('$y$')
pylab.legend()
pylab.savefig('fig1.pdf')
\begin{figure}
\begin{center}
  \includegraphics[width=\columnwidth]{fig1.pdf}
\end{center}
\end{figure}
不幸的是,我没有找到如何

  • 设置绘制图形的矩形宽度

  • 获取ylabel的宽度以从总figsize中减去它

如果我调整
pylab.axes()
,我可以更改图形在pdf中的位置,但将其向左或向右移动太远,然后剪切图片的部分,因为调整是在pdf内部执行的,而不是在LaTeX中。此外,我不知道应该移动多少,因为ylabel的宽度未知


你有什么建议吗?

以下是可以获取职位信息的代码:

fig = pylab.gcf()
ax = pylab.gca()
print fig.bbox.bounds
print ax.bbox.bounds
ylabel = ax.yaxis.label
print ylabel.get_transform().transform(l.get_position())

print ylabel.get_window_extent()
还可以尝试通过以下方式创建轴:

pylab.axes([0.125,0.2,0.75,0.95-0.2])

+1不仅用于解决方案,还用于更好地对齐轴。非常感谢。