【问题标题】:Bifurcation Diagram not plotting / Plot just not showing up分岔图未绘制/绘图只是未显示
【发布时间】:2016-12-02 09:27:33
【问题描述】:

我的家庭作业让我编写了一个代码,该代码模拟了泰勒教科书中经典力学中的数字。数字 和 如果有人有兴趣知道。

我能够重现其中一个,即下面的代码(这可能是我实际遇到问题的代码的一个很好的参考):

import nympy as np
import matplotlib.pyplot as plt

# We need to calculate the first fixed point
r1=np.array(np.arange(0,4,0.09))
x1 = np.zeros((len(r1),1))

# Now calculating the second fixed point
r2=np.array(np.arange(1,4,0.1))
x2 = (r2 -1)/r2

# Now finding when the fixed points split up again
r3=np.array(np.arange(3,4,0.1))
y1 = (((r3**2 - 2*r3 - 3)**0.5) + 1 + r3)/(2*r3)
y2 = ((-(r3**2 - 2*r3 - 3)**0.5) + 1 + r3)/(2*r3)

# Now finding the experimental values for 1/2 of a split
x3 = []
for r in np.arange(0,4,0.09):
    x = 0.666
    for i in range(100):
       x = (r**2) * x * (1.0 -x) - (r**3) * (x**2)*((1-x)**2)
    x3.append(x)

# Doing the same as above second 1/2
x4 = []
for r in np.arange(0,4,0.09):
    x = 0.8
    for i in range(100):
       x = (r**2) * x * (1.0 -x) - (r**3) * (x**2)*((1-x)**2)
    x4.append(x)

plt.plot(r1,x3,'bo', label='Experimental')
plt.plot(r1,x4,'bo')
plt.plot(r3,y2,'k-')
plt.plot(r3,y1,'k-')
plt.plot(r1,x1,'k-', label='Theoretical')
plt.plot(r2,x2,'k-')
plt.legend(loc=2)
plt.show()

这是第二张图片的代码,它似乎不起作用。我不确定为什么。任何帮助,将不胜感激。该图只是没有绘制,我不确定为什么。

import numpy as np
import matplotlib.pyplot as plt

for r in n.arange(2.8,4,0.01):
    x = 0.5
    for i in range(150):
        x = r*x*(1-x)
        if i >= 125:
            plt.plot(r,x,'k')
plt.xlim (2.8,4)
plt.show()

【问题讨论】:

  • 欢迎堆栈溢出。请阅读how to ask a good question。另外,请粘贴您的代码而不是插入图像。帖子中有一个{} 按钮,它将所有内容缩进四个空格,并将显示为代码
  • 你在使用 IPython 笔记本吗?

标签: python matplotlib plot ipython


【解决方案1】:

您的代码无法正常工作是有充分理由的。

当 x 和 y 需要一个数组时,您使用标量值反复调用 plt.plot:很难绘制只有 一个 点的线。 试试这个,你会看到你会得到一个空的情节:

plt.plot(0,1)
plt.show();

我不知道这是否是您最终要寻找的东西,但这里有一个工作版本,您至少可以将其用作起点。请注意,我更改了 i 上的循环:而不是先执行 i in range(150),然后执行 if i >=125,最好只执行 i in range(125,150),这样可以避免不必要的迭代:

fig, ax = plt.subplots(nrows=1, ncols=1)
r1 = []
x1 = []
for r in np.arange(2.8,4,0.01):
    x=0.5
    # Instead of range(150) then checking if i >= 125...
    for i in range(125,150):
        x = r*x*(1-x)
        r1.append(r)
        x1.append(x)

ax.plot(r1,x1,'k')
plt.show()

【讨论】:

    【解决方案2】:

    我偶然发现了这篇一年前的帖子,并意识到对于 OP 报告的问题有一个简单的答案。

    缺少的部分是 ma​​rker 参数。

    plot(r,x,'k') 替换为plot(r,x,'.',color='k'),然后分岔图突然显露出来。

    import numpy as np
    import matplotlib.pyplot as plt
    
    plt.figure()
    for r in n.arange(2.8,4,0.01):
        x = 0.5
        for i in range(150):
            x = r*x*(1-x)
            if i >= 125:
                plt.plot(r,x,'.',color='black',markersize=2)
    plt.xlim (2.8,4)
    plt.show()
    

    这段代码很好,虽然效率很低。只调用一次plot() 会快得多。

    plt.figure()
    def mark(r,x,diagram,v):
        N,M = np.shape(diagram)
        rmin,rmax = r[0],r[-1]
        for (i,j) in zip(r,x):
            nx = int(j*(M-1))
            nr = int((i-rmin)/(rmax-rmin)*(N-1))
            diagram[nx,nr] = v
    
    r = np.arange(2.8,4,0.01)
    diagram = np.zeros((200,200))
    x0 = 0.5
    x = np.ones_like(r)*x0
    for i in range(150):
        x = np.multiply(np.multiply(r,x),(1-x))
        if i >= 125:
            mark(r,x,diagram,1)
    
    plt.imshow(np.flipud(diagram), extent=[r[0],r[-1],0,1],
               aspect=(r[-1]-r[0]),cmap=plt.cm.Greys)
    plt.show()
    

    后面的代码使用

    CPU times: user 224 ms, sys: 29 µs, total: 224 ms
    Wall time: 261 ms
    

    而前者效率较低的代码需要花费 30 倍以上的时间

    CPU times: user 8.59 s, sys: 91.7 ms, total: 8.68 s
    Wall time: 8.68 s
    

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-27
      • 2021-12-17
      相关资源
      最近更新 更多