抱歉问了一个愚蠢的问题。我只是想用一下,但什么都不知道。
我的问题是:我想用 python 代码绘制图形。
我在终端 (我使用 mac) 中通过调用以下命令来运行它:
pdflatex -shell-escape plot_air_drag.tex
!我找不到文件“plot_air_drag.tex”。
<*> 绘图_空气_拖动.tex
(按 Enter 重试,或按 Control-D 退出)
请输入另一个输入文件名:
用什么方式来运行它?
这是我的代码:
\documentclass[a4paper]{book}
\usepackage{pgfplots}
\usepackage{python}
\pagestyle{empty}
\begin{document}
\begin{python}
from pylab import *
from scipy.integrate import odeint
from functools import partial
## set initial conditions and parameters
g = 9.81 # acceleration due to gravity
th = 45. # set launch angle
th = th * np.pi/180. # convert launch angle to radians
v0 = 20.0 # set speed
x0=0 # specify initial conditions
y0=0
vx0 = v0*sin(th)
vy0 = v0*cos(th)
## define function to compute f(X,t)
def f_func_k(k,state,time):
f = zeros(4) # create array to hold f vector
f[0] = state[2] # f[0] = x component of velocity
f[1] = state[3] # f[1] = x component of velocity
f[2] = - k*(f[0]**2 + f[1]**2)**(0.5)*f[0] # f[2] = acceleration in x direction
f[3] = -g - k*(f[0]**2 + f[1]**2)**(0.5)*f[1] # f[3] = acceleration in y direction
return f
def gen_plot_data(s,k):
f_func = partial(f_func_k,k)
## set initial state vector and time array
X0 = [ x0, y0, vx0, vy0] # set initial state of the system
t0 = 0.
# tf = input("Enter final time: ")
# tau = input("Enter time step: ")
tf = 3
tau = 0.05
# create time array starting at t0, ending at tf with a spacing tau
t = arange(t0,tf,tau)
## solve ODE using odeint
X = odeint(f_func,X0,t) # returns an 2-dimensional array with the
# first index specifying the time and the
# second index specifying the component of
# the state vector
# putting ':' as an index specifies all of the elements for
# that index so x, y, vx, and vy are arrays at times specified
# in the time array
x = X[:,0]
y = X[:,1]
vx = X[:,2]
vy = X[:,3]
xy = zip(x,y)
np.savetxt(s,xy,fmt='%0.5f')
gen_plot_data("plotdata1.dat",0.04)
gen_plot_data("plotdata2.dat",0)
\end{python}
\begin{tikzpicture}
\begin{axis}[no marks,samples=100,axis lines=center,xlabel=$x$,ylabel=$y$,enlargelimits]
\addplot table {plotdata1.dat};%
\addplot[magenta] table {plotdata2.dat};%
\end{axis}
\end{tikzpicture}
\end{document}
答案1
在 Mac 10.9.2 和 pdfTeX 版本 3.1415926-2.5-1.40.14 (TeX Live 2013) 上可以正常运行。但是:
将文件另存为plot_air_drag.tex(错误消息表明您正在使用不同的文件名)
修改你的序言
\documentclass[a4paper]{book}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usepackage{python}
启动终端,
键入 cdSpace
并删除文件夹那里的文件。
按 Enter。
并使用pdflatex -shell-escape plot_air_drag.tex