簡體   English   中英

標准偏差Python繪圖

[英]Standard Deviation Python Plotting

我需要一些快速幫助來繪制我從代碼中得到的答案。 盡管如此,我還是很陌生,我正在嘗試計算計算答案的標准偏差,然后我需要將剛剛執行的標准偏差計算的新答案繪制到圖形中。 問題在於,在執行計算之前就出現了一個圖,只是給出了一個空圖,然后允許運行代碼。 任何幫助將不勝感激。

from numpy import zeros
from random import choice, random
import math

def create_lattice(nx,ny):
    possibleSpins = (-1,1)
    lattice = zeros((nx,ny))
    for i in range(nx):
        for j in range(ny):
            lattice[i][j] = choice(possibleSpins)
    return lattice

def magnetization(nx, ny, lattice):
    magnet = 0.0
    for i in range(nx):
        for j in range(ny):
            magnet += (lattice[i][j]/(nx*ny))
    return magnet

def ising_model(nsweeps, nx, ny, Js):
    magnet = 0.0
    s_energy = 0.0
    e_energy = 0.0
    d_energy = 0.0
    spin = 0.0
    rand = 0.0
    good = 0.0
    bad = 0.0
    nostep = 0.0
    lattice = create_lattice(nx, ny)
    magnet = magnetization(nx, ny, lattice)
    energies = zeros((nx,ny))
    print(lattice)
    # Each sweep is a complete look at the lattice
    for sweeps in range(nsweeps):
        for i in range(nx):
            for j in range(ny):
                spin = lattice[i][j]
                s_energy = -1 * Js * spin * (lattice[(i-1)][j] + lattice[i][(j-1)] + lattice[i][(j+1)%ny] + lattice[(i+1)%nx][j])
                lattice[i][j] = -1 * spin
                e_energy = -1 * Js * lattice[i][j] * (lattice[(i-1)][j] + lattice[i][(j-1)] + lattice[i][(j+1)%ny] + lattice[(i+1)%nx][j])
                d_energy = e_energy - s_energy
                rand = random()
                if d_energy <= 0 :
                    good = good + 1
                    magnet += ((-2*spin)/(nx*ny))
                    answers.append(magnet)
                elif d_energy > 0 and rand <= math.exp(-1 * d_energy):
                    bad = bad + 1
                    magnet += ((-2*spin)/(nx*ny))
                    answers.append(magnet)
                else:
                    lattice[i][j] = spin
                    nostep = nostep + 1
                print(magnet)    
    print(lattice)
    print(good)
    print(bad)
    print(nostep)
    # energies array is
    return energies

answers = []
stdofmag = []
def standard_deviation():
    stdmag = statistics.stdev(answers)
    print(stdmag)
    stdofmag.append(stdmag)

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(stdofmag, 'r-', label = "Std of Magnetization")
title_temp = "Magnetization"
plt.title(title_temp, fontsize=12, fontweight='bold', color='green')
ax.legend(loc='best', ncol=1, fancybox=True, shadow=True)
plt.xlabel('Number of Iterations')
plt.ylabel('Answer') 
ax.grid(True)
plt.show(block=True)

忽略x和y標簽是虛擬變量。

您從未調用過standard_deviation函數,因此在將它傳遞給plotstdofmag=[]為空列表

您的stdofmag變量是一個空列表,因此您沒有進行任何繪圖。 您編寫的腳本實際上並沒有做任何工作,只是創建一個設置一些標簽的圖形並顯示它。 您需要實際調用您定義的函數才能填充列表。 話雖如此,我仍然可以看到許多其他錯誤,這些錯誤將阻止這些功能按您的預期工作。 最好退后一步,確保每個功能都按預期運行,然后再嘗試連接所有功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM