使用对数轴设置绘图的y限制(使用plt.semilogy)

时间:2015-04-22 05:45:44

标签: python matplotlib

我想用对数轴限制我的情节的y尺度。但是,添加plt.ylim((10^(-1),10^(0)))似乎没有任何改变。当我使用plt.semilogy时,我应该使用不同的命令吗?下面是代码和数据。

# Generate loss plots
    # --------------- Latex Plot Beautification --------------------------
    fig_width_pt = 492.0 #246.0  # Get this from LaTeX using \showthe\columnwidth
    inches_per_pt = 1.0/72.27               # Convert pt to inch
    golden_mean = (np.sqrt(5)-1.0)/2.0         # Aesthetic ratio
    fig_width = fig_width_pt*inches_per_pt  # width in inches
    fig_height = fig_width*golden_mean      # height in inches
    fig_size =  [fig_width+1,fig_height+1]
    params = {'backend': 'ps',
              'axes.labelsize': 12,
              'font.size': 12,
              'legend.fontsize': 10,
              'xtick.labelsize': 10,
              'ytick.labelsize': 10,
              'text.usetex': False,
              'figure.figsize': fig_size}
    plt.rcParams.update(params)
    # --------------- Latex Plot Beautification --------------------------

    train = {}

    tmp = list()
    with open('loss.csv', 'rb') as csv_file:
        reader = csv.reader(csv_file)
        for i, row in enumerate(reader):
            if i != 0:
                tmp.append(row)   


    tmp     = np.array(tmp)    

    train['iters'], train['seconds'], train['loss'], train['learn_rate'] = tmp[:,0], tmp[:,1], tmp[:,2], tmp[:,3]

    plt.subplot(211)
    plt.semilogy(train['iters'],train['loss'],'b',lw=2)
    plt.ylabel('loss')
    plt.ylim((10^(-1),10^(0)))

    plt.subplot(212)
    plt.semilogy(train['iters'],train['learn_rate'],'b',lw=2)
    plt.xlabel('iterations')
    plt.ylabel('learning rate')

    plt.show()

loss.csv

   NumIters,Seconds,TrainingLoss,LearningRate
    0.0,0.486213,0.693148,nan
    1000.0,7.557165,0.0961085,0.05
    2000.0,14.041684,0.00384812,0.05
    3000.0,20.410506,7.34072,0.05
    4000.0,26.772446,4.78843,0.05
    5000.0,34.117291,2.45869,0.05
    6000.0,40.249146,0.179548,0.05
    7000.0,46.377004,0.0033729,0.05
    8000.0,52.499923,0.00020626,0.05
    9000.0,59.317026,2.0962,0.05
    10000.0,66.679739,1.20523,0.05
    11000.0,72.846874,0.00894074,0.05
    12000.0,78.87727,2.37395,0.05
    13000.0,84.950737,0.00172985,0.05
    14000.0,91.036988,8.13143,0.05
    15000.0,98.153062,2.90689,0.05
    16000.0,104.252995,1.78791,0.05
    17000.0,110.286827,5.10336,0.05
    18000.0,116.47252,3.34482,0.05
    19000.0,122.683825,0.00838974,0.05
    20000.0,129.637347,0.00341582,0.05
    21000.0,135.640689,1.66777,0.05
    22000.0,141.66995,3.30503,0.05
    23000.0,147.721727,2.53775,0.05
    24000.0,154.084407,1.35748,0.05
    25000.0,161.426044,2.28748,0.05
    26000.0,168.492162,0.00397386,0.05
    27000.0,174.669545,0.000113542,0.05
    28000.0,180.803535,2.5192,0.05
    29000.0,187.004627,0.0019179,0.05
    30000.0,194.150244,4.36825,0.05
    31000.0,200.404565,1.38513,0.05
    32000.0,206.412659,0.0108084,0.05
    33000.0,212.437014,6.41096,0.05
    34000.0,218.56177,0.000235395,0.05
    35000.0,225.853988,7.88834,0.05
    36000.0,231.888062,0.00109338,0.05
    37000.0,238.976116,4.46498,0.05
    38000.0,246.112036,0.00246135,0.05
    39000.0,252.92424,0.00154073,0.05
    40000.0,261.114472,1.49658,0.05
    41000.0,268.695987,3.09471,0.05
    42000.0,275.331985,0.000266829,0.05
    43000.0,282.34568,1.06778,0.05
    44000.0,290.059307,5.98044,0.05
    45000.0,299.376506,0.00154176,0.05
    46000.0,306.722876,9.46019,0.05
    47000.0,314.33918,1.1353,0.05
    48000.0,321.358202,7.14507,0.05
    49000.0,328.710997,1.00035,0.05
    50000.0,335.206681,4.40056,0.05

1 个答案:

答案 0 :(得分:6)

^运算符执行按位异或:10 ^ -1 = -11,10 ^ 0为10(ref:Python operators)。使用**加注电源,或使用pow()功能。所以你可以使用:

plt.ylim( (10**-1,10**0) )

或者如果你想要更详细:

plt.ylim( (pow(10,-1),pow(10,0)) )