首页 > 解决方案 > plt.plot (rr,xlogistic) 在程序中的什么位置?我是 Python 新手

问题描述

将 numpy 导入为 np 将 matplotlib.pyplot 导入为 plt

def 逻辑 (n,r,initialTerm) :

xlogistic = np.zeros((n,1))
ylogistic = np.zeros((n,1)) 
rr = r * np.ones((n,1))
    
ylogistic[0] = initialTerm
for i in range(1,n,1):
    ylogistic[i] = r*ylogistic[i-1]*(1-ylogistic[i-1])
    #print(ylogistic[i]) 

xlogistic[0] = ylogistic[100]
for i in range(1,n,1):
    xlogistic[i] = r*xlogistic[i-1]*(1-xlogistic[i-1])
    #print(xlogistic[i])
    
plt.plot(rr,xlogistic)

return (xlogistic[i])

""" 测试函数 """ theLogisticMap =logistic(1000,3.9,0.5)

标签: pythonpython-3.x

解决方案


由于问题尚不清楚,我假设您在问如何显示图表。在使用plt.plot和执行其他与图形相关的命令进行绘图后,您可以使用plt.show()在您希望显示的代码行处显示绘图。

此外,您永远不应该在 numpy 索引中循环,这需要很长时间,尝试使用大量需要时间来处理的代码,然后通过将值转换为普通的 python 列表来修改它:

xlogistic = np.zeros((n,1))
ylogistic = np.zeros((n,1)) 
rr = r * np.ones((n,1))

xlogistic = list(xlogistic)
ylogistic = list(ylogistic)
rr = list(rr)

你会发现它的方式更快。

==============================

由于您是 python 新手,因此在显示图表之前可以使用以下命令:

plt.plot(x, y, ’r--’) 

颜色 (b,w,g,....) 和/或样式 (--, -, ..) 的第三个参数。你可以使用一个或两个。

您还可以在同一张图上绘制两个函数。有或没有样式

plt.plot(rr,xlogistic,’r’, x , y, ’--’)

标记一个或多个特定点(与绘制两个函数的逻辑相同)只需绘制一个特定点......最好对像 * 这样的点使用良好的样式,不像 -

plt.plot(x[0], y[0], ’ro’, x[-1], y[-1], ’r*’)

完成图表的一些示例:

plt.xlabel('t (s)')
plt.ylabel('y (m)')
plt.legend(['Line1'])         #one legend
plt.legend([’t**2’, ’e**t’]) #multiple legends
plt.grid('on')
plt.axis([x1, x2, y1, y2])   # axes limits
plt.title(’Title’)


推荐阅读