python - 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)
解决方案
由于问题尚不清楚,我假设您在问如何显示图表。在使用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’)
推荐阅读
- eclipse-plugin - STS 是否支持 spring 集成插件来查看集成流程?
- javascript - 如何正确检索 JQuery 中的值?
- c - 指向整数数组的指针与指向整数的双精度指针
- javascript - 按钮 Onclick 函数在加载时执行
- laravel-5 - VueJS:尽管数组中有对象,但 v-for 指令不渲染项目
- javascript - 提交通过推送器显示的实时表单时出现 419
- reactjs - 使用 React 中的 Axios 发出 http 请求以表达在预飞行后因 CORS 错误而失败。在服务器上启用了 CORS,但仍然无法正常工作
- regex - 匹配直到符号第一次出现
- reactjs - getDerivedStateFromError 后重置状态
- ios - UITextField 没有在 UIStackView 中归档可用宽度