简体   繁体   English

标准偏差Python绘图

[英]Standard Deviation Python Plotting

I need some quick help with plotting the answers I get for a code. 我需要一些快速帮助来绘制我从代码中得到的答案。 I'm pretty new to this, nonetheless, I'm trying to compute the standard deviation of the answers of the calculation, after which I need to plot the new answers of the standard deviation calculation just performed into a graph. 尽管如此,我还是很陌生,我正在尝试计算计算答案的标准偏差,然后我需要将刚刚执行的标准偏差计算的新答案绘制到图形中。 The problem is the plot appears before the calculation is performed just giving an empty plot, and then allowing for the running of the code. 问题在于,在执行计算之前就出现了一个图,只是给出了一个空图,然后允许运行代码。 Any help would be greatly appreciated. 任何帮助将不胜感激。

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)

Disregard the x and y labels they're dummy variables. 忽略x和y标签是虚拟变量。

您从未调用过standard_deviation函数,因此在将它传递给plotstdofmag=[]为空列表

Your stdofmag variable is an empty list so you are plotting nothing. 您的stdofmag变量是一个空列表,因此您没有进行任何绘图。 Your script as written isn't actually doing any work except creating a figure setting some labels and showing it. 您编写的脚本实际上并没有做任何工作,只是创建一个设置一些标签的图形并显示它。 You need to actually call the functions you define in order for your lists to get filled. 您需要实际调用您定义的函数才能填充列表。 That being said I can see numerous other errors that will prevent these functions from working as you probably intend. 话虽如此,我仍然可以看到许多其他错误,这些错误将阻止这些功能按您的预期工作。 Might be a good idea to take a step back and make sure each function is operating as you intend before trying to connect everything is one block. 最好退后一步,确保每个功能都按预期运行,然后再尝试连接所有功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM