Exemple #1
0
from mpl_toolkits.axes_grid.parasite_axes import HostAxes, ParasiteAxes
import matplotlib.pyplot as plt

if __name__ == "__main__":
    fig = plt.figure(1)

    host = HostAxes(fig, [0.15, 0.1, 0.65, 0.8])
    par1 = ParasiteAxes(host, sharex=host)
    par2 = ParasiteAxes(host, sharex=host)
    host.parasites.append(par1)
    host.parasites.append(par2)

    host.set_ylabel("Density")
    host.set_xlabel("Distance")

    host.axis["right"].set_visible(False)
    par1.axis["right"].set_visible(True)
    par1.set_ylabel("Temperature")

    par1.axis["right"].major_ticklabels.set_visible(True)
    par1.axis["right"].label.set_visible(True)

    par2.set_ylabel("Velocity")
    offset = (60, 0)
    new_axisline = par2._grid_helper.new_fixed_axis
    par2.axis["right2"] = new_axisline(loc="right", axes=par2, offset=offset)

    fig.add_axes(host)

    host.set_xlim(0, 2)
    host.set_ylim(0, 2)
from mpl_toolkits.axes_grid.parasite_axes import HostAxes, ParasiteAxes
import matplotlib.pyplot as plt

if __name__ == "__main__":
    fig = plt.figure(1)

    host = HostAxes(fig, [0.15, 0.1, 0.65, 0.8])
    par1 = ParasiteAxes(host, sharex=host)
    par2 = ParasiteAxes(host, sharex=host)
    host.parasites.append(par1)
    host.parasites.append(par2)

    host.set_ylabel("Density")
    host.set_xlabel("Distance")

    host.axis["right"].set_visible(False)
    par1.axis["right"].set_visible(True)
    par1.set_ylabel("Temperature")

    par1.axis["right"].major_ticklabels.set_visible(True)
    par1.axis["right"].label.set_visible(True)

    par2.set_ylabel("Velocity")
    offset = (60, 0)
    new_axisline = par2._grid_helper.new_fixed_axis
    par2.axis["right2"] = new_axisline(loc="right",
                                       axes=par2,
                                       offset=offset)


    fig.add_axes(host)
