for i in range(par.n): axes = plt.gca() axes.set_ylim([-20,50]) plt.plot(ttt,Pth, 'r' ) plt.plot(ttt,pot_arrays[i]) plt.show()
时间: 2024-03-04 17:51:28 浏览: 17
这段代码可以用来绘制多个曲线图。具体来说,代码通过一个 for 循环来遍历 par.n,对于每个 i,它会执行以下操作:
1. 获取当前的坐标轴对象 axes。
2. 设置 y 轴的范围为 [-20, 50]。
3. 使用红色 ('r') 绘制一条固定的曲线,该曲线的 x 轴是 ttt,y 轴是 Pth。
4. 使用默认颜色绘制一条曲线,该曲线的 x 轴是 ttt,y 轴是 pot_arrays[i]。
5. 调用 plt.show() 函数将图形显示出来。
因此,每次循环都会生成一个新的图形窗口,并在该图形窗口中绘制两条曲线。注意,由于在循环内部调用了 plt.show() 函数,因此每次循环都会显示一个新的图形窗口,直到循环结束。
相关问题
for i in range(par.n): axes = plt.gca() axes.set_ylim([-20,50]) plt.plot(ttt,Pth, 'r' ) plt.plot(ttt,pot_arrays[i]) plt.show()
这段代码中,通过一个 for 循环来遍历 par.n,对于每个 i,都会创建一个新的图形窗口并绘制两条曲线。第一条曲线使用红色绘制 ttt 和 Pth,并将 y 轴限制在 -20 到 50 的范围内。第二条曲线使用默认颜色绘制 ttt 和 pot_arrays[i]。最后,调用 plt.show() 函数将图形显示出来。由于每个 i 都会创建一个新的图形窗口,因此在循环内部每次调用 plt.show() 函数可以看到一个新的图形。
import numpy as np from matplotlib_inline import backend_inline from d2l import torch as d2l def f(x): return 3*x**2-4*x def numerical_lim(f,x,h): return(f(x+h)-f(x))/h def use_svg_display(): #@save backend_inline.set_matplotlib_formats('svg') def set_figsize(figsize=(3.5,2.5)): #@save use_svg_display() d2l.plt.rcParams['figure.figsize'] = figsize #@save def set_axes(axes,xlabel,ylabel,xlim,ylim,xscale,yscale,legend): axes.set_xlabel(xlabel) axes.set_ylabel(ylabel) axes.set_xscale(xscale) axes.set_yscale(yscale) axes.set_xlim(xlim) axes.set_ylim(ylim) if legend: axes.legend(legend) axes.grid() #@save def plot(X,Y=None,xlabel=None,ylabel=None,legend=None,xlim=None,ylim=None,xscale='linear',yscale='linear', fmts=('-','m--','g-.','r:'),figsize=(3.5,2.5),axes=None): if legend is None: legend = [] set_figsize(figsize) axes = axes if axes else d2l.plt.gca() def has_one_axis(X): return (hasattr(X,"ndim")and X.ndim == 1 or isinstance(X,list) and not hasattr(X[0],"__len__")) if has_one_axis(X): X = [X] if Y is None: X,Y = [[]]*len(X),X if has_one_axis(Y): Y = [Y] if len(X) != len(Y): X = X*len(Y) axes.cla() for x,y,fmt in zip(X,Y,fmts): if len(x): axes.plot(x,y,fmt) else: axes.plot(y,fmt) set_axes(axes,xlabel,ylabel,xlim ,ylim,xscale,yscale,legend) x = np.arange(0,3,0.1) plot(x,[f(x),2*x-3],'x','f(x)',legend=['f(x)','Tangent line(x=1)'])这段代码为什么在pycharm中运行不出来图形
这段代码在 PyCharm 中无法显示图形的原因可能是因为缺少了 `plt.show()` 的调用。在 PyCharm 中,需要手动调用 `plt.show()` 来显示图形窗口。你可以在代码的最后添加 `plt.show()` 来尝试解决该问题。修改后的代码如下所示:
```python
import numpy as np
from matplotlib_inline import backend_inline
from d2l import torch as d2l
def f(x):
return 3*x**2-4*x
def numerical_lim(f,x,h):
return(f(x+h)-f(x))/h
def use_svg_display(): #@save
backend_inline.set_matplotlib_formats('svg')
def set_figsize(figsize=(3.5,2.5)): #@save
use_svg_display()
d2l.plt.rcParams['figure.figsize'] = figsize
#@save
def set_axes(axes,xlabel,ylabel,xlim,ylim,xscale,yscale,legend):
axes.set_xlabel(xlabel)
axes.set_ylabel(ylabel)
axes.set_xscale(xscale)
axes.set_yscale(yscale)
axes.set_xlim(xlim)
axes.set_ylim(ylim)
if legend:
axes.legend(legend)
axes.grid()
#@save
def plot(X,Y=None,xlabel=None,ylabel=None,legend=None,xlim=None,ylim=None,xscale='linear',yscale='linear',
fmts=('-','m--','g-.','r:'),figsize=(3.5,2.5),axes=None):
if legend is None:
legend = []
set_figsize(figsize)
axes = axes if axes else d2l.plt.gca()
def has_one_axis(X):
return (hasattr(X,"ndim")and X.ndim == 1 or isinstance(X,list)
and not hasattr(X[0],"__len__"))
if has_one_axis(X):
X = [X]
if Y is None:
X,Y = [[]]*len(X),X
if has_one_axis(Y):
Y = [Y]
if len(X) != len(Y):
X = X*len(Y)
axes.cla()
for x,y,fmt in zip(X,Y,fmts):
if len(x):
axes.plot(x,y,fmt)
else:
axes.plot(y,fmt)
set_axes(axes,xlabel,ylabel,xlim
,ylim,xscale,yscale,legend)
x = np.arange(0,3,0.1)
plot(x,[f(x),2*x-3],'x','f(x)',legend=['f(x)','Tangent line(x=1)'])
d2l.plt.show()
```
请注意,`import matplotlib.pyplot as plt` 语句和 `plt.show()` 的调用被添加到了代码中。运行修改后的代码,你应该能够在 PyCharm 中看到图形窗口显示出来。