2015-10-04 71 views
3

我想比较几个算法的收敛概率曲线图。如何使pyplot的值均匀分布y值[0,1/2,3/4,7/8,...]

目前,我的图如下所示:

enter image description here

不允许看到许多曲线的差异。我想要y轴是“对数”,但在其与数值1的差异,即我想y值是[0,1/2,3/4,7/8,15/16,... 1023/1024],但是每一个嘀嗒仍然会有距离最后一个距离(即1/2到3/4的距离与15/16到31/32的距离相同)。

我使用yticks()功能试过了,但它并没有均匀地放置蜱:

enter image description here

如何使这个轴是否正确?


我当前的代码:

def plotCDFs(CDFs, names = []): 
    legend = [] 
    for i, CDF in enumerate(CDFs): 
     keys = sorted(CDF) 
     vals = sorted(CDF.values()) 
     plt.plot(keys,vals) 
     legend.append(str(names[i])) 
    plt.title('Cumulative Distribution') 
    plt.legend(legend, loc='lower right') 
    plt.xscale('log') 
    plt.gca().set_ylim([0,1]) 
    #plt.yticks([1-2**-i for i in xrange(11)]) 
    plt.show() 
+0

下面的答案有帮助吗? – plonser

回答

0

有两种可能性:你可以在一个普通的双对数坐标绘制1-cumulative Distribution这是我平时做,或者你(可能)有按照上面的描述创建自己的日志。至少我从来没有见过这样的内建函数。

此代码应工作

import numpy as np 
import matplotlib.pyplot as plt 

def ToLog(x): 
    return 1.-np.log10(1.-x) 

def plotCDFs(CDFs, names = []): 
    legend = [] 
    max_vals = 0.0 
    for i, CDF in enumerate(CDFs): 
     keys = sorted(CDF) 
     vals = sorted(CDF.values()) 
     if vals.max() > max_vals: 
      max_vals = vals 
     plt.plot(keys,ToLog(vals)) 
     legend.append(str(names[i])) 
    plt.title('Cumulative Distribution') 
    plt.legend(legend, loc='lower right') 
    plt.xscale('log') 

    # handling the yaxis ticks and ticklabels 
    i_max = np.floor(np.log(1-max_vals.max())/np.log(2.)) 
    yticks = 1.-2.**np.linspace(i_max,0,-i_max+1) 
    ax = plt.gca() 
    ax.set_yticks(1.-np.log10(1.-yticks)) 
    ax.set_yticklabels([str(i-1)+'/'+str(i) for i in 2**np.arange(-int(i_max),0,-1)]) 

    ax.set_ylim([0,1]) 
    plt.show() 

注意ToLog必须在所有ydata绘制之前应用。