为什么 matplotlib 的 xticks 使情节消失?

问题描述

我正在绘制伴随蒙特卡罗测试和伴随模拟的结果,所以我在 matplotlib 中的一些数据点旁边绘制了一条曲线;我需要 x 刻度以 1 秒为间隔从 0 到 10,所以我使用了 plt.xticks(np.linspace(0,10,11)),这 - 起初 - 有效,但更改我的代码中的任何内容都会使曲线变得不可见,即使只是移动我的光标运行时从 linspace 到代码的另一部分会破坏它......我不明白这到底是怎么可能的,即使使用手动枚举列表它也根本不起作用。 ex [0,1,2,...] 或多或少我实际上无法使用图表,除非我让 matplotlib 自动设置 x 刻度。任何人都可以深入了解正在发生的事情以及如何解决它?

Here's my actual graph when it's working

以及我拼凑起来绘制数据的代码

plt.plot(Time2,Adjoint,'k:',label='Adjoint Simulation')
plt.scatter(Time,SIGMA,marker='+',color='black',linewidth=1,label='Monte Carlo Calculation')
#plt.plot(Time,AVERAGE,'k:')
plt.title('Mean Squared Miss vs Flight Time')
plt.xlabel('Flight Time (S)')
plt.ylabel('Standard deviation (Ft)')
plt.ylim(0,50)
plt.xticks(np.linspace(0,11))
plt.legend()
plt.show()

编辑:时间的数据只是一个列表 1-10,每个列表都有一个数据点,这个散点图没有问题。 Time2 的数据以 0.01 秒的间隔通过梯级库塔积分循环与 y 数据一起生成。当我不将 xticks 留给 matplotlib 时,我的伴随运行曲线的图完全消失了 - 数据不变,仅触摸 xtick 设置,但该图仅显示散点图......当我使用 linspace 时,如果并且只有当我的光标位于 linspace 命令内时,它才会起作用,但前提是代码中没有其他任何更改 - 我绝对不知道这是怎么可能的,但它似乎是一致的...... .

我对堆栈溢出很陌生,我不确定添加示例数据的最佳方法 - 但代码只有 100 行,其中大部分是没有问题的蒙特卡罗循环,所以希望这会有所帮助

And here,to show what I mean by 'breaking': the only difference is that I deleted the '#' in the line setting the sticks for the second plot

import numpy as np
import matplotlib.pyplot as plt
import timeit

Vc,XNT,Vm,XNP,Tau,Runs = 4000,96.6,3000,3,50
Z = np.zeros([1000,1])
Time = []
SIGMA= []
AVERAGE = []
start_time = timeit.default_timer()
for TF in range(1,11,1):
    Z1 = 0
    for j in range(1,Runs+1):
        TStart = TF * np.random.uniform()
        PZ = np.random.uniform(-1,1)
        if PZ>0:
            Coef = 1
        else:
            Coef = -1
        Y = 0; YD = 0
        T = 0; S  = 0
        H = .01
        XNc = 0; XNL = 0
        while T < (TF-.0001):
            if T<TStart:
                XNT = 0
            else:
                XNT = Coef * 96.6
            YOLD = Y
            YDOLD= YD
            XNLOLD = XNL

            TGo = TF-T+.00001
            Rtm = Vc * TGo
            LambdaD = (Rtm*YD+Y*Vc)/(Rtm**2)
            XNc = XNP * Vc * LambdaD
            XNLD= (XNc-XNL)/Tau
            YDD = XNT-XNL

            Y += H*YD
            YD+= H*YDD
            XNL += H*XNLD
            T+=H

            TGo = TF-T+.00001
            Rtm = Vc * TGo
            LambdaD = (Rtm*YD+Y*Vc)/np.sqrt(Rtm)
            XNc = XNP * Vc * LambdaD
            XNLD= (XNc-XNL)/Tau
            YDD = XNT-XNL

            Y = .5*(YOLD + Y + H*YD)
            YD= .5*(YDOLD+ YD+ H*YDD)
            XNL + .5*(XNLOLD + XNL + H*XNLD)
            S+=H

        Z[j] = Y
        Z1 += Y
        MEAN = Z1/j
    Sigma = 0; Z1 = 0
    for i in range(1,Runs):
        Z1 += (Z[i]-MEAN)**2
        if i == 1:
            Sigma = 0
        else:
            Sigma = np.sqrt(Z1/(i-1))
    Time.append(TF)
    SIGMA.append(Sigma)
    AVERAGE.append(MEAN)

print('Monte-Carlo ',(timeit.default_timer()-start_time))
start_time = timeit.default_timer()

T=0; S=0
TP=T+.00001
H = .01
X1 = 0
X2 = 0
X3 = 1
X4 = 0
X5 = 0

Adjoint = []
Time2 = []

while TP<(TF-.00001):
    S+=H
    X1OLD = X1
    X2OLD = X2
    X3OLD = X3
    X4OLD = X4
    X5OLD = X5

    X1D = X2
    X2D = X3
    Y1 = (X4-X2)/Tau
    TGo = TP+.00001
    X3D = XNP*Y1/TGo
    X4D = -Y1
    X5D = X1*X1

    X1 += H*X1D
    X2 += H*X2D
    X3 += H*X3D
    X4 += H*X4D
    X5 += H*X5D
    TP += H

    X1D = X2
    X2D = X3
    Y1 = (X4-X2)/Tau
    TGo = TP+.00001
    X3D = XNP*Y1/TGo
    X4D = -Y1
    X5D = X1*X1

    X1 = .5*(X1OLD + X1 + H*X1D)
    X2 = .5*(X2OLD + X2 + H*X2D)
    X3 = .5*(X3OLD + X3 + H*X3D)
    X4 = .5*(X4OLD + X4 + H*X4D)
    X5 = .5*(X5OLD + X5 + H*X5D)

    if S < .099999:
        continue
    S=0
    Adjoint.append(XNT*np.sqrt(X5/TGo))
    Time2.append(TP)

print('Adjoint ',(timeit.default_timer()-start_time))
start_time = timeit.default_timer()
TF= np.linspace(.001,101)
x = TF/Tau
MUDNT = XNT/4 * np.sqrt((Tau**5/TF)*(3-(np.exp(-2*x)*(2*x**4+4*x**3+6*x**2+6*x+3))))
print('Analytical Solution ',(timeit.default_timer()-start_time))

plt.plot(Time2,label='Adjoint Simulation')
plt.plot(TF,MUDNT,'k-',label='Analytical Solution')
plt.scatter(Time,50)
#plt.xticks(np.linspace(0,11))
plt.legend()
plt.show()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)