Exemple #3
0
def _plotMTCombinedDouble(conf,opConf, data, opData, subject='__'):
    mt, time, idS, id, p = data['all']
    _mt, _time, _idS, _id, _p = opData['all']
    
    time2 = numpy.linspace(time.min(),time.max(),30000)
    _time2 = numpy.linspace(_time.min(),_time.max(),30000)

    figname = os.path.join(grPath,subject+'_'+conf[0]+'-'+conf[1]+'_MTGraph_Combined'+grExt)
    figtitle = subject+' '+conf[0]+'-'+conf[1]+'  '+' Double plot'
    fig = pylab.figure()
    
    host = HostAxes(fig, [0.15, 0.1, 0.65, 0.8])
    par1 = ParasiteAxes(host, sharex=host)
    host.parasites.append(par1)

    host.set_ylabel("MT (ms)")
    host.set_xlabel("Time (ms)")

    host.axis["right"].set_visible(False)
    par1.axis["right"].set_visible(True)
    par1.set_ylabel("ID")

    par1.axis["right"].major_ticklabels.set_visible(True)
    par1.axis["right"].label.set_visible(True)

    fig.add_axes(host)
    
    sigmoidArr = sigmoid(p,time2)
    _sigmoidArr = sigmoid(_p,_time2)
    t2 = time2[numpy.where(abs(sigmoidArr - (sigmoidArr.min() + sigmoidArr.max())/2)< eps2)[0]]     
    _t2 = time2[numpy.where(abs(_sigmoidArr - (_sigmoidArr.min() + _sigmoidArr.max())/2)< eps2)[0]]
    t50 = t2
    _t50 = _t2
    if isinstance(t50, numpy.ndarray):
        t50 = t50.mean()
    if isinstance(_t50, numpy.ndarray):
        _t50 = _t50.mean()
    host.set_xlim(0, 90000)
    #host.set_ylim(mt.min(),mt.max())
    
    if conf[0] == '4':
        host.set_ylim(200,900)
        host.set_xlim(0, 70000)
    else:
        host.set_ylim(200,1600)
        host.set_xlim(0, 90000)

    host.scatter(time,mt,s=0.1,c='r',marker='x',linewidth=0.1,label="MT")
    host.scatter(_time,_mt,s=0.1,c='b',marker='x',linewidth=0.1,label="MT")
    host.plot(time2,sigmoid(p,time2),'-r',linewidth=1.5,label="Fitted MT")
    host.plot(_time2,sigmoid(_p,_time2),'-b',linewidth=1.5,label="Fitted MT")
    par1.scatter(time, id, s=1, c='r',marker='o',linewidth=0.5,label="ID") 
    par1.scatter(_time, _id,s=1,c='b',marker='o',linewidth=0.5,label="ID") 
    msg = "MT50-1 = %f; MT50-2 = %f" % (t50,_t50)
    host.text(  0.2 , 0.95, msg, fontsize = 8,\
                horizontalalignment='left', verticalalignment='center',\
                transform = host.transAxes)
    host.vlines(t50,mt.min(),mt.max())
    host.vlines(_t50,mt.min(),mt.max())
    if conf[0] == '4':
        host.set_ylim(200,900)
        host.set_xlim(0, 70000)
    else:
        host.set_ylim(200,1600)
        host.set_xlim(0, 90000)
    par1.set_ylim(min(int(conf[0]),int(conf[1])),max(int(conf[0]),int(conf[1])))
    #host.legend()
    host.set_title(figtitle)
    fig.savefig(figname)
    pylab.close() 
Exemple #4
0
def _plotMTInd(conf, data, subject='__'):
    for i , (mt, time, idS, id,p) in enumerate(data['ind']):
        time2 = numpy.linspace(time.min(),time.max(),30000)
        
        figname = os.path.join(grPath,subject+'_'+conf[0]+'-'+conf[1]+'_'+conf[2]+'_'+str(i)+'_MTGraph_OneTrial'+grExt)
        figtitle = subject+' '+conf[0]+'-'+conf[1]+'  '+conf[2]+' Replication no: '+str(i)
        fig = pylab.figure()
        

        host = HostAxes(fig, [0.15, 0.1, 0.65, 0.8])
        par1 = ParasiteAxes(host, sharex=host)
        host.parasites.append(par1)
        host.set_ylabel("MT (ms)")
        host.set_xlabel("Trial Time (ms)")
        par1.set_ylabel("ID")
        host.axis["right"].set_visible(False)
        par1.axis["right"].set_visible(True)
        par1.axis["right"].major_ticklabels.set_visible(True)
        par1.axis["right"].label.set_visible(True)

        fig.add_axes(host)
        sigmoidArr = sigmoid(p,time2)
        t50 = time2[numpy.where(abs(sigmoidArr - (sigmoidArr.min() + sigmoidArr.max())/2) < eps2)]
        if isinstance(t50, numpy.ndarray):
            t50 = t50.mean()
            
        if conf[0] == '4':
            host.set_ylim(200,900)
            host.set_xlim(0, 70000)
        else:
            host.set_ylim(200,1600)
            host.set_xlim(0, 90000)
            
        host.scatter(time,mt,s=0.1,marker='x',linewidth=0.5, label="MT")
        host.plot(time2,sigmoidArr,'-r',linewidth=1.5,label="Fitted MT")
        par1.scatter(time, id, s=1,c='g',marker='o',linewidth=0.7, label="ID") 
        msg = "MT50 = %f" % t50
        host.text(0.1 , 0.95, msg, fontsize = 6,\
            horizontalalignment='left', verticalalignment='center',\
            transform = host.transAxes)
        host.vlines(t50,mt.min(),mt.max())
        if conf[0] == '4':
            host.set_ylim(200,900)
            host.set_xlim(0, 70000)
        else:
            host.set_ylim(200,1600)
            host.set_xlim(0, 90000)
        par1.set_ylim(min(int(conf[0]),int(conf[1])),max(int(conf[0]),int(conf[1])))
        host.set_title(figtitle)
        print figname
        fig.savefig(figname)
        pylab.close()
