Ejemplo n.º 1
0
def show_images(imagedata_list, save_as=None, fontsize=12):
    ncols = 3
    if len(imagedata_list) <= 3:
        ncols = len(imagedata_list)
        nrows = 1
    else:
        nrows = int(np.ceil(len(imagedata_list) / 3))

    # f, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 10))
    f, axes = plt.subplots(nrows, ncols, squeeze=False)
    f.tight_layout()
    f.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.)
    # f.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0., hspace=-0.5)
    if nrows == 1:
        plt.setp(axes, xticks=np.arange(0, 1280, 250))

    axes = np.reshape(axes, -1)
    for imagedata, ax in zip(imagedata_list, axes):
        (img, title, cmap) = imagedata
        if cmap is not None:
            ax.imshow(img, cmap=cmap)
        else:
            ax.imshow(img)
        ax.set_title(title, fontsize=fontsize)

    extra_axes_count = len(axes) - len(imagedata_list)
    for i in range(extra_axes_count):
        plt.delaxes(axes[len(imagedata_list) + i])

    if save_as is not None:
        plt.savefig(save_as, bbox_inches='tight', dpi=300)
    plt.show()
Ejemplo n.º 2
0
def plot_cut_in_bin(RE_ranked, RE_data_dict, path_out):
    #Set number of plots
    Num_plots=len(RE_ranked)
    
    #Plot distribution of restriction fragments length
    if Num_plots%4==0:
        fig1, plots=plt.subplots(Num_plots//4,4,figsize=(15,15), dpi=100)
    else:
        fig1, plots=plt.subplots((Num_plots//4)+1,4,figsize=(15,15), dpi=100)
    i=0
    iter_plot=plots.reshape(-1)
    while i<Num_plots:
        RE_name=RE_ranked[i][0]
        catnum=RE_data_dict[RE_name][2]
        iter_plot[i].hist(catnum, edgecolor='black', color='#ff862f', linewidth=1)
        iter_plot[i].set_title(RE_name, size=22)
        iter_plot[i].set_xlabel('Number of cuts', size=18)  
        iter_plot[i].set_ylabel('Number of bins', size=18)
        iter_plot[i].tick_params(axis='both', labelsize=14)
        print('mean '+str(int(np.mean(catnum)))+' cuts\nmedian '+str(int(np.median(catnum)))+' cuts')
        iter_plot[i].annotate('mean '+str(int(np.mean(catnum)))+' cuts\nmedian '+str(int(np.median(catnum)))+' cuts', xytext=(0.4, 0.8), textcoords='axes fraction', xy=(0, 0), size=16.5)
        i+=1
    
    #Remove empty subplots
    for elem in iter_plot[Num_plots:]:
        plt.delaxes(elem)
    
    plt.tight_layout()
    plt.savefig(path_out+"E_coli_re_analysis_cut_in_bin.png", figsize=(15,10), dpi=400)
    plt.close()
    return
Ejemplo n.º 3
0
	def plotMoni(self, ax, number):
		"""
		defines the monitors that should be plotted.
		"""
		if number==1: #plot driver activity
			moni=self.m.totalTreeMoni
			if len(moni)>0:
				ax.plot(moni.tseries(),moni.yseries(), drawstyle='steps-post')
				ax.axis([-0.1, self.now(), -0.1, (max(moni.yseries())+1)])
				ax.grid(True)
				ax.set_xlabel('time(s)')
				ax.set_ylabel('trees picked up')
		elif number==2: #plot trees planted
			moni=self.m.distMoni
			if len(moni)>0:
				ax.plot(moni.tseries(),moni.yseries(), drawstyle='steps-post')
				ax.axis([-0.1, self.now(), -0.1, (max(moni.yseries())+1)])
				ax.grid(True)
				ax.set_xlabel('time(s)')
				ax.set_ylabel('dist. traveled (m)')
		elif number==3:
			#create a new axis..
			plt.delaxes(ax)
			ax=self.p.fig.add_subplot(326, aspect='equal', axisbg='#A2CD5A')
			lim=self.m.pos[0]-15, self.m.pos[0]+15, self.m.pos[1]-15, self.m.pos[1]+15
			self.p.update_world(ax,lim)
		return ax
Ejemplo n.º 4
0
    def generate_sex_pic(self, sex_data):
        """
        生成性别数据图片
        因为plt在子线程中执行会出现自动弹出弹框并阻塞主线程的行为,plt行为均放在主线程中
        :param sex_data:
        :return:
        """
        # 绘制「性别分布」柱状图
        # 'steelblue'
        bar_figure = plt.bar(range(3),
                             sex_data,
                             align='center',
                             color=self.bar_color,
                             alpha=0.8)
        # 添加轴标签
        plt.ylabel(u'Number')
        # 添加标题
        plt.title(u'Male/Female in your Wechat', fontsize=self.title_font_size)
        # 添加刻度标签
        plt.xticks(range(3), [u'Male', u'Female', u'UnKnown'])
        # 设置Y轴的刻度范围
        # 0, male; 1, female; 2, unknown
        max_num = max(sex_data[0], max(sex_data[1], sex_data[2]))
        plt.ylim([0, max_num * 1.1])

        # 为每个条形图添加数值标签
        for x, y in enumerate(sex_data):
            plt.text(x, y + len(str(y)), y, ha='center')

        # 保存图片
        plt.savefig(ALS.result_path + '/2.png')
        # todo 如果不调用此处的关闭,就会导致生成最后一个图像出现折叠、缩小的混乱
        #bar_figure.remove()
        plt.clf()
        plt.delaxes()
Ejemplo n.º 5
0
 def boxplots(self, column, for_test=True, for_target=True):
     n_subplots = 2
     if for_test:
         n_subplots += 1
     if for_target:
         n_subplots += 1
     f, ax = plt.subplots(1,
                          n_subplots,
                          figsize=(4 * (n_subplots + 1), 4),
                          sharey=True)
     train = self._data.loc[self._train, [column]]
     b1 = sb.boxplot(data=train, y=column, orient='v', ax=ax[0])
     ax[0].set(title='train')
     if for_test:
         test = self._data.loc[self._test, [column]]
         b2 = sb.boxplot(data=test, y=column, orient='v', ax=ax[1])
         ax[1].set(title='test')
     if for_target:
         if for_test:
             i = 2
         else:
             i = 1
         train = self._data.loc[self._train, [column]].join(
             self._target.to_frame('TARGET'))
         b3 = sb.boxplot(data=train,
                         y=column,
                         x='TARGET',
                         orient='v',
                         ax=ax[i])
         ax[i].set(title='train')
     plt.delaxes(ax[-1])
     plt.show()
Ejemplo n.º 6
0
 def distplots(self, column, for_test=True, for_target=True):
     if for_target:
         n_subplots = 3
     else:
         n_subplots = 2
     f, ax = plt.subplots(1,
                          n_subplots,
                          figsize=(n_subplots * 6, 4),
                          sharex=True,
                          sharey=True)
     d1 = sb.distplot(self._data.loc[self._train, column],
                      hist=False,
                      label='train',
                      ax=ax[0])
     if for_test:
         d2 = sb.distplot(self._data.loc[self._test, column],
                          hist=False,
                          label='test',
                          ax=ax[0])
     plt.legend()
     if for_target:
         d3 = sb.distplot(
             self._data.loc[self._target[self._target.eq(0)].index, column],
             hist=False,
             label='target=0',
             ax=ax[1])
         d4 = sb.distplot(
             self._data.loc[self._target[self._target.eq(1)].index, column],
             hist=False,
             label='target=1',
             ax=ax[1])
         plt.legend()
     plt.delaxes(ax[-1])
     plt.show()
Ejemplo n.º 7
0
def scatter_plot(data, show=False):

    data = data[:, 1:]  # Get rid of id
    colors = ["red"] * 50 + ["green"] * 50 + ["blue"] * 50  # the data is ordered so this is ok
    fig, axs = plt.subplots(4, 4)  # we have 4 features, so the scatter matrix is 4x4

    # legend
    legend_elements = [Line2D([0], [0], marker='o', color='w', label='Iris-setosa', markerfacecolor='r', markersize=5),
                       Line2D([0], [0], marker='o', color='w', label='Iris-versicolor', markerfacecolor='g', markersize=5),
                       Line2D([0], [0], marker='o', color='w', label='Iris-virginica', markerfacecolor='b', markersize=5),
                      ]
    fig.legend(handles=legend_elements, loc='upper left')

    # title
    plt.suptitle("Scatter Plot Matrix")

    # filling each cell in the matrix
    for i in range(4):
        for j in range(4):
            if i == j:
                plt.delaxes(axs[i, j])
                continue
            ax = axs[i, j]  # get the current axis
            # hide tick and tick label of the big axes
            ax.tick_params(direction="in", labelcolor='none', top=False, bottom=False, left=False, right=False, pad=0.0)
            x = data[:, i].reshape((150, ))  # plot only the relevant feature
            y = data[:, j].reshape((150, ))  # plot only the relevant feature
            ax.scatter(x, y, s=1, color=colors)
            ax.set_xlabel(get_label(i), labelpad=-8, fontsize= 10)  # xlabel
            ax.set_ylabel(get_label(j), labelpad=-5, fontsize=10)  # ylabel
    if show:
        plt.show()
Ejemplo n.º 8
0
def all_params_plotted():
    fig, axs = plt.subplots(4, 2, figsize=(11, 11))
    fig.subplots_adjust(hspace=0.3,
                        top=0.95,
                        bottom=0.05,
                        left=0.1,
                        right=0.95)

    plt.axes(axs[0, 0])
    vary_parameter(500.0, 3000.0, TEMP, show_fig=False)

    #plt.sca(axs[0,1])
    #vary_parameter(4.0,8.0, DENS, show_fig=False)
    plt.delaxes(axs[0, 1])

    plt.axes(axs[1, 0])
    vary_parameter(50, 200, FLUX, show_fig=False)

    plt.axes(axs[1, 1])
    vary_parameter(0.01, 0.1, ATMO, show_fig=False)

    plt.axes(axs[2, 0])
    vary_parameter(0.1, 0.4, EFFI, show_fig=False)

    plt.axes(axs[2, 1])
    vary_parameter(0.1, 10, PRES, show_fig=False)

    plt.axes(axs[3, 0])
    vary_parameter(2500, R_H2, GASC, show_fig=False)

    plt.axes(axs[3, 1])
    vary_parameter(50, 200, TIME, show_fig=False)

    plt.show()
Ejemplo n.º 9
0
def generate_data(choice='linear'):
    global X, Y, N
    if choice == 'linear':
        N = np.random.randint(50, 150)
        std = (np.random.randint(6, 10)) * 0.1
        X, Y = make_blobs(n_samples=N,
                          centers=2,
                          random_state=0,
                          cluster_std=0.60)
    elif choice == 'circle':
        N = np.random.randint(100, 200)
        factor = (np.random.randint(1, 6)) * 0.1
        X, Y = make_circles(n_samples=N, factor=factor, noise=0.1)
    plt.delaxes()
    plt.scatter(X[:, 0],
                X[:, 1],
                c=Y,
                cmap='winter',
                s=100,
                edgecolors='black')
    plt.xlabel("Feature1")
    plt.ylabel("Feature2")
    plt.title("DATA POINTS")
    gendat = BytesIO()
    plt.savefig(gendat, format='png')
    # figfile.seek(0)     # Rewind to the beginning of the file
    gendat_img = base64.b64encode(gendat.getbuffer()).decode('ascii')
    return gendat_img
Ejemplo n.º 10
0
    def reset(self, colors=None):

        if self.ax is not None:
            plt.delaxes(self.ax)
        self.ax = self.fig.add_subplot(111, aspect="equal")
        self.polygons = {}
        for key in self.env.sim.bodies.keys():
            if colors is not None and key in colors.keys():
                self.env.sim.bodies[key].color = colors[key]

            self.polygons[key] = Polygon(
                [[0, 0]],
                ec=self.env.sim.bodies[key].color + [1],
                fc=self.env.sim.bodies[key].color + [1],
                closed=True,
            )

            self.ax.add_artist(self.polygons[key])

        self.ax.set_xlim(self.xlim)
        self.ax.set_ylim(self.ylim)
        if not self.offline:
            self.fig.show()
        else:
            self.ts = 0
Ejemplo n.º 11
0
def visualize(model, filename):
    values = model.all_params()
    cols = OrderedDict()
    for key, value in values.items():
        if isinstance(value, np.ndarray):  # TODO group by prefix, showing W and b side by side
            cols.setdefault(key[:-2] + key[-1] if len(key) > 1 and key[-2] in "bW" else key, []).append((key, value))
    _, axes = plt.subplots(2, len(cols), figsize=(5*len(cols), 10))  # TODO https://stackoverflow.com/a/13784887/223267
    plt.tight_layout()
    for j, col in enumerate(tqdm(cols.values(), unit="param", desc=filename)):
        for i in range(2):
            axis = axes[i, j] if len(values) > 1 else axes
            if len(col) <= i:
                plt.delaxes(axis)
            else:
                plt.sca(axis)
                key, value = col[i]
                plt.colorbar(plt.pcolormesh(smooth(value)))
                for pattern, repl in REPLACEMENTS:  # TODO map 0123->ifoc
                    key = re.sub(pattern, repl, key)
                plt.title(key)
                for axis in (plt.gca().xaxis, plt.gca().yaxis):
                    axis.set_major_locator(MaxNLocator(integer=True))
    output_file = filename + ".png"
    plt.savefig(output_file)
    plt.clf()
    print("Saved '%s'." % output_file)
Ejemplo n.º 12
0
    def separate(self, adjust: np.ndarray, layout: np.ndarray, xlabel: str):
        """
        plt.subplots(nrows=1, ncols=ncols, figsize=(9.1, 2.1), constrained_layout=False)

        :param adjust:
        :param layout:
        :param xlabel:
        :return:
        """

        # The number of curves
        nlines = self.predictions.line.shape[1]

        # Proceed
        fig, handle = plt.subplots(nrows=math.ceil(nlines / 2), ncols=2)
        fig.subplots_adjust(hspace=adjust[0], wspace=adjust[1])
        fig.tight_layout(h_pad=layout[0], w_pad=layout[1])

        for i in range(nlines):
            self.get_separate(handle=handle[i // 2, i % 2],
                              index=i,
                              xlabel=xlabel)

        # Delete the final/empty subplot
        if (nlines // 2) > 0:
            plt.delaxes(handle[nlines // 2, nlines % 2])

        return fig, handle
Ejemplo n.º 13
0
  def __call__(self):
    fig, ax = plt.subplots(3, 3)
    
    

    # map and path
    map_plot = ax[0][0].imshow(self.map_with_path, cmap='Greys', interpolation='nearest')
    map_plot.axes.get_xaxis().set_visible(False)
    map_plot.axes.get_yaxis().set_visible(False)
    ax[0][0].set_title("Map and A* path")

    # closest wall distance
    dists = self.metrics.get("closestDist")
    dist_plot = ax[0][1].imshow(dists, cmap='RdYlGn', interpolation='nearest')
    dist_plot.axes.get_xaxis().set_visible(False)
    dist_plot.axes.get_yaxis().set_visible(False)
    ax[0][1].set_title("Distance to \nclosest obstacle")
    dist_cbar = fig.colorbar(dist_plot, ax=ax[0][1], orientation='horizontal')
    dist_cbar.ax.tick_params(labelsize='xx-small')

    # characteristic dimension
    cdr = self.metrics.get("char_dimension")
    cdr_plot = ax[0][2].imshow(cdr, cmap='binary', interpolation='nearest')
    cdr_plot.axes.get_xaxis().set_visible(False)
    cdr_plot.axes.get_yaxis().set_visible(False)
    ax[0][2].set_title("Char dimension")
    cdr_cbar = fig.colorbar(cdr_plot, ax=ax[0][2], orientation='horizontal')
    cdr_cbar.ax.tick_params(labelsize='xx-small')

    # average visibility
    avgVis = self.metrics.get("avgVis")
    avgVis_plot = ax[1][0].imshow(avgVis, cmap='RdYlGn', interpolation='nearest')
    avgVis_plot.axes.get_xaxis().set_visible(False)
    avgVis_plot.axes.get_yaxis().set_visible(False)
    ax[1][0].set_title("Average visibility")
    avgVis_cbar = fig.colorbar(avgVis_plot, ax=ax[1][0], orientation='horizontal')
    avgVis_cbar.ax.tick_params(labelsize='xx-small')
    
    # dispersion
    dispersion = self.metrics.get("dispersion")
    disp_plot = ax[1][1].imshow(dispersion, cmap='RdYlGn', interpolation='nearest')
    disp_plot.axes.get_xaxis().set_visible(False)
    disp_plot.axes.get_yaxis().set_visible(False)
    ax[1][1].set_title("%d-square radius dispersion" % self.dispersion_radius)
    disp_cbar = fig.colorbar(disp_plot, ax=ax[1][1], orientation='horizontal')
    disp_cbar.ax.tick_params(labelsize='xx-small')

    # jackal's navigable map, low-res
    jmap_plot = ax[2][0].imshow(self.jackal_map_with_path, cmap='Greys', interpolation='nearest')
    jmap_plot.axes.get_xaxis().set_visible(False)
    jmap_plot.axes.get_yaxis().set_visible(False)
    ax[2][0].set_title("Jackal navigable map")

    plt.delaxes(ax[1][2])
    plt.axis('off')
    plt.show()
Ejemplo n.º 14
0
def smaller_axes(ax1):
    # Replace existing axes with one only 90% width and 75% height, 
    # allowing more room for ticks and titles
    ax_pos = ax1.get_position().get_points()
    ax_width = ax_pos[1,0] - ax_pos[0,0]
    ax_height = ax_pos[1,1] - ax_pos[0,1]
    pp.delaxes(ax1)
    ax2 = pp.axes([ax_pos[0,0] + .10 * ax_width, ax_pos[0,1] + .20 * ax_height, 
                  .90 * ax_width, .75 * ax_height])  
    return ax2
Ejemplo n.º 15
0
    def remove(self, name):
        for i in range(len(self.axes)):
            if (name == self.functions_name[i]):
                print("found number 3")
                del self.functions_name[i]
                del self.functions[i]

                pyplot.delaxes(self.axes[i])
                self.axes = self.axes[self.axes != self.axes[i]]

                self.draw()
def plot_form_factors(ge2_vals, gm2_vals, q2, save_path=None):

	""" Plots the distributions of proton form factors for a given Q^2 side-by-side, based on Monte Carlo simulations

	Args:
		ge2_vals			(list) - The values of the electric form factor squared
		gm2_vals 			(list) - The values of the magnetic form factor squared

	Returns:
		(tuple) - 
			(float) - The uncertainty/standard deviation of the electric form factor measurements
			(float) - The uncertainty/standard deviation of the magnetic form factor measurements

	"""

	plt.delaxes()
	plt.figure(figsize=(24,10))
	ax1 = plt.subplot(121)
	mu1, sigma1 = stats.norm.fit(ge2_vals)

	n, bins, patches = plt.hist(ge2_vals, bins=50, normed=1, facecolor='red', linewidth=3, histtype="stepfilled")
	y = mlab.normpdf(bins, mu1, sigma1)
	plt.plot(bins, y, "-", color="black", linewidth=4)
	plt.axvspan(mu1-sigma1, mu1+sigma1, color="white", alpha=0.5)
	plt.xticks(fontsize=20, rotation=30)
	plt.yticks(fontsize=20)

	ax2 = plt.subplot(122)
	mu2, sigma2 = stats.norm.fit(gm2_vals)
	n, bins, patches = plt.hist(gm2_vals, bins=50, normed=1, facecolor='blue', linewidth=3, histtype="stepfilled")
	y = mlab.normpdf(bins, mu2, sigma2)
	plt.plot(bins, y, "-", color="black", linewidth=4)
	plt.axvspan(mu2-sigma2, mu2+sigma2, color="white", alpha=0.5)
	plt.xticks(fontsize=20, rotation=30)
	plt.yticks(fontsize=20)

	ax1.set_xlabel(r'$G_E^2$', fontsize=30, labelpad=20)
	ax1.set_ylabel(r'Frequency', fontsize=30, labelpad=20)
	ax1.annotate('$Q^2 = %.3f$ \n $\mu = %f$ \n $\sigma = %f$' % (q2, mu1, sigma1), xy=(mu1, 0), xycoords='data',
		xytext=(0.6, 0.7), textcoords="axes fraction", verticalalignment='bottom', horizontalalignment='left', fontsize=30)
	ax1.text(0.5, 1.05, 'a', transform=ax1.transAxes, 
            size=40, weight='bold')
	ax2.set_xlabel(r'$G_M^2$', fontsize=30, labelpad=20)
	ax2.set_ylabel(r'Frequency', fontsize=30, labelpad=20)
	ax2.annotate('$Q^2 = %.3f$ \n $\mu = %f$ \n $\sigma = %f$' % (q2, mu2, sigma2), xy=(mu2, 0), xycoords='data',
		xytext=(0.65, 0.7), textcoords="axes fraction", verticalalignment='bottom', horizontalalignment='left', fontsize=30)
	ax2.text(0.5, 1.05, 'b', transform=ax2.transAxes, 
            size=40, weight='bold')
	plt.subplots_adjust(bottom=0.15)
	if not save_path:
		plt.show()
	else:
		plt.savefig(save_path+"/ff_q2_"+str(q2)+".png", bbox_inches="tight")
	return sigma1, sigma2
Ejemplo n.º 17
0
def tetrahedron_array_st():
    """ 
    Make figure of tetrahedra along 
    different combinations of singular vectors 
    colored by scottrade classification 
    """

    # Load Fits
    filename = 'saves/8_factor_matrices.pkl.gz'
    E, W, C, SSE, varexpl = local_load(filename)

    # Calculate Singular Vectors
    A = np.dot(E, W)
    u, s, vt = svd(A.T, full_matrices=0)
    vtE = np.asarray(np.dot(vt, E))

    # Define colors and labels
    cols = [
        '#FFF000', '#FDBF6F', '#A6CEE3', '#33A02C', '#FB9A99', '#B2DF8A',
        '#E31A1C', '#6A3D9A', '#FF7F00', '#543005', '#CAB2D6', '#000000',
        '#1F78B4', '#8C510A'
    ]
    numcos = [58, 61, 41, 42, 107, 53, 40, 93, 6, 57, 55, 31, 46, 15]

    sulis = [[v] * k for k, v in zip(numcos, cols)]
    csuarray = np.array([item for sublist in sulis for item in sublist])

    # Plot figure
    fig, axs = plt.subplots(8, 8, figsize=(20, 20))

    for ((j, i), ax) in np.ndenumerate(axs):
        if i < j:
            xs, ys = u[:, i].T * s[i], u[:, j].T * s[j]
            ax.scatter(xs, ys, s=5, color=csuarray)
            ax.scatter(vtE[i, :], vtE[j, :], s=10, color='k')
            ax.set_axis_off()

            xy = zip(vtE[i, :], vtE[j, :])
            for p in itertools.combinations(xy, 2):
                ax.plot([p[0][0], p[1][0]], [p[0][1], p[1][1]],
                        ls='--',
                        color='#D9D9D9')

        else:
            plt.delaxes(ax)

    plt.subplots_adjust(hspace=0.05, wspace=0.05)
    plt.savefig('figures/tetrahedron_array_st.png',
                bbox_inches='tight',
                pad_inches=0)
    plt.close()

    return
Ejemplo n.º 18
0
    def reorg_fig(self, nplots):
        """Reorganise the views to show nplots"""
        if CmdBase.mode == CmdMode.GUI:
            self.parent_application.sp_nviews.blockSignals(True)
            self.parent_application.sp_nviews.setValue(nplots)
            self.parent_application.sp_nviews.blockSignals(False)

        self.plotselecttabWidget.blockSignals(True)
        nplot_old = self.nplots
        self.nplots = nplots
        gs = self.organizeplots(self.pot, self.nplots, self.ncols)

        # hide tabs if only one figure
        self.plotselecttabWidget.setVisible(nplots > 1)

        for i in range(nplots):
            self.axarr[i].set_position(gs[i].get_position(self.figure))
            self.axarr[i].set_subplotspec(gs[i])
        for tab in self.hidden_tab:
            tab_id = tab[0]
            widget = tab[1]
            tab_text = tab[2]
            self.plotselecttabWidget.insertTab(tab_id, widget, tab_text)
        self.hidden_tab = []

        for i in range(nplots, len(self.axarr)):
            self.axarr[i].set_visible(False)
            tab_id = i + 1
            widget = self.plotselecttabWidget.widget(nplots + 1)
            tab_text = self.plotselecttabWidget.tabText(nplots + 1)
            self.hidden_tab.append([tab_id, widget, tab_text])
            self.plotselecttabWidget.removeTab(nplots + 1)

        self.set_bbox()
        for i in range(self.nplots):
            self.axarr[i].set_position(self.bbox[i])
        # add new axes to plt
        for i in range(nplot_old, self.nplots):
            try:
                plt.subplot(self.axarr[i])
            except:
                pass
        # remove axes from plt
        for i in range(self.nplots, nplot_old):
            try:
                plt.delaxes(self.axarr[i])
            except:
                pass
        for ds in self.parent_application.datasets.values():
            ds.nplots = nplots
        self.parent_application.update_all_ds_plots()
        self.handle_plottabChanged(0) # switch to all plot tab
        self.plotselecttabWidget.blockSignals(False)
Ejemplo n.º 19
0
    def barplots(self,
                 column,
                 for_test=True,
                 for_target=True,
                 nan_as_cat=True):
        if for_test:
            n_subplots = 3
        else:
            n_subplots = 2
        f, ax = plt.subplots(1, n_subplots, figsize=(n_subplots * 6, 4))
        data = self._data.loc[self._train, column].value_counts(
            normalize=True, dropna=nan_as_cat).sort_index()
        pos = np.arange(data.shape[0])
        if for_target:
            dt = []
            for t in [0, 1]:
                idx = self._target[self._target.eq(t)].index
                sh = self._data.loc[self._train, column].shape[0]
                temp = self._data.loc[idx, column].value_counts(
                    dropna=nan_as_cat) / sh
                dt.append(
                    pd.merge(data.to_frame('total'),
                             temp.to_frame(str(t)),
                             left_index=True,
                             right_index=True,
                             how='left')[str(t)].fillna(0))

            b1 = ax[0].bar(pos, dt[0].values, alpha=0.5)
            b2 = ax[0].bar(pos, dt[1].values, bottom=dt[0].values, alpha=0.5)
            ax[0].legend([b1, b2], ['target=0', 'target=1'])
        else:
            b1 = ax[0].bar(pos, data.values, alpha=0.5)
        ax[0].set(title=column + ' train')
        ax[0].set_xticks(pos)
        ax[0].set_xticklabels(list(data.index), rotation=90)
        if for_test:
            data = pd.merge(data.to_frame('train'),
                            self._data.loc[self._test, column].value_counts(
                                normalize=True,
                                dropna=nan_as_cat).to_frame('test'),
                            left_index=True,
                            right_index=True,
                            how='outer').fillna(0).sort_index(axis=0)
            pos = np.arange(data.shape[0])
            b1 = ax[1].bar(pos, data.train.values, alpha=0.5)
            b2 = ax[1].bar(pos, data.test.values, alpha=0.5)
            ax[1].set(title=column + ' test')
            ax[1].set_xticks(pos)
            ax[1].set_xticklabels(list(data.index), rotation=90)
            ax[1].legend([b1, b2], ['train', 'test'])
        plt.delaxes(ax[-1])
        plt.show()
Ejemplo n.º 20
0
def main(infile="waves.out", outfile="out.mp4", startpic="start.png"):
    """Visualize shallow water simulation results.

    Args:
        infile: Name of input file generated by simulator
        outfile: Desired output file (mp4 or gif)
        startpic: Name of picture generated at first frame
    """

    u = np.fromfile(infile, dtype=np.dtype('f4'))
    nx = int(u[0])
    ny = int(u[1])
    x = range(0, nx)
    y = range(0, ny)
    u = u[2:]
    nframe = len(u) // (nx * ny)
    stride = nx // 20
    u = np.reshape(u, (nframe, nx, ny))
    X, Y = np.meshgrid(x, y)

    fig = plt.figure(figsize=(10, 10))

    #print("In Plotting: nx = %d, ny = %d" % (nx,ny))
    #print(u);
    def plot_frame(i, stride=5):
        ax = fig.add_subplot(111, projection='3d')
        ax.set_zlim(0, 2)
        Z = u[i, :, :]
        ax.plot_surface(X, Y, Z, rstride=stride, cstride=stride)
        return ax

    if startpic:
        ax = plot_frame(0)
        plt.savefig(startpic)
        plt.delaxes(ax)

    metadata = dict(title='Wave animation', artist='Matplotlib')
    if outfile[-4:] == ".mp4":
        Writer = manimation.writers['ffmpeg']
        writer = Writer(
            fps=15,
            metadata=metadata,
            extra_args=["-r", "30", "-c:v", "libx264", "-pix_fmt", "yuv420p"])
    elif outfile[-4:] == ".gif":
        Writer = manimation.writers['imagemagick']
        writer = Writer(fps=15, metadata=metadata)

    with writer.saving(fig, outfile, nframe):
        for i in range(nframe):
            ax = plot_frame(i)
            writer.grab_frame()
            plt.delaxes(ax)
Ejemplo n.º 21
0
def figPause(fig):
    global gInPause
    buttonAxes = plt.axes([0.9, 0.9, 0.1, 0.05])
    button = Button(buttonAxes, '>>')
    button.on_clicked(onclick)
    gInPause = True
    print('Program paused in figure window...')
    while gInPause:
        plt.pause(0.1)
        plt.draw()
    button.disconnect_events()
    plt.delaxes(buttonAxes)
    plt.draw()
Ejemplo n.º 22
0
def figPause(fig):
    global gInPause
    buttonAxes = plt.axes([0.9, 0.9, 0.1, 0.05])
    button = Button(buttonAxes, '>>')
    button.on_clicked(onclick)
    gInPause = True
    print('Program paused in figure window...')
    while gInPause:
        plt.pause(0.0001)
        plt.draw()
    button.disconnect_events()
    plt.delaxes(buttonAxes)
    plt.draw()
Ejemplo n.º 23
0
def mk_2d_sequence_gif(x_seq, y_seq, filename='make_2d_sequence_gif.gif', plot_kwargs={}, edit_funs={}, writeGif_kwargs={}, savefig_kwargs={}, **kwargs):
    # handling defaults
    plot_kwargs = dict({'color':'b', 'marker':'o', 'linestyle':'-', 'linewidth':0.2}, **plot_kwargs)
    savefig_kwargs = dict({'dpi':200}, **savefig_kwargs)

    writeGif_kwargs = dict({'size':(600,600), 'duration':0.2}, **writeGif_kwargs)

    # handling edit_funs
    if hasattr(edit_funs, '__call__'):
        edit_fun = edit_funs
    else:
    # defining an edit function from the edit_funs dict
        def edit_fun(): # this function assumes keys of edit_funs are plt attributes, and tries to call the values on them
            for k, v in edit_funs.iteritems():
                if hasattr(plt, edit_funs[k]):
                    f = plt.__getattribute__(k)
                    try:
                        f(**v)
                    except:
                        try:
                            f(*v)
                        except:
                            f(v)
                else:
                    v()

    tmp_dir = tempfile.mkdtemp('mk_2d_sequence_gif')

    # get the axis limits
    if 'xylims' in kwargs.keys():
        ax = kwargs['xylims']
    else:
        # plot the full sequences to get the axis limits from it
        plt.plot(x_seq, y_seq, **plot_kwargs)
        edit_fun()
        ax = plt.axis()
    # create files that will be absorbed by the gif
    for i in range(1,len(x_seq)+1):
        plt.delaxes()
        plt.plot(x_seq[:i], y_seq[:i], **plot_kwargs)
        plt.axis(ax)
        edit_fun()
        plt.savefig(os.path.join(tmp_dir, 'mk_2d_sequence_gif%02.0f'%i), **savefig_kwargs)
    # create the gif
    size = writeGif_kwargs['size']
    del writeGif_kwargs['size']
    file_names = sorted(fnmatch.filter(os.listdir(tmp_dir), 'mk_2d_sequence_gif*'))
    images = [Image.open(os.path.join(tmp_dir,fn)) for fn in file_names]
    for im in images:
        im.thumbnail(size, Image.ANTIALIAS)
    writeGif(filename=filename, images=images, **writeGif_kwargs)
Ejemplo n.º 24
0
def create_figure(n,plot=False,figsize=False,vertical=False):
    if n==1:
        if not plot or plot=='pie':
            fig, (ax1) = plt.subplots(1,1,figsize=(8,8))
            ax=[ax1]   
        elif plot=='line' or plot=='stacked':
            fig, (ax1) = plt.subplots(1,1,figsize=(10,4))
            ax=[ax1]
    elif n==2:
        if vertical:
            if figsize:
                fig, (ax1,ax2) = plt.subplots(2,1,figsize=figsize)
                ax=[ax1,ax2]
            else:
                fig, (ax1,ax2) = plt.subplots(2,1,figsize=(5,10))
                ax=[ax1,ax2]
        else:
            fig, (ax1,ax2) = plt.subplots(1,2,figsize=(10,5))
            ax=[ax1,ax2]
    elif n==3:
        if figsize:
            if vertical:
                fig, (ax1,ax2,ax3) = plt.subplots(3,1,figsize=figsize)
            else:
                fig, (ax1,ax2,ax3) = plt.subplots(1,3,figsize=figsize)
        else:
            fig, (ax1,ax2,ax3) = plt.subplots(1,3,figsize=(9,3,))
        ax=[ax1,ax2,ax3]
    else:
        if vertical==True:
            nrows=n
            ncols=1
        else:
            nrows,ncols=plot_layout(n)
        if figsize:
            fx,fy=figsize
        else:
            fx,fy=get_figsize(nrows,ncols)
        fig, axes = plt.subplots(nrows,ncols,figsize=(fx,fy))
        ax=[]
        for a in axes:
            if vertical==True:
                ax.append(a)
            else:
                for b in a:
                    ax.append(b)
        if len(ax) != n:
            plt.delaxes(ax[-1])

    return fig, ax
Ejemplo n.º 25
0
def main(infile="dam_break.out", outfile="out-local.mp4", startpic="start.png"):
    """Visualize shallow water simulation results.

    Args:
        infile: Name of input file generated by simulator
        outfile: Desired output file (mp4 or gif)
        startpic: Name of picture generated at first frame
    """

    u = np.fromfile(infile, dtype=np.dtype('f4'))
    nx = int(u[0])
    ny = int(u[1])
    x = range(0,nx)
    y = range(0,ny)
    u = u[2:]
    nframe = len(u) // (nx*ny)
    stride = nx // 20
    u = np.reshape(u, (nframe,nx,ny))
    X, Y = np.meshgrid(x,y)

    fig = plt.figure(figsize=(10,10))

    def plot_frame(i, stride=5):
        ax = fig.add_subplot(111, projection='3d')
        ax.set_zlim(0, 2)
        Z = u[i,:,:];
        ax.plot_surface(X, Y, Z, rstride=stride, cstride=stride)
        return ax

    if startpic:
        ax = plot_frame(0)
        plt.savefig(startpic)
        plt.delaxes(ax)

    metadata = dict(title='Wave animation', artist='Matplotlib')
    if outfile[-4:] == ".mp4":
        Writer = manimation.writers['ffmpeg']
        writer = Writer(fps=15, metadata=metadata,
                        extra_args=["-r", "30",
                                    "-c:v", "libx264",
                                    "-pix_fmt", "yuv420p"])
    elif outfile[-4:] == ".gif":
        Writer = manimation.writers['imagemagick']
        writer = Writer(fps=15, metadata=metadata)

    with writer.saving(fig, outfile, nframe):
        for i in range(nframe):
            ax = plot_frame(i)
            writer.grab_frame()
            plt.delaxes(ax)
Ejemplo n.º 26
0
def tetrahedron_array():
    """ 
    Make figure of tetrahedra along 
    different combinations of singular vectors 
    colored by sector association 
    """

    # Load Fits
    filename = 'saves/8_factor_matrices.pkl.gz'
    E, W, C, SSE, varexpl = local_load(filename)

    # Calculate Singular Vectors
    A = np.dot(E, W)
    u, s, vt = svd(A.T, full_matrices=0)
    vtE = np.asarray(np.dot(vt, E))

    # Define colors and assign to data points
    csu = np.array([
        '#984EA3', '#E41A1C', '#A65628', '#F781BF', '#4DAF4A', '#377EB8',
        '#FF7F00', '#FFF000'
    ])
    csuarray = csu[np.argmax(W, 0)]

    # Plot
    fig, axs = plt.subplots(8, 8, figsize=(20, 20))

    for ((j, i), ax) in np.ndenumerate(axs):
        if i < j:
            xs, ys = u[:, i].T * s[i], u[:, j].T * s[j]
            ax.scatter(xs, ys, s=5, color=csuarray)
            ax.scatter(vtE[i, :], vtE[j, :], s=10, color='k')
            ax.set_axis_off()

            xy = zip(vtE[i, :], vtE[j, :])
            for p in itertools.combinations(xy, 2):
                ax.plot([p[0][0], p[1][0]], [p[0][1], p[1][1]],
                        ls='--',
                        color='#D9D9D9')

        else:
            plt.delaxes(ax)

    plt.subplots_adjust(hspace=0.05, wspace=0.05)
    plt.savefig('figures/tetrahedron_array.png',
                bbox_inches='tight',
                pad_inches=0)
    plt.close()

    return
Ejemplo n.º 27
0
def histogram(data):
    fig, axs = plt.subplots(3, 5, figsize=(24, 12))
    i = 0
    j = 0
    bestStd = None
    for row in data:
        if all(isinstance(x, float) for x in data[row]):
            stdScore = std(data[row].dropna())
            if bestStd == None or bestStd > stdScore:
                bestStd = stdScore
                bestRow = row
            Gryffindor = getHouse(data, row, 'Gryffindor')
            Slytherin = getHouse(data, row, 'Slytherin')
            Ravenclaw = getHouse(data, row, 'Ravenclaw')
            Hufflepuff = getHouse(data, row, 'Hufflepuff')
            if len(Gryffindor) > 0 and len(Slytherin) > 0 and len(
                    Ravenclaw) > 0 and len(Hufflepuff) > 0:
                axs[i][j].set_title(str(row))
                axs[i][j].hist(np.hstack(Gryffindor),
                               histtype='bar',
                               alpha=0.3,
                               label='Gryffindor')
                axs[i][j].hist(np.hstack(Slytherin),
                               histtype='bar',
                               alpha=0.3,
                               label='Slytherin')
                axs[i][j].hist(np.hstack(Ravenclaw),
                               histtype='bar',
                               alpha=0.3,
                               label='Ravenclaw')
                axs[i][j].hist(np.hstack(Hufflepuff),
                               histtype='bar',
                               alpha=0.3,
                               label='Hufflepuff')
                axs[i][j].legend(prop={'size': 6})
                j += 1
                if j > 4:
                    i += 1
                    j = 0
    if i < 3 and j < 4:
        plt.delaxes(axs[2][3])
        plt.delaxes(axs[2][4])
    print(
        str(bestRow) +
        " has the most homogeneous distribution of scores between the four houses"
    )
    plt.show()
def generateTaxSubplots(search_rank):
    for ax_ind1, ax_ind2, bcs, name in zip(subplot_coordinates_list_columns, subplot_coordinates_list_rows, ncbiblast_barcodes, name_list):
        applyTaxPlotStyle(ax_ind1, ax_ind2, bcs, search_rank, name)    

    if len(list(ntblasthit_reads_filtered_barcodes['barcode_arrangement'].unique())) != 0:
        plt.delaxes(ax[subplot_coordinates_list_columns[-1], subplot_coordinates_list_rows[-1]])
    
    plt.suptitle('Reads Hitting NCBI Database - % By ' + str(search_rank).title() + ' By Sample', 
             fontsize='x-large',
             y=1.02,
             fontweight="bold")
    
    plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=1.4, hspace=0.4)
    plt.tight_layout()
    
    plt.savefig('/home/yiheng/analysis/WGS/figures/' + 'figure3_%s_%s.png' % (column_name, search_rank), bbox_inches='tight')
    plt.show()
Ejemplo n.º 29
0
 def pieplots(self,
              column,
              for_test=True,
              for_target=True,
              nan_as_cat=True):
     n_subplots = 2
     subtitles = [column + '_train']
     data = [
         self._data.loc[self._train,
                        column].value_counts(normalize=True,
                                             dropna=nan_as_cat).values
     ]
     labels = [
         self._data.loc[self._train,
                        column].value_counts(normalize=True,
                                             dropna=nan_as_cat).index
     ]
     if for_test:
         n_subplots += 1
         subtitles.append(column + '_test')
         data.append(self._data.loc[self._test, column].value_counts(
             normalize=True, dropna=nan_as_cat).values)
         labels.append(self._data.loc[self._test, column].value_counts(
             normalize=True, dropna=nan_as_cat).index)
     if for_target:
         for t in [0, 1]:
             n_subplots += 1
             subtitles.append(column + ' target=' + str(t))
             idx = self._target[self._target.eq(t)].index
             data.append(self._data.loc[idx, column].value_counts(
                 normalize=True, dropna=nan_as_cat).values)
             labels.append(self._data.loc[idx, column].value_counts(
                 normalize=True, dropna=nan_as_cat).index)
     f, ax = plt.subplots(
         1,
         n_subplots,
         figsize=(n_subplots * 4 + sum([for_test, for_target, for_target]),
                  4))
     for i in range(n_subplots - 1):
         ax[i].pie(x=data[i],
                   labels=labels[i],
                   explode=[0.1] * len(labels[i]),
                   autopct='%1.2f%%')
         ax[i].set(title=subtitles[i])
     plt.delaxes(ax[-1])
     plt.show()
Ejemplo n.º 30
0
def plot_1dscans(filenames,
                 plot_file=None,
                 cols=3,
                 width=14,
                 l_col='log_likelihood'):
    """ plots the scan for all filenames as a grid """
    rows = np.ceil(len(filenames) / cols).astype(int)
    _, axes = plt.subplots(rows,
                           cols,
                           figsize=(width, 0.7 * width / cols * rows))
    # fig = plt.figure()
    for i, ax in enumerate(axes.ravel()):
        if i < len(filenames):
            scan, parameter = read_1dscan(filenames[i], l_col)

            param_range = scan.to_numpy()[:, 0]
            ll = scan.to_numpy()[:, 1]

            ax.plot(param_range, ll, label='scan')

            params_config = read_params_config(filenames[i])
            init = get_params_config(params_config,
                                     parameter)["init"].values[0]
            init_idx = np.searchsorted(param_range, init)
            ax.axvline(x=init, label='init', ls='--', color='tab:orange')
            ax.axhline(y=ll[init_idx], ls='--', color='tab:orange')

            x_scan_max, y_scan_max = param_range[np.nanargmax(ll)], np.nanmax(
                ll)

            ax.axvline(x=x_scan_max,
                       label='ll max',
                       ls='--',
                       color='tab:green')
            ax.axhline(y=y_scan_max, ls='--', color='tab:green')

            ax.set_xlabel(parameter)
            ax.set_ylabel('log likelihood')
            ax.ticklabel_format(style='sci', scilimits=(0, 1), useOffset=False)
            ax.legend()
        else:
            plt.delaxes(ax)
    plt.tight_layout()
    if plot_file != None:
        plt.savefig(plot_file + '_1dscans.pdf')
    plt.show()
Ejemplo n.º 31
0
    def handle_plottabChanged(self, index):
        self.parent_application.current_viewtab = index
        if index == 0:  # multiplots
            view_name = self.parent_application.multiviews[0].name
            if CmdBase.mode == CmdMode.GUI:
                ind = self.parent_application.viewComboBox.findText(
                    view_name, Qt.MatchExactly)
                self.parent_application.viewComboBox.blockSignals(True)
                self.parent_application.viewComboBox.setCurrentIndex(
                    ind)  # set the view combobox according to current view
                self.parent_application.viewComboBox.blockSignals(False)
            for i in range(self.nplots):
                self.axarr[i].set_position(self.bbox[i])
                self.axarr[i].set_visible(True)
                try:
                    plt.subplot(self.axarr[i])
                except:
                    pass

        else:  # single plot max-size
            tab_to_maxi = index - 1  # in 0 1 2
            view_name = self.parent_application.multiviews[tab_to_maxi].name
            if CmdBase.mode == CmdMode.GUI:
                ind = self.parent_application.viewComboBox.findText(view_name)
                self.parent_application.viewComboBox.blockSignals(True)
                self.parent_application.viewComboBox.setCurrentIndex(
                    ind)  # set the view combobox according to current view
                self.parent_application.viewComboBox.blockSignals(False)
            for i in range(self.nplots):
                if i == tab_to_maxi:  # hide other plots
                    self.axarr[i].set_visible(True)
                    self.axarr[i].set_position(self.bboxmax)
                    try:
                        plt.subplot(self.axarr[i])
                    except:
                        pass
                else:
                    self.axarr[i].set_visible(False)
                    try:
                        plt.delaxes(self.axarr[i])
                    except:
                        pass
        self.parent_application.update_datacursor_artists()
        self.canvas.draw()
        self.parent_application.set_view_tools(view_name)
Ejemplo n.º 32
0
def plot_noisy_param_run(filenames, params_config, skip=0, cols=3, width=14):
    """ needs list of final files """
    no_params = len(params_config)
    rows = np.ceil(no_params / cols).astype(int)
    _, axes = plt.subplots(rows,
                           cols,
                           figsize=(width, 0.7 * width / cols * rows))
    ax = axes.ravel()
    for i, k in enumerate(params_config.items()):
        n = 0
        for minimization_final_file in filenames:
            if os.path.isfile(minimization_final_file):
                parameters_settings = read_params_config(
                    minimization_final_file)
                final = get_params_config(parameters_settings,
                                          k[0])["final"].values[0]
                init = get_params_config(parameters_settings,
                                         k[0])["init"].values[0]

                err_bar = float(
                    pd.read_csv(minimization_final_file,
                                skiprows=14)[k[0]].iloc[2])
                ax[i].errorbar(n,
                               final,
                               yerr=err_bar,
                               color='tab:blue',
                               fmt='o',
                               ms=3,
                               label="deviation prediction")

                # ax[i].scatter(n, final, color="tab:blue")
                ax[i].scatter(n, init, color="tab:orange")
            else:
                print(minimization_final_file, "not found!")
            n += 1

        # ax[i].ticklabel_format(style='sci', scilimits=(0,1), useOffset=False)
        ax[i].set_ylabel(k[0])
        ax[i].set_xlabel("run")

    for i in range(len(params_config), len(ax)):
        plt.delaxes(ax[i])
    plt.tight_layout()

    plt.show()
Ejemplo n.º 33
0
def plotHistWithoutOutliers(df,
                            fig=(12, 8),
                            thresh=0.25,
                            imputestrategy="median",
                            outliertreat="remove"):
    """this function does not change the dataframe permanently"""
    df = df.select_dtypes("number")

    col = 4
    row = int(len(df.columns) / col) + 1
    _, axes = plt.subplots(row, col, figsize=fig)
    delete = row * col - len(df.columns)
    for d in range(delete):
        plt.delaxes(axes[row - 1, col - d - 1])

    plt.suptitle("Histograms without outliers")
    r = 0
    c = 0
    fc = 0
    for f in sorted(df.columns):
        Q1 = df[f].fillna(df[f].agg(imputestrategy)).quantile(thresh)
        Q3 = df[f].fillna(df[f].agg(imputestrategy)).quantile(1 - thresh)
        IQR = Q3 - Q1
        t1 = (Q3 + 1.5 * IQR)
        t2 = (Q1 - 1.5 * IQR)
        cond = ((df[f] > t1) | (df[f] < t2))

        r = int(fc / 4)
        c = fc % 4
        if outliertreat == "remove":
            df[~cond][f].hist(ax=axes[r, c])
        elif outliertreat == "cap":
            s = df[f].copy()
            s.where(s > t2, t2, inplace=True)
            s.where(s < t1, t1, inplace=True)
            s.hist(ax=axes[r, c])
        else:
            print("wrong value for outliertreat")
            raise
        #axes[r,c].set_xticklabels(axes[r,c].get_xticklabels(), rotation=r,ha='right')
        axes[r, c].set_title(f)
        plt.tight_layout(rect=[0, 0.03, 1, 0.95])
        fc = fc + 1
Ejemplo n.º 34
0
    def show(self, outfn='1dexample'):
        # Compute stats about the solution.
        self.hist()

        fig, axes = plt.subplots(2,
                                 2,
                                 gridspec_kw={
                                     'width_ratios': [1, 3],
                                     'height_ratios': [1, 3]
                                 })
        plt.delaxes(axes[0, 0])

        # Objective function.
        dom = np.linspace(0, 1, 1000)
        axes[1, 1].plot(dom, self.energy(dom), pt.pc['db'], lw=0.5)

        # Statistics of solution.
        axes[0, 1].hist(self.states, bins=np.linspace(0, 1, 30))
        axes[0, 1].set_xlim(axes[1, 1].get_xlim())
        axes[0, 1].set_yticks(pt.thin_ticks(axes[0, 1].get_yticks()))

        axes[1, 0].hist(self.ens, orientation='horizontal')
        print(axes[1, 0].get_xlim(), axes[1, 0].get_ylim())
        #axes[1,0].set_yticks(np.linspace(0,1,5))
        #axes[1,1].set_yticks(np.linspace(0,1,5))
        #axes[1,0].set_xticks(np.linspace(0,100,3,dtype=int))
        axes[1, 0].set_xticks(pt.thin_ticks(axes[1, 0].get_xticks()))

        pt.fix_lims(axes[1, 1])
        axes[0, 1].set_xlim(axes[1, 1].get_xlim())
        axes[1, 0].set_ylim(axes[1, 1].get_ylim())
        fig.set_size_inches(4, 3)

        # Labeling.
        axes[1, 1].set_xlabel('Domain')
        axes[0, 1].set_ylabel('Histogram')
        axes[1, 0].set_xlabel('Histogram')
        axes[1, 0].set_ylabel('Objective')

        fig.tight_layout()
        sns.despine()
        fig.savefig(outfn + '.pdf')
        fig.savefig(outfn + '.eps')
def basemap_temperature(data_comp,met):
    lat_ts=90.0 ; lat_0=90.0 ; lon_0=-45.0 ;
    sgn=1
    width=7000000. ; height=7000000.0 ;
    vmin = 32 ; vmax = 36 ;
    ind = 0
    
    if met == 5:
        mm = 0
    elif met == 200:
        mm = 16
    elif met == 400:
        mm = 21
    else:
        print "error: wrong depth level"
    
    fig, axes = plt.subplots(2,3)
    for data in data_comp:
        m = Basemap(ax=axes.flat[ind],width=width,height=height,resolution='h',\
                    projection='stere',lat_ts=lat_ts,lat_0=lat_0,lon_0=lon_0)
        # the continents will be drawn on top.
        #m.drawmapboundary(fill_color='white')
        # fill continents, set lake color same as ocean color.
        #m.fillcontinents(color='grey',lake_color='navy')
        m.drawlsmask(land_color='grey',ocean_color='white',lakes=True)

        x,y=m(data_comp[data].lon,data_comp[data].lat)
        datam = data_comp[data].S[mm,:,:]
        sitm = np.ma.masked_where(np.isnan(datam),datam)
        CS=m.pcolormesh(x,y,sitm,cmap=cmocean.cm.salt,vmin=vmin,vmax=vmax)
        m.drawparallels(np.arange(-80.,81.,15.))
        m.drawmeridians(np.arange(-180.,181.,30.))
        #m.drawcoastlines()
        axes.flat[ind].set_title('Sal at '+str(met)+'m - '+data_comp[data].title)
        ind += 1
    cbar_ax = fig.add_axes([1.9, 0.3, 0.045, 1.4])
    cbar = plt.colorbar(CS, cax=cbar_ax,)
    cbar.ax.set_ylabel(r'$psu$')
    plt.delaxes(axes.flat[-1])
    fig.subplots_adjust(right=1.7,top=1.8)
Ejemplo n.º 36
0
def plot_changes_dept(df_pre_merged, df_post_merged, col_names, ctrl_terms,
                      department_list):
    '''
    Plot changes by department
    '''
    n_cols = 2
    n_rows = math.ceil(len(department_list) / n_cols)
    #print('number of rows', n_rows)
    if n_rows == 2:
        fig, axs = plt.subplots(n_rows, n_cols, figsize=(7.5, 7.5))
    elif n_rows == 3:
        fig, axs = plt.subplots(n_rows, n_cols, figsize=(7.5, 9.5))

    for i, department_name in enumerate(department_list):
        df = get_changes(df_pre_merged,
                         df_post_merged,
                         'id',
                         col_names,
                         'ttal',
                         ctrl_terms,
                         department_name,
                         pctg=False)
        #df.to_csv('images/fig5_{}.csv'.format(re.sub(r"[^a-zA-Z0-9]+", '', department_name)))
        plt.subplot(n_rows, n_cols, i + 1)
        plt.xlabel("Terms", fontsize=9, fontfamily='Arial')
        plt.ylabel("Change", fontsize=9, fontfamily='Arial')
        plt.title(department_name, fontsize=10, fontfamily='Arial')
        plt.grid(which='major', axis='x')
        plt.xticks(rotation='vertical', fontsize=8, fontfamily='Arial')
        plt.yticks(fontsize=8, fontfamily='Arial')
        plt.gca().spines['right'].set_color('none')
        plt.gca().spines['top'].set_color('none')
        plt.bar(df['term'], df['change'], color="#d9b80d")
    if len(department_list) % 2 != 0:
        plt.delaxes(axs[n_cols - 1, n_rows - 1])
    plt.tight_layout()
    plt.show()

    #plt.savefig(save_name)
    '''
Ejemplo n.º 37
0
  def savePlot(self, source, hold):

    for i, data in enumerate(source):
      try:
        self.figure += 1
      except AttributeError:
        self.figure = 1
      plt.figure(self.figure)
      
      if(hold == None):
        plt.plot(data)
      else:
        if(isinstance(hold, list)):
          plt.axis([hold[i][0], hold[i][-1], -1.0, 1.0])
          print(i, data, hold[i][0], hold[i][-1])
          #plt.ylim(-1.0, 1.0)
          plt.plot(hold[i], data)
        else:
          plt.axis([hold[0], hold[-1], -1.0, 1.0])
          plt.plot(hold, data)
      
      plt.savefig('image/' + str(i) + '.png', dpi=72)
      plt.delaxes()
Ejemplo n.º 38
0
def main(infile="waves.out", startpic="start", frame="0"):
    """Visualize shallow water simulation results.

    Args:
        infile: Name of input file generated by simulator
        outfile: Desired output file (mp4 or gif)
        startpic: Name of picture generated at first frame
    """

    u = np.fromfile(infile, dtype=np.dtype('f4'))
    nx = int(u[0])
    ny = int(u[1])
    x = range(0,nx)
    y = range(0,ny)
    u = u[2:]
    fn = int(frame)
    nframe = len(u) // (nx*ny)
    stride = nx // 20
    u = np.reshape(u, (nframe,nx,ny))
    X, Y = np.meshgrid(x,y)

    fig = plt.figure(figsize=(10,10))

    def plot_frame(i, stride=5):
        ax = fig.add_subplot(111, projection='3d')
        ax.set_zlim(0, 2)
        Z = u[i,:,:];
        ax.plot_surface(X, Y, Z, rstride=stride, cstride=stride)
        return ax

    for imgn in range(fn):
        ax = plot_frame(int(imgn))
        plt.savefig(startpic + str(imgn) + ".png")
        plt.delaxes(ax)

    """
Ejemplo n.º 39
0
def plot_ltt(var, **kwargs): 
    """Plot the daily min, max, and mean of a parameter, filtering for known
    bad data as listed in filter_times.py.
    
    :var:  MSID or derived parameter name (string)
    
    Optional inputs:
    :savefig:      Save and close figure (default is True)
    :saveas:       Name to save figure as (default is <MSID>.png)
    :samefig:      Use current figure (default is False)
    :start:        Start time (default is after OAC on 1999:250)
    :stop:         Stop time (default is None)
    :stat:         Statistic type ('5min' or 'daily', default is 'daily')
    :filter:       User-defined Start and End times to filter out due to bad 
                   data.  If none are supplied, it will default to any listed 
                   in filter_times.py.  In addition, LTTplot will always 
                   filter known NSM and SSM events as listed in 
                   filter_times.py (bad_all).
    :plot_stds:    Plot standard deviations (default is True)
    :plot_mins:    Plot minimum values (default is True)
    :plot_means:   Plot mean values (default is True)
    :plot_maxes:   Plot maximum values (default is True)
    :plot_limits:  Plot database yellow caution and red warning limits 
                   (default is True)
    :yellow:       User-defined yellow caution limit lines (default is none)
    :min_mark:     User-defined marker for minimum values (default is 'b:')
    :max_mark:     User-defined marker for maximum values (default is 'g:')
    :mean_mark:    User-defined marker for mean values (default is 'k+')
    :red:          User-defined red warning limit lines  (default is none)                
    :subplot:      Subplot information in [rows, columns, subplot number] 
                   (default is [1, 1, 1])
    :fig_width:    Figure width (default is 8).  Helpful if x-axis is crowded.
                   Irrelevant if samefig=True.
    :fig_height:   Figure height (default varies by number of subplots:  
                   6 for 1 row of subplots, 9 for 2 rows, otherwise 12).
                   Irrelevant if samefig=True.
    :ylim:         User-defined  y-limits (default is none)
    
    :legend:       Display legend (default is False)
    :cust_title:   Custom title (default is MSID and description)
    :cust_unit:    Custom unit to be displayed on y-axis label, typically for 
                   use with custom_mult or derived parameters (default is 
                   none)
    :custom_mult:  Custom multiplier, typically for use with custom unit 
                   (default is none)   
    
    e.g.
    LTTplot('Dist_SatEarth')
    LTTplot('pline05t', ylim=[30,170], savefig=False) 
    LTTplot('pr1tv01t', limit_lines=False, yellow=150, red=240)
    LTTplot('pr2tv01t', limit_lines=False, yellow=[40,150], red=[37,240])
    LTTplot('plaed3gt', filter=['2011:299:00:00:00 2011:300:00:00:00'])
    LTTplot('pcm01t', subplot=[4,1,1], savefig=False) 
    LTTplot('aogbias1', cust_unit='ARCSEC/SEC', cust_mult=3600*180/pi, 
            subplot=311)
    LTTplot('aosares1', start='2003:001', stop='2003:100', fig_width=10, 
            fig_height=6)
    LTTplot('dp_css1_npm_sun', plot_means=False, plot_maxes=False, 
             plot_stds=False, min_mark='rd', legend=True)
    """
    var = var.lower()
    start = kwargs.pop('start', '1999:250')
    stop = kwargs.pop('stop', None)
    stat = kwargs.pop('stat', 'daily')
    
    # Collect data and filter for bad points
    data = fetch.Msid(var, start, stop, stat=stat)
    data.filter_bad_times(table=getattr(bad, 'bad_all'))
    if kwargs.has_key('filter'):
        data.filter_bad_times(table=kwargs.pop('filter'))
    elif hasattr(bad, 'bad_' + var):
        data.filter_bad_times(table=getattr(bad, 'bad_' + var))
    
    # Define subplots and figure size
    # Figure size will vary depending on the number of subplots. If 
    # plotting standard deviations, ax1 will only be used to reference 
    # default axes size.  ax1 will be deleted and data will be plotted on ax2.
    sub = kwargs.pop('subplot', 111)
    # Convert subplot input to list form if provided by user as integer
    if np.isscalar(sub):
        sub = [int(str(sub)[0]), int(str(sub)[1]), int(str(sub)[2])]
    # Create a new figure unless using existing figure
    if not kwargs.pop('samefig', False):
        if kwargs.has_key('fig_height'):
            fig_height = kwargs.pop('fig_height')
        elif sub[0] == 1:  
            fig_height = 6
        elif sub[0] == 2:  
            fig_height = 6
        else:  
            fig_height = 12
        fig_width = kwargs.pop('fig_width', 8)
        pp.figure(figsize=(fig_width, fig_height))
    ax1 = pp.subplot(sub[0], sub[1], sub[2])
    ax1.ticklabel_format(useOffset=False)
    if kwargs.get('plot_stds', True):
        ax_pos = ax1.get_position().get_points()
        ax_width = ax_pos[1,0] - ax_pos[0,0]
        ax_height = ax_pos[1,1] - ax_pos[0,1]
        pp.delaxes(ax1)
        ax2 = pp.axes([ax_pos[0,0], ax_pos[0,1] + .20 * ax_height, ax_width, 
                      .75 * ax_height])  
        ax2.ticklabel_format(useOffset=False)        
    
    # Plot data
    mult = kwargs.pop('cust_mult', 1)    
    lim = kwargs.pop('limit_lines', True)
    if kwargs.pop('plot_maxes', True):
        plot_cxctime(data.times, data.maxes * mult, 
                     kwargs.pop('max_mark', 'g.'), markersize=3, label=(stat + ' maxes'))
    if kwargs.pop('plot_mins', True):
        plot_cxctime(data.times, data.mins * mult, 
                     kwargs.pop('min_mark', 'b.'), markersize=3, label=(stat + ' mins'))
    if kwargs.pop('plot_means', True):
        plot_cxctime(data.times, data.means * mult, 
                     kwargs.pop('mean_mark', 'k.'), markersize=3, label=(stat + ' means'))
  
    # Adjust x-axis
    x_ax = Time.DateTime(pp.xlim(), format='plotdate').mjd
    dur = np.diff(x_ax)
    x_ax_new = [x_ax[0], x_ax[1] + .1*dur[0]]
    pp.xlim(Time.DateTime(x_ax_new, format='mjd').plotdate)

    # Plot limits
    if kwargs.pop('plot_limits', True):
        # Check if single limit set exists in TDB
        if (hasattr(data, 'tdb') and (data.tdb.Tlmt is not None)):
            pp.plot(pp.xlim(), np.array([data.tdb.Tlmt[4] * mult, 
                                         data.tdb.Tlmt[4] * mult]), 'r')
            pp.plot(pp.xlim(), np.array([data.tdb.Tlmt[2] * mult, 
                                         data.tdb.Tlmt[2] * mult]), 'y')
            pp.plot(pp.xlim(), np.array([data.tdb.Tlmt[3] * mult, 
                                         data.tdb.Tlmt[3] * mult]), 'y')
            pp.plot(pp.xlim(), np.array([data.tdb.Tlmt[5] * mult, 
                                         data.tdb.Tlmt[5] * mult]), 'r')  
            if ~kwargs.has_key('ylim'):
                pp.ylim(np.array([data.tdb.Tlmt[4] - 10.0, 
                                  data.tdb.Tlmt[5] + 10.0]))
        
    # Add title
    if kwargs.has_key('cust_title'):
        title = kwargs.pop('cust_title')
    elif hasattr(data, 'tdb'):
        title = data.tdb.technical_name + ' - ' + data.msid.upper() 
    else:
        title=data.msid.upper()    
    pp.title(title)
    
    # Account for custom y-labels
    if kwargs.has_key('cust_unit'):
        pp.ylabel(kwargs.pop('cust_unit'))
    else:
        pp.ylabel(data.unit)
    
    pp.grid()
    
    # Plot custom limit lines
    if kwargs.has_key('ylim'):
        pp.ylim(kwargs.pop('ylim'))
    if kwargs.has_key('yellow'):
        y = np.array([kwargs.pop('yellow')])
        for i in range(len(y)):
            pp.plot(pp.xlim(), np.array([y[i], y[i]]), 'y') 
    if kwargs.has_key('red'):
        r = np.array([kwargs.pop('red')])
        for i in range(len(r)):
            pp.plot(pp.xlim(), np.array([r[i], r[i]]), 'r')        
    
    # Add legend
    if kwargs.pop('legend', False):
        pp.legend(loc='best')
    
    # Plot standard deviations on ax3
    if kwargs.get('plot_stds', True):
        ax2.set_xticklabels([])
        ax3 = pp.axes([ax_pos[0,0], ax_pos[0,1] + .05 * ax_height, 
                       ax_width, .15 * ax_height])
        plot_cxctime(data.times, data.stds * mult, color='k', 
                     label=(stat + ' stdev'))
        ax3.yaxis.set_major_locator(ticker.MaxNLocator(2))
        y_ticks = pp.yticks()
        y_lim = pp.ylim()
        pp.xlim(Time.DateTime(x_ax_new, format='mjd').plotdate) 
        # prevent overlap between y-axis and stdev y-axis
        if (y_lim[1] - y_ticks[0][-1]) / y_lim[-1] < .70:
            pp.yticks(y_ticks[0][:-1])
        
       
    # Ensure xticks aren't rotated
    ax = pp.gca()
    pp.setp(ax.get_xticklabels(), 'rotation', 0)
    
    # Save and close figure
    s = kwargs.pop('savefig', True)
    if s == True:
        figname = kwargs.pop('saveas', data.msid.lower() + '.png')
        pp.savefig(figname)
        pp.close()
Ejemplo n.º 40
0
def fig_lfp_decomposition(fig, axes, params, transient=200, X=['L23E', 'L6E'], show_xlabels=True):
    # ana_params.set_PLOS_2column_fig_style(ratio=0.5)
    # fig, axes = plt.subplots(1,5)
    # fig.subplots_adjust(left=0.06, right=0.96, wspace=0.4, hspace=0.2)
    
    if analysis_params.bw:
        # linestyles = ['-', '-', '--', '--', '-.', '-.', ':', ':']
        linestyles = ['-', '-', '-', '-', '-', '-', '-', '-']
        markerstyles = ['s', 's', 'v', 'v', 'o', 'o', '^', '^']
    else:
        if plt.matplotlib.__version__ == '1.5.x':
            linestyles = ['-', ':']*(len(params.Y) / 2)
            print('CSD variance semi log plots may fail with matplotlib.__version__ {}'.format(plt.matplotlib.__version__))
        else:
            linestyles = ['-', (0, (1,1))]*(len(params.Y) / 2) #cercor version
        # markerstyles = ['s', 's', 'v', 'v', 'o', 'o', '^', '^']
        markerstyles = [None]*len(params.Y)
        linewidths = [1.25 for i in range(len(linestyles))]
    
    plt.delaxes(axes[0])
    
    #population plot
    axes[0] = fig.add_subplot(261)
    axes[0].xaxis.set_ticks([])
    axes[0].yaxis.set_ticks([])
    axes[0].set_frame_on(False)
    plot_population(axes[0], params, aspect='tight', isometricangle=np.pi/32,
                           plot_somas = False, plot_morphos = True,
                           num_unitsE = 1, num_unitsI=1,
                           clip_dendrites=False, main_pops=True,
                           rasterized=False)
    phlp.annotate_subplot(axes[0], ncols=5, nrows=1, letter='A')
    axes[0].set_aspect('auto')
    axes[0].set_ylim(-1550, 50)
    axis = axes[0].axis()
    
    
    
    phlp.remove_axis_junk(axes[1])
    plot_signal_sum(axes[1], params,
                    fname=os.path.join(params.populations_path, X[0] + '_population_LFP.h5'),
                    unit='mV', T=[800,1000], ylim=[axis[2], axis[3]],
                    rasterized=False)
    
    # CSD background colorplot
    im = plot_signal_sum_colorplot(axes[1], params, os.path.join(params.populations_path,  X[0] + '_population_CSD.h5'),
                              unit=r'$\mu$Amm$^{-3}$', T=[800,1000],
                              colorbar=False,
                              ylim=[axis[2], axis[3]], fancy=False,
                              cmap=plt.get_cmap('gray', 21) if analysis_params.bw else plt.get_cmap('bwr_r', 21),
                              rasterized=False)

    cb = phlp.colorbar(fig, axes[1], im,
                       width=0.05, height=0.5,
                       hoffset=-0.05, voffset=0.5)
    cb.set_label('($\mu$Amm$^{-3}$)', labelpad=0.)

    axes[1].set_ylim(-1550, 50)
    axes[1].set_title('LFP and CSD ({})'.format(X[0]), va='baseline')
    phlp.annotate_subplot(axes[1], ncols=3, nrows=1, letter='B')
     
    #quickfix on first axes
    axes[0].set_ylim(-1550, 50)
    if show_xlabels:
        axes[1].set_xlabel(r'$t$ (ms)',labelpad=0.)
    else:
        axes[1].set_xlabel('')
    


    phlp.remove_axis_junk(axes[2])
    plot_signal_sum(axes[2], params,
                    fname=os.path.join(params.populations_path, X[1] + '_population_LFP.h5'), ylabels=False,
                    unit='mV', T=[800,1000], ylim=[axis[2], axis[3]],
                    rasterized=False)
    
    # CSD background colorplot
    im = plot_signal_sum_colorplot(axes[2], params, os.path.join(params.populations_path, X[1] + '_population_CSD.h5'),
                              unit=r'$\mu$Amm$^{-3}$', T=[800,1000], ylabels=False,
                              colorbar=False,
                              ylim=[axis[2], axis[3]], fancy=False, 
                              cmap=plt.get_cmap('gray', 21) if analysis_params.bw else plt.get_cmap('bwr_r', 21),
                              rasterized=False)

    cb = phlp.colorbar(fig, axes[2], im,
                       width=0.05, height=0.5,
                       hoffset=-0.05, voffset=0.5)
    cb.set_label('($\mu$Amm$^{-3}$)', labelpad=0.)

    axes[2].set_ylim(-1550, 50)
    axes[2].set_title('LFP and CSD ({})'.format(X[1]), va='baseline')
    phlp.annotate_subplot(axes[2], ncols=1, nrows=1, letter='C')
    if show_xlabels:
        axes[2].set_xlabel(r'$t$ (ms)',labelpad=0.)
    else:
        axes[2].set_xlabel('')
    

    plotPowers(axes[3], params, params.Y, 'CSD', linestyles=linestyles, transient=transient, markerstyles=markerstyles, linewidths=linewidths)
    axes[3].axis(axes[3].axis('tight'))
    axes[3].set_ylim(-1550, 50)
    axes[3].set_yticks(-np.arange(16)*100)
    if show_xlabels:
        axes[3].set_xlabel(r'$\sigma^2$ ($(\mu$Amm$^{-3})^2$)', va='center')
    axes[3].set_title('CSD variance', va='baseline')
    axes[3].set_xlim(left=1E-7)
    phlp.remove_axis_junk(axes[3])
    phlp.annotate_subplot(axes[3], ncols=1, nrows=1, letter='D')
    
    
    plotPowers(axes[4], params, params.Y, 'LFP', linestyles=linestyles, transient=transient, markerstyles=markerstyles, linewidths=linewidths)
    axes[4].axis(axes[4].axis('tight'))
    axes[4].set_ylim(-1550, 50)
    axes[4].set_yticks(-np.arange(16)*100)
    if show_xlabels:
        axes[4].set_xlabel(r'$\sigma^2$ (mV$^2$)', va='center')
    axes[4].set_title('LFP variance', va='baseline')
    axes[4].legend(bbox_to_anchor=(1.37, 1.0), frameon=False)
    axes[4].set_xlim(left=1E-7)
    phlp.remove_axis_junk(axes[4])
    phlp.annotate_subplot(axes[4], ncols=1, nrows=1, letter='E')
    
    
    
    
    return fig
Ejemplo n.º 41
0
m = 0
s_max = 2
s_min = 0.2
x = np.linspace(m -3*s_max, m + 3*s_max, 1000)
s_values = np.linspace(s_max, s_min, 30)
# f is max for x=m; smaller s gives larger max value
max_f = f(m, m, s_min)

# Make a first plot
mpl.ion()
fig = mpl.figure()

# Show the movie, and make hardcopies of frames simulatenously
counter = 0
for s in s_values:
    mpl.delaxes()  # delete plot, replot everything for this s
    ax = fig.gca()
    ax.axis([x[0], x[-1], -0.1, max_f])
    y = f(x, m, s)
    ax.plot(x, y)
    ax.set_xlabel('x')
    ax.set_ylabel('f')
    ax.legend(['s=%4.2f' % s])
    mpl.draw()
    fig.savefig('tmp_%04d.png' % counter)
    counter += 1
raw_input('Type Return key: ')



Ejemplo n.º 42
0
def pcol( x, y, data, projection=None, vmin=None, vmax=None, **kwargs):
    """function h=pcol(x,y,v)
    function h=pcol(x,y,v, projection = mp )
    
    plots 2D scalar fields v on the MITgcm cubed sphere grid with pcolormesh.
    x,y are really 'xg', and 'yg', that is, they should be the coordinates
    of the points one half grid cell to the left and bottom, that is
    vorticity points for tracers, etc. 
    
    The optional flag 'sphere' results in a 3D visualization on the sphere
    without any specific projection. Good for debugging.
    
    If present, 'projection' (a basemap instance) is used to transform 
    coordinates. Unfortunatly, cylindrical and conic maps are limited to 
    the [-180 180] range. 
    projection = 'sphere' results in a 3D visualization on the sphere
    without any specific projection. Good for debugging.

    Example script to use pcol.py:

    from mpl_toolkits.basemap import Basemap
    import MITgcmutils as mit
    import matplotlib.pyplot as plt
    from sq import sq

    x=mit.rdmds('XG'); y=mit.rdmds('YG'); e=mit.rdmds('Eta',np.Inf)
    fig = plt.figure();
    mp = Basemap(projection='moll',lon_0 = 0.,
                 resolution = 'l', area_thresh = 1000.)
    plt.clf()
    h = mit.cs.pcol(x,y,sq(e), projection = mp)
    mp.fillcontinents(color = 'grey')
    mp.drawmapboundary()
    mp.drawmeridians(np.arange(0, 360, 30))
    mp.drawparallels(np.arange(-90, 90, 30))
    plt.show()

    """

# pcol first divides the 2D cs-field(6*n,n) into six faces. Then for
# each face, an extra row and colum is added from the neighboring faces in
# order to fool pcolor into drawing the entire field and not just
# (n-1,m-1) data points. There are two corner points that have no explicit
# coordinates so that they have to be found by
# interpolation/averaging. Then each face is divided into 4 tiles,
# assuming cs-geometry, and each tile is plotted individually in
# order to avoid problems due to ambigous longitude values (the jump
# between -180 and 180, or 360 and 0 degrees). As long as the poles
# are at the centers of the north and south faces and the first tile is
# symmetric about its center this should work.

    # get the figure handle
    fig=plt.gcf()

    mapit = 0
    if projection!=None:
        mp = projection
        if mp=='sphere': mapit=-1
        else: mapit = 1

    # convert to [-180 180[ representation
    x = np.where(x>180,x-360.,x)

    ny,nx = data.shape
    # determine range for color range
    cax = [data.min(),data.max()]
    if cax[1]-cax[0]==0: cax = [cax[0]-1,cax[1]+1]

    if vmin!=None: cax[0]=vmin
    if vmax!=None: cax[1]=vmax

    if mapit == -1:
        # set up 3D plot
        if len(fig.axes)>0: 
            # if present, remove and replace the last axis of fig
            geom=fig.axes[-1].get_geometry()
            plt.delaxes(fig.axes[-1])
        else:
            # otherwise use full figure
            geom = ((1,1,1))
        ax = fig.add_subplot(geom[0],geom[1],geom[2],projection = '3d',
                             axisbg='None')
        # define color range
        tmp = data - data.min()
        N = tmp/tmp.max()       
        # use this colormap
        colmap = cm.jet
        colmap.set_bad('w',1.0)
        mycolmap = colmap(N) #cm.jet(N)

    ph=np.array([])
    jc=x.shape[0]/2
    xxf=np.empty((jc+1,jc+1,4))
    yyf=xxf
    ffld=np.empty((jc,jc,4))
    xff=[]
    yff=[]
    fldf=[]
    for k in range(0,6):
        ix = np.arange(0,ny) + k*ny
        xff.append(x[0:ny,ix])
        yff.append(y[0:ny,ix])
        fldf.append(data[0:ny,ix])

    # find the missing corners by interpolation (one in the North Atlantic)
    xfodd = (xff[0][-1,0]+xff[2][-1,0]+xff[4][-1,0])/3.
    yfodd = (yff[0][-1,0]+yff[2][-1,0]+yff[4][-1,0])/3.
    # and one south of Australia
    xfeven= (xff[1][0,-1]+xff[3][0,-1]+xff[5][0,-1])/3.
    yfeven= (yff[1][0,-1]+yff[3][0,-1]+yff[5][0,-1])/3.

    # loop over tiles
    for k in range(0,6):
        kodd  = 2*(k/2)
        kodd2 = kodd
        if kodd==4: kodd2=kodd-6
        keven  = 2*(k/2)
        keven2 = keven
        if keven==4: keven2=keven-6
        fld = fldf[k]
        if np.mod(k+1,2):
            xf = np.vstack( [ np.column_stack( [xff[k],xff[1+kodd][:,0]] ),
                              np.flipud(np.append(xff[2+kodd2][:,0],xfodd))] )
            yf = np.vstack( [ np.column_stack( [yff[k],yff[1+kodd][:,0]] ),
                              np.flipud(np.append(yff[2+kodd2][:,0],yfodd))] )
        else:
            xf = np.column_stack( [np.vstack( [xff[k],xff[2+keven2][0,:]] ),
                                   np.flipud(np.append(xff[3+keven2][0,:],
                                                       xfeven))] )
            yf = np.column_stack( [np.vstack( [yff[k],yff[2+keven2][0,:]] ),
                                   np.flipud(np.append(yff[3+keven2][0,:],
                                                       yfeven))] )

        if mapit==-1:
            ix = np.arange(0,ny) + k*ny
            # no projection at all (projection argument is 'sphere'), 
            # just convert to cartesian coordinates and plot a 3D sphere
            deg2rad=np.pi/180.
            xcart,ycart,zcart = sph2cart( xf*deg2rad, yf*deg2rad )
            ax.plot_surface(xcart,ycart,zcart,rstride=1,cstride=1,
                            facecolors=mycolmap[0:ny,ix],
                            linewidth=2,shade=False)
            ph = np.append(ph, ax)
        else:
            # divide all faces into 4 because potential problems arise at
            # the centers 
            for kf in range(0,4):
                if   kf==0: i0,i1,j0,j1 =  0,  jc+1, 0,  jc+1
                elif kf==1: i0,i1,j0,j1 =  0,  jc+1,jc,2*jc+1
                elif kf==2: i0,i1,j0,j1 = jc,2*jc+1, 0,  jc+1
                elif kf==3: i0,i1,j0,j1 = jc,2*jc+1,jc,2*jc+1
                xx = xf[i0:i1,j0:j1]
                yy = yf[i0:i1,j0:j1]
                ff = fld[i0:i1-1,j0:j1-1]
                if np.median(xx) < 0:
                    xx = np.where(xx>=180,xx-360.,xx)
                else:
                    xx = np.where(xx<=-180,xx+360.,xx)
             
                # if provided use projection
                if mapit==1: xx,yy = mp(xx,yy)
            
                # now finally plot 4x6 tiles
                ph = np.append(ph, plt.pcolormesh(xx, yy, ff,
                                                  vmin=cax[0], vmax=cax[1],
                                                  **kwargs))

    if mapit == -1: 
        ax.axis('image')
        ax.set_axis_off()
#        ax.set_visible=False
        # add a reasonable colormap
        m = cm.ScalarMappable(cmap=colmap)
        m.set_array(data)
        plt.colorbar(m)
    elif mapit == 0:
        ax = fig.axes[-1]
        ax.axis('image')
        plt.grid('on')

    return ph
Ejemplo n.º 43
0
def poincarePlot(xyvalues1, xyvalues2, box, max, projections=(0,0)):
    """ 
    Scatter plot with projection histograms 
    IN:
        xyvalues    = tuple (float,float)
        box         = text 
        max         = tuple (float,float)
        projections = tuple (bool,bool)
    """
    from matplotlib.ticker import NullFormatter

    x1, y1 = xyvalues1  # sample 1
    x2, y2 = xyvalues2  # sample2
    ax = plt.subplot()
    if projections == (0,0):
        # the scatter plot
        ax.scatter(x1,y1,s=1)
        if xyvalues2 != (0,0):
            ax.scatter(x2,y2,s=1,c='r')

        # set tick label size
        ax.tick_params(labelsize='xx-small')

        # place a text box in upper left in axes coords
        props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)  # these are matplotlib.patch.Patch properties
        ax.text(0.05, 0.95, box, transform=ax.transAxes, fontsize=10, verticalalignment='top', bbox=props)

        (xmax,ymax) = max
        plt.xlim([-xmax,xmax])
        plt.ylim([-ymax,ymax])
        plt.autoscale(enable=False,axis='both')
        return
    
    elif projections != (0,0):
        nullfmt = NullFormatter()         # no labels
        plt.delaxes(ax)
        # definitions for the axes
        left, width    = 0.1, 0.65
        bottom, height = 0.1, 0.65
        bottom_h       = bottom + height + 0.02
        left_h         = left + width + 0.02

        rect_scatter = [left, bottom, width, height]
        rect_histx   = [left, bottom_h, width, 0.2]
        rect_histy   = [left_h, bottom, 0.2, height]
        
        # the scatter plot
        axScatter = plt.axes(rect_scatter)
        axScatter.scatter(x1, y1, s=1)
        if xyvalues2 != (0,0):
            axScatter.scatter(x2, y2, s=1, c='r')
        
        # set tick label size
        axScatter.tick_params(labelsize='xx-small')

        # place a text box in upper left in axes coords
        props = dict(boxstyle='round', facecolor='wheat', alpha=0.5)  # these are matplotlib.patch.Patch properties
        axScatter.text(0.05, 0.95, box, transform=axScatter.transAxes, fontsize=10, verticalalignment='top', bbox=props)

        (prx,pry)   = projections
        (xmax,ymax) = max
        if prx == 1:
            # x projection
            axHistx      = plt.axes(rect_histx)
            # no labels
            axHistx.xaxis.set_major_formatter(nullfmt)
            axHistx.yaxis.set_major_formatter(nullfmt)
            # now determine nice limits by hand:
            binwidthx = xmax/100.
            limx = (int(xmax/binwidthx) + 1) * binwidthx
            axScatter.set_xlim((-limx, limx))
            binsx = NP.arange(-limx, limx + binwidthx, binwidthx)
            axHistx.hist(x1, bins=binsx, color='black')
            # axHistx.set_xlim(axScatter.get_xlim())

        if pry == 1:
            # y projection
            axHisty      = plt.axes(rect_histy)
            # no labels
            axHisty.xaxis.set_major_formatter(nullfmt)
            axHisty.yaxis.set_major_formatter(nullfmt)
            # now determine nice limits by hand:
            binwidthy = ymax/100.
            limy = (int(ymax/binwidthy) + 1) * binwidthy
            axScatter.set_ylim((-limy, limy))
            binsy = NP.arange(-limy, limy + binwidthy, binwidthy)
            axHisty.hist(y1, bins=binsy, orientation='horizontal', color='black')
            # axHisty.set_ylim(axScatter.get_ylim())

    return
Ejemplo n.º 44
0
  ax.set_xlim(0.0, 2.5)
  ax.set_ylim(0.0, 2.5)
  ax.set_zlim(0.0, 3.0)
  data = np.loadtxt("./particles_"+times[i], skiprows=1)
  locations[:,0] = data[:,0]
  locations[:,1] = data[:,1]
  locations[:,2] = data[:,2]
  #velocities[:,0] = data[:,3]
  #velocities[:,1] = data[:,4]
  #velocities[:,2] = data[:,5]
  X = data[:,0]
  Y = data[:,1]
  Z = data[:,2]
  ax.scatter(X, Z, Y, s=10*10)
  return ax

metadata = dict(title='SPH', artist='Matplotlib')

Writer = manimation.writers['ffmpeg']
writer = Writer(fps=15, metadata=metadata,
                extra_args=["-r", "30",
                            "-c:v", "libx264",
                            "-pix_fmt", "yuv420p"])

with writer.saving(fig, "movie.mp4", f):
  for i in range(f):
    print "frame ",i," written. ",float(i)/float(f)*100.0,"% done. \n",
    ax = plot_frame(i)
    writer.grab_frame()
    plt.delaxes(ax)
Ejemplo n.º 45
0
 def __init__(self, prevHandler, savefile, source_name='time_freq', extent='default'):
   PlotTimeFreqDataHandler.__init__(self, prevHandler, source_name, extent)
   plt.savefig(savefile, dpi=72)
   plt.delaxes()
Ejemplo n.º 46
0
    def _initialize(self):

        # Count number of plots to create:
        num_plots = 0
        for config in self._config.itervalues():
            num_plots += len(config)

        # Set default grid of plot positions:
        if not self._rows*self._cols == num_plots:
            self._cols = int(np.ceil(np.sqrt(num_plots)))
            self._rows = int(np.ceil(num_plots/float(self._cols)))
        self.f, self.axarr = plt.subplots(self._rows, self._cols,
                                          figsize=self._figsize)
        
        # Remove unused subplots:
        for i in xrange(num_plots, self._rows*self._cols):
            plt.delaxes(self.axarr[np.unravel_index(i, (self._rows, self._cols))])
        cnt = 0
        self.handles = []
        self.types = []
        keywds = ['handle', 'ydata', 'fmt', 'type', 'ids', 'shape'] 
        if not isinstance(self.axarr, np.ndarray):
            self.axarr = np.asarray([self.axarr])
        for LPU, configs in self._config.iteritems():
            for plt_id, config in enumerate(configs):
                ind = np.unravel_index(cnt, self.axarr.shape)
                cnt+=1

                # Some plot types require specific numbers of
                # neuron ID arrays:
                if 'type' in config:
                    if config['type'] == 'quiver':
                        assert len(config['ids'])==2
                        config['type'] = 0
                    elif config['type'] == 'hsv':
                        assert len(config['ids'])==2
                        config['type'] = 1
                    elif config['type'] == 'image':
                        assert len(config['ids'])==1
                        config['type'] = 2
                    elif config['type'] == 'waveform':
                        config['type'] = 3
                    elif config['type'] == 'raster':
                        config['type'] = 4
                    elif config['type'] == 'rate':
                        config['type'] = 5
                    else:
                        raise ValueError('Plot type not supported')
                else:
                    if str(LPU).startswith('input') or not self._graph[LPU][str(config['ids'][0])]['spiking']:
                        config['type'] = 2
                    else:
                        config['type'] = 4
                        
                if config['type'] < 3:
                    if not 'shape' in config:

                        # XXX This can cause problems when the number
                        # of neurons is not equal to
                        # np.prod(config['shape'])
                        num_neurons = len(config['ids'][0])
                        config['shape'] = [int(np.ceil(np.sqrt(num_neurons)))]
                        config['shape'].append(int(np.ceil(num_neurons/float(config['shape'][0]))))
                        
                if config['type'] == 0:
                    config['handle'] = self.axarr[ind].quiver(\
                               np.reshape(self._data[LPU][config['ids'][0],0],config['shape']),\
                               np.reshape(self._data[LPU][config['ids'][1],0],config['shape']))
                elif config['type'] == 1:
                    X = np.reshape(self._data[LPU][config['ids'][0],0],config['shape'])
                    Y = np.reshape(self._data[LPU][config['ids'][1],0],config['shape'])
                    V = (X**2 + Y**2)**0.5
                    H = (np.arctan2(X,Y)+np.pi)/(2*np.pi)
                    S = np.ones_like(V)
                    HSV = np.dstack((H,S,V))
                    RGB = hsv_to_rgb(HSV)
                    config['handle'] = self.axarr[ind].imshow(RGB)
                elif config['type'] == 2:
                    if 'trans' in config:
                        if config['trans'] is True:
                            to_transpose = True
                        else:
                            to_transpose = False
                    else:
                        to_transpose = False
                        config['trans'] = False
                    
                    if to_transpose:
                        temp = self.axarr[ind].imshow(np.transpose(np.reshape(\
                                self._data[LPU][config['ids'][0],0], config['shape'])))
                    else:
                        temp = self.axarr[ind].imshow(np.reshape(\
                                self._data[LPU][config['ids'][0],0], config['shape']))
            
            
            
                    temp.set_clim(self._imlim)
                    temp.set_cmap(plt.cm.gist_gray)
                    config['handle'] = temp
                elif config['type'] == 3:
                    fmt = config['fmt'] if 'fmt' in config else '' 
                    self.axarr[ind].set_xlim(self._xlim)
                    self.axarr[ind].set_ylim(self._ylim)
                    if len(config['ids'][0])==1:
                        config['handle'] = self.axarr[ind].plot([0], \
                                            [self._data[LPU][config['ids'][0][0],0]], fmt)[0]
                        config['ydata'] = [self._data[LPU][config['ids'][0][0],0]]
                    else:
                        config['handle'] = self.axarr[ind].plot(self._data[LPU][config['ids'][0],0])[0]
                        
                elif config['type'] == 4:
                    config['handle'] = self.axarr[ind]
                    config['handle'].vlines(0, 0, 0.01)
                    config['handle'].set_ylim([.5, len(config['ids'][0]) + .5])
                    config['handle'].set_ylabel('Neurons',
                                                fontsize=self._fontsize-1, weight='bold')
                    config['handle'].set_xlabel('Time (s)',fontsize=self._fontsize-1, weight='bold')
                    config['handle'].set_xlim([0,len(self._data[LPU][config['ids'][0][0],:])*self._dt])
                    config['handle'].axes.set_yticks([])
                    config['handle'].axes.set_xticks([])
                for key in config.iterkeys():
                    if key not in keywds:
                        try:
                            self._set_wrapper(self.axarr[ind],key, config[key])
                        except:
                            pass
                        try:
                            self._set_wrapper(config['handle'],key, config[key])
                        except:
                            pass
                if config['type']<3:
                    config['handle'].axes.set_xticks([])
                    config['handle'].axes.set_yticks([])

            if self.suptitle is not None:
                self.f.suptitle(self._title, fontsize=self._fontsize+1, x=0.5,y=0.03, weight='bold')

        plt.tight_layout()

        if self.out_filename:
            self.writer = FFMpegFileWriter(fps=self.fps, codec=self.codec)

            # Use the output file to determine the name of the temporary frame
            # files so that two concurrently run visualizations don't clobber
            # each other's frames:
            self.writer.setup(self.f, self.out_filename, dpi=80,
                              frame_prefix=os.path.splitext(self.out_filename)[0]+'_')
            self.writer.frame_format = 'png'
            self.writer.grab_frame()
        else:
            self.f.show()
Ejemplo n.º 47
0
    def _onKeyRelease(self, event):
        """
        Fired on all key releases.

        It essentially is a simple state manager with the key being routed to
        different places depending on the current application state.
        """
        key = event.key

        # Default mode is no mode.
        if self._current_app_mode is None:
            # Enter help mode.
            if key == KEYMAP["show help"]:
                self._current_app_mode = "help"
                self._axes_to_restore = plt.gcf().axes
                self.help_axis = plt.gcf().add_axes((0, 0, 1, 1))
                self.help_axis.text(
                    0.5, 0.8, HELP_TEXT, transform=self.help_axis.transAxes,
                    verticalalignment="center", horizontalalignment="center")
                self.help_axis.set_xticks([])
                self.help_axis.set_yticks([])
                self._deactivate_multicursor()
                plt.draw()
            # Enter weight selection mode.
            elif key == KEYMAP["set weight"]:
                self._current_app_mode = "weight_selection"
                self._current_weight = ""
                self._update_current_weight("", editing=True)
            # Navigation
            elif key == KEYMAP["next station"]:
                self.next()
            elif key == KEYMAP["previous station"]:
                self.prev()
            elif key == KEYMAP["reset station"]:
                self.reset()
            return

        # Weight selection mode.
        elif self._current_app_mode == "weight_selection":
            weight_keys = "0123456789."
            # Keep typing the new weight.
            if key in weight_keys:
                self._current_weight += key
                self._update_current_weight(self._current_weight, editing=True)
            # Set the new weight. If that fails, reset it. In any case, leave
            # the weight selection mode.
            else:
                self._current_app_mode = None
                try:
                    weight = float(self._current_weight)
                except:
                    self._update_current_weight(self.weight)
                    return
                self._update_current_weight(weight)

        # Help selection mode. Any keypress while in it will exit it.
        elif self._current_app_mode == "help":
            plt.delaxes(self.help_axis)
            del self.help_axis
            for ax in self._axes_to_restore:
                plt.gcf().add_axes(ax)
            del self._axes_to_restore
            plt.tight_layout()
            self._activate_multicursor()
            plt.draw()
            self._current_app_mode = None
            return
Ejemplo n.º 48
0
    def plot(self, show_best=True, show_mean=True, latexify=False,
             font_family="serif", scale='linear', summarize=True,
             print_pareto=False):
        """
        Visualize the progress of an optimization.

        Args:
            show_best (bool): Point out the best point on legend and on plot. If
                more than one best point (i.e., multiple equal maxima), show
                them all. If multiobjective, shows best for each objective, and
                prints the best value and x for each objective.
            show_mean (bool): Show the mean and standard deviation for the
                guesses as the computations are carried out.
            latexify (bool): Use LaTeX for formatting.
            font_family (str): The font family to use for rendering. Choose from
                'serif', 'sans-serif', 'fantasy', 'monospace', or 'cursive'.
            scale (str): Whether to scale the plot's y axis according to log
                ('log') or 'linear' scale.
            summarize (bool): If True, stdouts summary from .summarize.
            print_pareto (bool): If True, display all Pareto-optimal objective
                values.

        Returns:
            A matplotlib plot object handle
        """
        if not self.is_configured:
            raise NotConfiguredError("Use MissionControl.configure to configure"
                                     "your optimization collection before "
                                     "plotting!")
        maximize = self.config["maximize"]
        fxstr = "$f(x)$" if latexify else "f(x)"
        opt = max if maximize else min
        objs = self.c.find_one({'index': {'$exists': 1}})['y']
        n_objs = len(objs) if isinstance(objs, (list, tuple)) else 1
        dt = datetime.datetime.now()
        dtdata = [dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second]
        timestr = "{}-{}-{} {}:{}.{}".format(*dtdata)
        t0 = time.time()
        if latexify:
            plt.rc('text', usetex=True)
        else:
            plt.rc('text', usetex=False)
        plt.rc('font', family=font_family, size=9)
        n_cols = 3
        if n_objs < n_cols:
            _, ax_arr = plt.subplots(n_objs, squeeze=False)
        else:
            _, ax_arr = plt.subplots(n_cols, int(math.ceil(n_objs / n_cols)),
                                    squeeze=False)
        docset = self.c.find({'index': {'$exists': 1}})
        docs = [None] * docset.count()
        for i, doc in enumerate(docset):
            docs[i] = {'y': doc['y'], 'index': doc['index'], 'x': doc['x']}
        if n_objs > 1:
            all_y = np.asarray([doc['y'] for doc in docs])
            pareto_set = all_y[pareto(all_y, maximize=maximize)].tolist()
            pareto_graph = [(i + 1, doc['y']) for i, doc in enumerate(docs)
                            if doc['y'] in pareto_set]
            pareto_i = [i[0] for i in pareto_graph]

        print("Optimization Analysis:")
        print("Number of objectives: {}".format(n_objs))

        for obj in range(n_objs):
            ax = ax_arr[obj % n_cols, int(math.floor(obj / n_cols))]

            i = []
            fx = []
            best = []
            mean = []
            std = []
            n = self.c.find().count() - 2

            for doc in docs:
                fx.append(doc['y'] if n_objs == 1 else doc['y'][obj])
                i.append(doc['index'])
                best.append(opt(fx))
                mean.append(np.mean(fx))
                std.append(np.std(fx))

            if time.time() - t0 > 60:
                self.logger.warn(
                    "Gathering data from the db is taking a while. Ensure"
                    "the latency to your db is low and the bandwidth"
                    "is as high as possible!")

            mean = np.asarray(mean)
            std = np.asarray(std)

            ax.scatter(i, fx, color='blue', label=fxstr, s=10)
            ax.plot(i, best, color='orange', label="best {} value found so far"
                                                   "".format(fxstr))
            if show_mean:
                ax.plot(i, mean, color='grey', label="mean {} value (with std "
                                                     "dev.)".format(fxstr))
                ax.fill_between(i, mean + std, mean - std, color='grey',
                                alpha=0.3)

            ax.set_xlabel("{} evaluation".format(fxstr))
            ax.set_ylabel("{} value".format(fxstr))
            best_val = opt(best)

            if show_best:

                if latexify:
                    best_label = "Best value: $f(x) = {}$" \
                                 "".format(latex_float(best_val))
                else:
                    best_label = "Best value: f(x) = {:.2E}".format(best_val)
                best = self.c.find({'y': best_val})

                if n_objs == 1:
                    print("\tNumber of optima: {}".format(best.count()))
                else:
                    print("\tNumber of optima for objective {}: {}"
                          "".format(obj + 1, best.count()))

                for b in best:
                    bl = None if n_objs > 1 else best_label
                    ax.scatter([b['index']], [best_val], color='darkgreen',
                               s=50,
                               linewidth=3, label=bl, facecolors='none',
                               edgecolors='darkgreen')

                    artext = "$x = $ [" if latexify else "x = ["
                    for i, xi in enumerate(b['x']):
                        if i > 0:
                            artext += ". \mbox{~~~~~}" if latexify else "     "
                        if type(xi) in dtypes.floats:
                            if latexify:
                                artext += "${}$,\n".format(latex_float(xi))
                            else:
                                artext += "{:.2E},\n".format(xi)
                        else:
                            artext += str(xi) + ",\n"

                    artext = artext[:-2] + "]"
                    objstr = "objective {}".format(
                        obj + 1) if n_objs > 1 else ""
                    if maximize:
                        print("\t\tmax(f(x)) {} is {} at x = {}"
                              "".format(objstr, best_val, b['x']))
                    else:
                        print("\t\tmin(f(x)) {} is {} at x = {}"
                              "".format(objstr, best_val, b['x']))
                    ax.annotate(artext,
                                xy=(b['index'] + 0.5, best_val),
                                xytext=(b['index'] + float(n) / 12.0, best_val),
                                arrowprops=dict(color='green'),
                                color='darkgreen',
                                bbox=dict(facecolor='white', alpha=1.0))
            else:
                best_label = ""
            if n_objs > 1:
                pareto_fx = [i[1][obj] for i in pareto_graph]
                ax.scatter(pareto_i, pareto_fx, color='red',
                           label="Pareto optimal", s=20)
            if n_objs > 1:
                ax.set_title("Objective {}: {}".format(obj + 1, best_label))
            ax.set_yscale(scale)
        plt.gcf().set_size_inches(10, 10)
        if summarize:
            print(self.summarize())

        if print_pareto and n_objs > 1:
            print("Pareto Frontier: {} points, ranked by hypervolume".format(
                len(pareto_set)))
            pareto_y = [doc['y'] for doc in docs if doc['y'] in pareto_set]
            pareto_x = [doc['x'] for doc in docs if doc['y'] in pareto_set]

            # Order y by hypervolume
            hypervolumes = [np.prod(y) for y in pareto_y]
            pareto_y_ordered = [y for _, y in
                                sorted(zip(hypervolumes, pareto_y),
                                       reverse=True)]
            pareto_x_ordered = [x for _, x in
                                sorted(zip(hypervolumes, pareto_x),
                                       reverse=True)]
            hypervolumes_ordered = sorted(hypervolumes, reverse=True)

            for i, _ in enumerate(pareto_set):
                print("f(x) = {} @ x = {} with hypervolume {}".format(
                    pareto_y_ordered[i], pareto_x_ordered[i],
                    hypervolumes_ordered[i]))
        if n_objs % n_cols != 0 and n_objs > n_cols:
            for i in range(n_objs % n_cols, n_cols):
                plt.delaxes(ax_arr[i, -1])
        plt.legend()
        # plt.tight_layout(pad=0.01, w_pad=0.01, h_pad=0.01)
        plt.subplots_adjust(wspace=0.3, hspace=0.5)
        plt.suptitle("Rocketsled optimization results for {} - "
                     "{}".format(self.c.name, timestr), y=0.99)
        return plt
Ejemplo n.º 49
0
def boxPlot(x_axis = 'initialFreq',
            dataPath = '/home/muddcs15/research/work/hemiplasy/results/',
            prob1 = '0.001',
            prob2 = '0.01',
            prob3 = '0.05',
            prob4 = '0.1',
            prob5 = '0.5',
            spectree = '/home/muddcs15/research/work/hemiplasy/data/config/fungi.stree',
            output = 'save'):
    """
    A function that will output boxplots of probability of hemiplasy and probability of hemiplasy over duploss vs. initial allele frequency
    x_axis can be:
    'initialFreq'
    'pairs'
    'dupLocation'
    output can be: 'save' or 'print'
    """

    if x_axis == 'initialFreq':
        events = open('/home/muddcs15/research/work/hemiplasy/results/hemiplasy-loss.error.txt', 'r')

        for line in events:
            famid, locus, spcs, gns, snode, lca = line.rstrip().split('\t')

        # define number of plots to be outputed
        fig, axes = plt.subplots(nrows=1, ncols=2)

        # identify the files for each of the different initial frequencies
        probs1 = os.path.join(dataPath, 'probabilities-' + prob1 + '.txt')
        probs2 = os.path.join(dataPath, 'probabilities-' + prob2 + '.txt')
        probs3 = os.path.join(dataPath, 'probabilities-' + prob3 + '.txt')
        probs4 = os.path.join(dataPath, 'probabilities-' + prob4 + '.txt')
        probs5 = os.path.join(dataPath, 'probabilities-' + prob5 + '.txt')
        probsList = [probs1, probs2, probs3, probs4, probs5]

        totalPerList = []   # probability of hemiplasy compared to duploss
        totalAList = []     # probability of hemiplasy ocurring

        h = 0 # probability that ocurred by hemiplasy
        d = 0 # probability that ocurred by duploss

        # open each probability file
        for probFilename in probsList:

            
            hList = []      # list of probability of hemiplasy
            perList = []    # list of percentage with prob hemiplasy > prob duploss
            aveList = []    # list of average probability of hemiplasy per fam id

            # look at each famid for that initial frequency
            probFile = open(probFilename, "r")
            for line in probFile:
                sepProbs = line.split()
                fam = sepProbs.pop(0)
                famid = fam[6:]

                # get the probability of duploss and hemiplasy for each trial in each famid
                for pair in sepProbs:
                    duploss, hemiplasy = map(float, pair.split(','))
                    hList.append(hemiplasy)
                    # check whether hemiplasy is more likely or duploss
                    if hemiplasy > duploss:
                        h += 1
                    else:
                        d += 1
                        
                # calculate the percent that likely ocurred by hemiplasy            
                percent = float(h)/float(h+d)
                # get the average probability of hemiplasy for each famid
                ave = stats.mean(hList)

                # append percent by hemiplasy to perList and average for the famid to aveList
                perList.append(percent)
                aveList.append(ave)
                
            # append the lists through each famid to the large lists for each list of values
            totalPerList.append(perList)
            totalAList.append(aveList)

            # close file
            probFile.close()

        # define the first plot and its labels
        axes[0].boxplot(totalAList)
        axes[0].set_title('Probability of Hemiplasy')
        axes[0].set_xticklabels(['0.001','0.01','0.05','0.1','0.5'],minor=False)
        axes[0].set_xlabel('Initial Frequency')
        axes[0].set_ylabel('Probability')

        # define the second plot and its labels
        axes[1].boxplot(totalPerList)
        axes[1].set_title('Probability of Hemiplasy vs. DupLoss')
        axes[1].set_xticklabels(['0.001','0.01','0.05','0.1','0.5'],minor=False)
        axes[1].set_xlabel('Initial Frequency')
        axes[1].set_ylabel('Probability')

        # save the plots if that is desired
        if output == 'save':
            fig = plt.gcf()
            fig.set_size_inches(10, 8)
            fig.savefig(dataPath+'figures/freq.png')

        # print the plots if that is desired
        elif output == 'print':
            plt.show()

    elif x_axis == 'pairs':
        stree = treelib.read_tree(spectree) # species tree
        species = stree.leaf_names()
        species1 = []
        species2 = []
        for node in stree:
            if len(node.leaves()) == 2:
                species1.append(node.children[0].name)
                species2.append(node.children[1].name)

        # identify the files for each of the different initial frequencies
        probs1 = os.path.join(dataPath, 'probabilities-' + prob1 + '.txt')
        probs2 = os.path.join(dataPath, 'probabilities-' + prob2 + '.txt')
        probs3 = os.path.join(dataPath, 'probabilities-' + prob3 + '.txt')
        probs4 = os.path.join(dataPath, 'probabilities-' + prob4 + '.txt')
        probsList = [probs1, probs2, probs3, probs4]

        totalPerList = []   # probability of hemiplasy compared to duploss
        totalAList = []     # probability of hemiplasy ocurring
        totalPairs = []

        h = 0 # probability that ocurred by hemiplasy
        d = 0 # probability that ocurred by duploss

        pair1 = []
        pair2 = []
        pair3 = []
        pair4 = []
        pair5 = []

        # open each probability file
        for probFilename in probsList:
            events = open('/home/muddcs15/research/work/hemiplasy/results/hemiplasy-loss.txt', 'r')
            
            hList = []      # list of probability of hemiplasy
            perList = []    # list of percentage with prob hemiplasy > prob duploss
            aveList = []    # list of average probability of hemiplasy per fam id

            countTrue = 0

            # look at each famid for that initial frequency
            probFile = open(probFilename, "r")
            for line in probFile:
                sepProbs = line.split()
                fam = sepProbs.pop(0)
                famid = fam[6:]

                # get the probability of duploss and hemiplasy for each trial in each famid
                for pair in sepProbs:
                    duploss, hemiplasy = map(float, pair.split(','))
                    hList.append(hemiplasy)
                    # check whether hemiplasy is more likely or duploss
                    if hemiplasy > duploss:
                        h += 1
                    else:
                        d += 1

                # calculate the percent that likely ocurred by hemiplasy            
                percent = float(h)/float(h+d)
                # get the average probability of hemiplasy for each famid
                ave = stats.mean(hList)

                # append percent by hemiplasy to perList and average for the famid to aveList
                perList.append(percent)
                aveList.append(ave)

                # get the number pair between which the hemiplasy could have occurred
                for line in events:
                    ev_famid, locus, spcs, gns, snode, lca = line.rstrip().split('\t')
                    if famid == ev_famid:
                        countTrue += 1
                        for sp1, sp2 in zip(species1, species2):
                            if (sp1 in spcs and sp2 not in spcs):
                                spec_check = sp1
                                specPos = species1.index(sp1)
                                
                            elif (sp2 in spcs and sp1 not in spcs):
                                spec_check = sp2
                                specPos = species2.index(sp2)
                        break

                # add the hemiplasy probability to the list of the pair in which it potentially occurred        
                if specPos == 0:
                    pair1.append(ave)
                if specPos == 1:
                    pair2.append(ave)
                if specPos == 2:
                    pair3.append(ave)
                if specPos == 3:
                    pair4.append(ave)
                if specPos == 4:
                    pair5.append(ave)
            events.close()
            

            # close file
            probFile.close()

        # append the list of probabilities of hemiplasy for each pair as a separate list to the totalPairs list, creating a list of lists
        totalPairs.append(pair1)
        totalPairs.append(pair2)
        totalPairs.append(pair3)
        totalPairs.append(pair4)
        totalPairs.append(pair5)

        # define the plot's inputs
        plt.boxplot(totalPairs)
        plt.title('Hemiplasy by Pairs')
        plt.xlabel('Pair')
        plt.ylabel('Probability')
        
        # save the plots if that is desired
        if output == 'save':
            fig = plt.gcf()
            fig.set_size_inches(10, 8)
            fig.savefig(dataPath+'figures/pairs.png')

        # print the plots if that is desired
        elif output == 'print':
            plt.show()

    elif x_axis == 'dupLocation':
        stree = treelib.read_tree(spectree) # species tree
        species = stree.leaf_names()
        species1 = []
        species2 = []
        for node in stree:
            if len(node.leaves()) == 2:
                species1.append(node.children[0].name)
                species2.append(node.children[1].name)
        
        # define number of plots to be outputed
        fig, axes = plt.subplots(nrows=2, ncols=3)

        # identify the files for each of the different initial frequencies
        probs1 = os.path.join(dataPath, 'probabilities-' + prob1 + '.txt')
        probs2 = os.path.join(dataPath, 'probabilities-' + prob2 + '.txt')
        probs3 = os.path.join(dataPath, 'probabilities-' + prob3 + '.txt')
        probs4 = os.path.join(dataPath, 'probabilities-' + prob4 + '.txt')
        probsList = [probs1, probs2, probs3, probs4]

        totalPerList = []   # probability of hemiplasy compared to duploss
        totalAList = []     # probability of hemiplasy ocurring
        totalPairs = []

        h = 0 # probability that ocurred by hemiplasy
        d = 0 # probability that ocurred by duploss

        pair1 = []
        pair2 = []
        pair3 = []
        pair4 = []
        pair5 = []
        pairList = [pair1, pair2, pair3, pair4, pair5]

        totalFList = []
        totalPDList =[]

        # open each probability file
        for probFilename in probsList:
            events = open('/home/muddcs15/research/work/hemiplasy/results/hemiplasy-loss.txt', 'r')
            
            hList = []      # list of probability of hemiplasy
            perList = []    # list of percentage with prob hemiplasy > prob duploss
            aveList = []    # list of average probability of hemiplasy per fam id
            famList = []
            PDList = []

            # look at each famid for that initial frequency
            probFile = open(probFilename, "r")
            for line in probFile:
                sepProbs = line.split()
                fam = sepProbs.pop(0)
                famid = fam[6:]
                famList.append(famid)
                
                # get the probability of duploss and hemiplasy for each trial in each famid
                for pair in sepProbs:
                    duploss, hemiplasy = map(float, pair.split(','))
                    hList.append(hemiplasy)
                    # check whether hemiplasy is more likely or duploss
                    if hemiplasy > duploss:
                        h += 1
                    else:
                        d += 1

                # calculate the percent that likely ocurred by hemiplasy            
                percent = float(h)/float(h+d)
                # get the average probability of hemiplasy for each famid
                ave = stats.mean(hList)

                # append percent by hemiplasy to perList and average for the famid to aveList
                perList.append(percent)
                aveList.append(ave)

                # get the number pair between which the hemiplasy could have occurred
                for line in events:
                    ev_famid, locus, spcs, gns, dup, lca = line.rstrip().split('\t')
                    if famid == ev_famid:
                        for sp1, sp2 in zip(species1, species2):
                            if (sp1 in spcs and sp2 not in spcs):
                                spec_check = sp1
                                specPos = species1.index(sp1)
                                
                            elif (sp2 in spcs and sp1 not in spcs):
                                spec_check = sp2
                                specPos = species2.index(sp2)
                        break
                
                PDList.append((specPos, dup))
            # add the duplication location and the hemiplasy probability to the list of the pair it was determined to have occurred in
            famNum = 0
            for pos, dpl in PDList:
                if pos == 0:
                    pair1.append((int(dpl), aveList[famNum]))
                if pos == 1:
                    pair2.append((int(dpl), aveList[famNum]))
                if pos == 2:
                    pair3.append((int(dpl), aveList[famNum]))
                if pos == 3:
                    pair4.append((int(dpl), aveList[famNum]))
                if pos == 4:
                    pair5.append((int(dpl), aveList[famNum]))
                famNum += 1
            events.close()
                    
            # append the lists through each famid to the large lists for each list of values
            totalPerList.append(perList)
            totalAList.append(aveList)
            totalFList.append(famList)
            totalPDList.append(PDList)

            # close file
            probFile.close()

        # create a dictionary with the lists of each pair
        finalPair = collections.defaultdict(list)

        # go through each pair list and get the duplication
        pairCount = 0
        for pairNum in pairList:
            pairCount += 1
            dup = collections.defaultdict(list)

            # get the probability pertaining to each duplication location and the pair, and append it to the dictionary 
            for (dupLoc, prob) in pairNum:
                dup[dupLoc].append(prob)

            # add each pair as a separate list of lists to the finalPair list 
            finalPair[pairCount].extend([dup[dupLoc] for dupLoc in xrange(1,14)])
       
                
        # define the first plot and its labels
        axes[0,0].boxplot(finalPair[1])
        axes[0,0].set_title('Pair1')
        axes[0,0].set_xlabel('Duplication Location')
        axes[0,0].set_ylabel('Probability')
        axes[0,0].set_ylim(0,0.25)

        # define the second plot and its labels
        axes[0,1].boxplot(finalPair[2])
        axes[0,1].set_title('Pair2')
        axes[0,1].set_xlabel('Duplication Location')
        axes[0,1].set_ylabel('Probability')
        axes[0,1].set_ylim(0,0.25)

        # define the third plot and its labels
        axes[0,2].boxplot(finalPair[3])
        axes[0,2].set_title('Pair3')
        axes[0,2].set_xlabel('Duplication Location')
        axes[0,2].set_ylabel('Probability')
        axes[0,2].set_ylim(0,0.25)

        # define the fourth plot and its labels
        axes[1,0].boxplot(finalPair[4])
        axes[1,0].set_title('Pair4')
        axes[1,0].set_xlabel('Duplication Location')
        axes[1,0].set_ylabel('Probability')
        axes[1,0].set_ylim(0,0.25)
        
        # define the fifth plot and its labels
        axes[1,1].boxplot(finalPair[5])
        axes[1,1].set_title('Pair5')
        axes[1,1].set_xlabel('Duplication Location')
        axes[1,1].set_ylabel('Probability')
        axes[1,1].set_ylim(0,0.25)

        plt.delaxes(axes[1,2])
        # save the plots if that is desired
        if output == 'save':
            fig = plt.gcf()
            fig.set_size_inches(15, 10)
            fig.savefig(dataPath+'figures/dupLoc.png')

        # print the plots if that is desired
        elif output == 'print':
            plt.show()
Ejemplo n.º 50
0
    def _initialize(self):

        # Count number of plots to create:
        num_plots = 0
        for config in self._config.itervalues():
            num_plots += len(config)

        # Set default grid of plot positions:
        if not self._rows*self._cols == num_plots:
            self._cols = int(np.ceil(np.sqrt(num_plots)))
            self._rows = int(np.ceil(num_plots/float(self._cols)))
        self.f, self.axarr = plt.subplots(self._rows, self._cols,
                                          figsize=self._figsize)

        # Remove unused subplots:
        for i in xrange(num_plots, self._rows*self._cols):
            plt.delaxes(self.axarr[np.unravel_index(i, (self._rows, self._cols))])
        cnt = 0
        self.handles = []
        self.types = []
        keywds = ['handle', 'ydata', 'fmt', 'type', 'ids', 'shape', 'norm']
        # TODO: Irregular grid in U will make the plot better
        U, V = np.mgrid[0:np.pi/2:complex(0, 60),
                        0:2*np.pi:complex(0, 60)]
        X = np.cos(V)*np.sin(U)
        Y = np.sin(V)*np.sin(U)
        Z = np.cos(U)
        self._dome_pos_flat = (X.flatten(), Y.flatten(), Z.flatten())
        self._dome_pos = (X, Y, Z)
        self._dome_arr_shape = X.shape
        if not isinstance(self.axarr, np.ndarray):
            self.axarr = np.asarray([self.axarr])
        for LPU, configs in self._config.iteritems():
            for plt_id, config in enumerate(configs):
                ind = np.unravel_index(cnt, self.axarr.shape)
                cnt+=1

                # Some plot types require specific numbers of
                # neuron ID arrays:
                if 'type' in config:
                    if config['type'] == 'quiver':
                        assert len(config['ids'])==2
                        config['type'] = 0
                    elif config['type'] == 'hsv':
                        assert len(config['ids'])==2
                        config['type'] = 1
                    elif config['type'] == 'image':
                        assert len(config['ids'])==1
                        config['type'] = 2
                    elif config['type'] == 'waveform':
                        config['type'] = 3
                    elif config['type'] == 'raster':
                        config['type'] = 4
                    elif config['type'] == 'rate':
                        config['type'] = 5
                    elif config['type'] == 'dome':
                        config['type'] = 6
                    else:
                        raise ValueError('Plot type not supported')
                else:
                    if str(LPU).startswith('input') and not self._graph[LPU].node[str(config['ids'][0][0])]['spiking']:
                        config['type'] = 2
                    else:
                        config['type'] = 4

                if config['type'] < 3:
                    if not 'shape' in config:

                        # XXX This can cause problems when the number
                        # of neurons is not equal to
                        # np.prod(config['shape'])
                        num_neurons = len(config['ids'][0])
                        config['shape'] = [int(np.ceil(np.sqrt(num_neurons)))]
                        config['shape'].append(int(np.ceil(num_neurons/float(config['shape'][0]))))

                if config['type'] == 0:
                    config['handle'] = self.axarr[ind].quiver(\
                               np.reshape(self._data[LPU][config['ids'][0],0],config['shape']),\
                               np.reshape(self._data[LPU][config['ids'][1],0],config['shape']))
                elif config['type'] == 1:
                    X = np.reshape(self._data[LPU][config['ids'][0],0],config['shape'])
                    Y = np.reshape(self._data[LPU][config['ids'][1],0],config['shape'])
                    V = (X**2 + Y**2)**0.5
                    H = (np.arctan2(X,Y)+np.pi)/(2*np.pi)
                    S = np.ones_like(V)
                    HSV = np.dstack((H,S,V))
                    RGB = hsv_to_rgb(HSV)
                    config['handle'] = self.axarr[ind].imshow(RGB)
                elif config['type'] == 2:
                    if 'trans' in config:
                        if config['trans'] is True:
                            to_transpose = True
                        else:
                            to_transpose = False
                    else:
                        to_transpose = False
                        config['trans'] = False

                    if to_transpose:
                        temp = self.axarr[ind].imshow(np.transpose(np.reshape(\
                                self._data[LPU][config['ids'][0],0], config['shape'])))
                    else:
                        temp = self.axarr[ind].imshow(np.reshape(\
                                self._data[LPU][config['ids'][0],0], config['shape']))



                    temp.set_clim(self._imlim)
                    temp.set_cmap(plt.cm.gist_gray)
                    config['handle'] = temp
                elif config['type'] == 3:
                    fmt = config['fmt'] if 'fmt' in config else '' 
                    self.axarr[ind].set_xlim(self._xlim)
                    self.axarr[ind].set_ylim(self._ylim)
                    if len(config['ids'][0])==1:
                        config['handle'] = self.axarr[ind].plot([0], \
                                            [self._data[LPU][config['ids'][0][0],0]], fmt)[0]
                        config['ydata'] = [self._data[LPU][config['ids'][0][0],0]]
                    else:
                        config['handle'] = self.axarr[ind].plot(self._data[LPU][config['ids'][0],0])[0]

                elif config['type'] == 4:
                    config['handle'] = self.axarr[ind]
                    config['handle'].vlines(0, 0, 0.01)
                    config['handle'].set_ylim([.5, len(config['ids'][0]) + .5])
                    config['handle'].set_ylabel('Neurons',
                                                fontsize=self._fontsize-1, weight='bold')
                    config['handle'].set_xlabel('Time (s)',fontsize=self._fontsize-1, weight='bold')
                    min_id = min(self._id_to_data_idx[LPU].keys())
                    min_idx = self._id_to_data_idx[LPU][min_id]
                    config['handle'].set_xlim([0,len(self._data[LPU][min_idx,:])*self._dt])
                    config['handle'].axes.set_yticks([])
                    config['handle'].axes.set_xticks([])
                elif config['type'] == 6:
                    self.axarr[ind].axes.set_yticks([])
                    self.axarr[ind].axes.set_xticks([])
                    self.axarr[ind] = self.f.add_subplot(self._rows,
                                                         self._cols,
                                                         cnt,
                                                         projection='3d')
                    config['handle' ] = self.axarr[ind]
                    config['handle'].axes.set_yticks([])
                    config['handle'].axes.set_xticks([])
                    config['handle'].xaxis.set_ticks([])
                    config['handle'].yaxis.set_ticks([])
                    config['handle'].zaxis.set_ticks([])
                    if 'norm' not in config.keys():
                        config['norm'] = Normalize(vmin=-70, vmax=0, clip=True)
                    elif config['norm'] == 'auto':
                        if self._data[LPU].shape[1] > 100:
                            config['norm'] = Normalize(vmin = np.min(self._data[LPU][config['ids'][0],100:]),
                                                       vmax = np.max(self._data[LPU][config['ids'][0],100:]),
                                                       clip = True)
                        else:
                            config['norm'] = Normalize(vmin = np.min(self._data[LPU][config['ids'][0],:]),
                                                       vmax = np.max(self._data[LPU][config['ids'][0],:]),
                                                       clip = True)
                            
                    node_dict = self._graph[LPU].node
                    if str(LPU).startswith('input'):
                        latpositions = np.asarray([ node_dict[str(nid)]['lat'] \
                                                    for nid in range(len(node_dict)) \
                                                    if node_dict[str(nid)]['extern'] ])
                        longpositions = np.asarray([ node_dict[str(nid)]['long'] \
                                                     for nid in range(len(node_dict)) \
                                                     if node_dict[str(nid)]['extern'] ])
                    else:
                        latpositions = np.asarray([ node_dict[str(nid)]['lat']
                                                    for nid in config['ids'][0] ])
                        longpositions = np.asarray([ node_dict[str(nid)]['long']
                                                     for nid in config['ids'][0] ])
                    xx = np.cos(longpositions) * np.sin(latpositions)
                    yy = np.sin(longpositions) * np.sin(latpositions)
                    zz = np.cos(latpositions)
                    config['positions'] = (xx, yy, zz)
                    colors = griddata(config['positions'], self._data[LPU][config['ids'][0],0],
                                      self._dome_pos_flat, 'nearest').reshape(self._dome_arr_shape)
                    colors = config['norm'](colors).data
                    colors = np.tile(np.reshape(colors,
                                                [self._dome_arr_shape[0],self._dome_arr_shape[1],1])
                                     ,[1,1,4])
                    colors[:,:,3] = 1.0
                    config['handle'].plot_surface(self._dome_pos[0], self._dome_pos[1],
                                                  self._dome_pos[2], rstride=1, cstride=1,
                                                  facecolors=colors, antialiased=False,
                                                  shade=False)
                    
                for key in config.iterkeys():
                    if key not in keywds:
                        try:
                            self._set_wrapper(self.axarr[ind],key, config[key])
                        except:
                            pass
                        try:
                            self._set_wrapper(config['handle'],key, config[key])
                        except:
                            pass
                
                if config['type']<3:
                    config['handle'].axes.set_xticks([])
                    config['handle'].axes.set_yticks([])

            if self.suptitle is not None:
                self.f.suptitle(self._title, fontsize=self._fontsize+1, x=0.5,y=0.03, weight='bold')

        plt.tight_layout()

        if self.out_filename:
            if self.FFMpeg is None:
                if which(matplotlib.rcParams['animation.ffmpeg_path']):
                    self.writer = FFMpegFileWriter(fps=self.fps, codec=self.codec)
                elif which(matplotlib.rcParams['animation.avconv_path']):
                    self.writer = AVConvFileWriter(fps=self.fps, codec=self.codec)
                else:
                    raise RuntimeError('cannot find ffmpeg or avconv')
            elif self.FFMpeg:
                if which(matplotlib.rcParams['animation.ffmpeg_path']):
                    self.writer = FFMpegFileWriter(fps=self.fps, codec=self.codec)
                else:
                    raise RuntimeError('cannot find ffmpeg')
            else:
                if which(matplotlib.rcParams['animation.avconv_path']):
                    self.writer = AVConvFileWriter(fps=self.fps, codec=self.codec)
                else:
                    raise RuntimeError('cannot find avconv')

            # Use the output file to determine the name of the temporary frame
            # files so that two concurrently run visualizations don't clobber
            # each other's frames:
            self.writer.setup(self.f, self.out_filename, dpi=80,
                              frame_prefix=os.path.splitext(self.out_filename)[0]+'_')
            self.writer.frame_format = 'png'
            self.writer.grab_frame()
        else:
            self.f.show()
Ejemplo n.º 51
0
filteredDataGyro = [value]

angle = value
angleData = []


for i in range(18000000):
	value = gyro.getAccelX() * 90.0
	accel = value
	data.append(value)
	filteredData.append(0.9 * filteredData[-1] + 0.1 * value) 
	value = gyro.getGyroY() * -1.0
	dataGyro.append(value)
	filteredDataGyro.append(0.9 * filteredDataGyro[-1] + 0.1 * value) 
	angle = (0.9) * (angle + value * 0.01) + (0.1 * filteredData[-1])
	angleData.append(angle)
	if not i % 80:
		plt.delaxes()
		#plt.plot(data, color="black")
		plt.plot(filteredData, color="red")
		#plt.plot(dataGyro, color="black")
		#plt.plot(filteredDataGyro, color="cyan")
		#plt.plot(angleData, color="green")
		plt.draw()
		print str(angle)
	else:
		time.sleep(0.01)

if plt.waitforbuttonpress(0.01) != None:
	quit()