Exemple #5
0
def runner (parser, options, args):
    
    options = Options (options)

    run_method = getattr(parser, 'run_method', 'subcommand')
    if os.name=='posix' and run_method == 'subprocess':
        print 'This script cannot be run using subprocess method. Choose subcommand to continue.'
        return

    if args:
        if len (args)==1:
            if options.input_path:
                print >> sys.stderr, "WARNING: overwriting input path %r with %r" % (options.input_path,  args[0])
            options.input_path = args[0]

    if options.input_path is None:
        parser.error('Expected --input-path but got nothing')

    data, titles = RowFile (options.input_path).read(with_titles = True)
    if options.print_keys:
        print 'rowfile keys:' + ', '.join(data.keys())
        print 'length:', len(data[data.keys()[0]] or [])
        return

    if not options.x_keys and not options.y_keys:
        options.x_keys = titles[0]
        options.y_keys = ','.join(titles[1:])
        parser.save_options(options, [])

    if not options.x_keys or not options.y_keys:
        print 'No x or y keys specified for the plot.'
        return

    x_keys = [str (k).strip() for k in options.x_keys.split (',')]
    y_keys = [str (k).strip() for k in options.y_keys.split (',')]

    from mpl_toolkits.axes_grid.parasite_axes import HostAxes, ParasiteAxes
    import matplotlib.pyplot as plt
    fig = plt.figure(1, figsize=(12,6))
    host = HostAxes(fig, [0.1, 0.1, 0.55, 0.8])
    host.axis["right"].set_visible(False)
    fig.add_axes(host)

    host.set_title(options.input_path)

    legend_list = []
    axes_ylim = []
    plot_axes = []

    for x_key in x_keys:
        x_data = data.get(x_key)
        if x_data is None:
            print 'Rowfile does not contain a column named %r' % (x_key)
            continue
        offset = 20
        host.set_xlabel(x_key)
        for y_key in y_keys:
            plot_str = 'plot'
            if y_key.startswith('log:'):
                y_key = y_key[4:]
                plot_str = 'semilogy'
            y_data = data.get(y_key)
            if y_data is None:
                print 'Rowfile does not contain a column named %r' % (y_key)
                continue
            if axes_ylim:
                ax = ParasiteAxes(host, sharex=host)
                host.parasites.append(ax)
                #ax.axis["right"].set_visible(True)
                #ax.axis["right"].major_ticklabels.set_visible(True)
                #ax.axis["right"].label.set_visible(True)
                new_axisline = ax._grid_helper.new_fixed_axis
                ax_line = ax.axis["right2"] = new_axisline(loc="right",
                                                  axes=ax,
                                                  offset=(offset,0))
                if plot_str=='semilogy':
                    offset += 60
                else:
                    offset += 40
            else:
                ax = host
                ax_line = ax.axis['left']
            ax.set_ylabel(y_key)
            p, = getattr(ax, plot_str)(x_data, y_data, label = y_key)
            axes_ylim.append((ax, min (y_data), max (y_data)))
            plot_axes.append((p, ax_line))
    [ax.set_ylim(mn,mx) for ax,mn,mx in axes_ylim]
    #[ax_line.label.set_color (p.get_color()) for p, ax in plot_axes]
    host.legend()

    def on_keypressed(event):
        key = event.key
        if key=='q':
            sys.exit(0)

    fig.canvas.mpl_connect('key_press_event', on_keypressed)

    plt.draw()

    output_path = options.get (output_path='')
    if output_path:
        plt.savefig (output_path)
        print 'wrote',output_path
        sys.exit(0)
    plt.show()