Python matplotlib.pyplot.hist2d() Examples

The following are 9 code examples of matplotlib.pyplot.hist2d(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module matplotlib.pyplot , or try the search function .
Example #1
Source File: bdk_demo.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def run_synthetic_SGLD():
    theta1 = 0
    theta2 = 1
    sigma1 = numpy.sqrt(10)
    sigma2 = 1
    sigmax = numpy.sqrt(2)
    X = load_synthetic(theta1=theta1, theta2=theta2, sigmax=sigmax, num=100)
    minibatch_size = 1
    total_iter_num = 1000000
    lr_scheduler = SGLDScheduler(begin_rate=0.01, end_rate=0.0001, total_iter_num=total_iter_num,
                                 factor=0.55)
    optimizer = mx.optimizer.create('sgld',
                                    learning_rate=None,
                                    rescale_grad=1.0,
                                    lr_scheduler=lr_scheduler,
                                    wd=0)
    updater = mx.optimizer.get_updater(optimizer)
    theta = mx.random.normal(0, 1, (2,), mx.cpu())
    grad = nd.empty((2,), mx.cpu())
    samples = numpy.zeros((2, total_iter_num))
    start = time.time()
    for i in xrange(total_iter_num):
        if (i + 1) % 100000 == 0:
            end = time.time()
            print("Iter:%d, Time spent: %f" % (i + 1, end - start))
            start = time.time()
        ind = numpy.random.randint(0, X.shape[0])
        synthetic_grad(X[ind], theta, sigma1, sigma2, sigmax, rescale_grad=
        X.shape[0] / float(minibatch_size), grad=grad)
        updater('theta', grad, theta)
        samples[:, i] = theta.asnumpy()
    plt.hist2d(samples[0, :], samples[1, :], (200, 200), cmap=plt.cm.jet)
    plt.colorbar()
    plt.show() 
Example #2
Source File: bdk_demo.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
def run_synthetic_SGLD():
    theta1 = 0
    theta2 = 1
    sigma1 = numpy.sqrt(10)
    sigma2 = 1
    sigmax = numpy.sqrt(2)
    X = load_synthetic(theta1=theta1, theta2=theta2, sigmax=sigmax, num=100)
    minibatch_size = 1
    total_iter_num = 1000000
    lr_scheduler = SGLDScheduler(begin_rate=0.01, end_rate=0.0001, total_iter_num=total_iter_num,
                                 factor=0.55)
    optimizer = mx.optimizer.create('sgld',
                                    learning_rate=None,
                                    rescale_grad=1.0,
                                    lr_scheduler=lr_scheduler,
                                    wd=0)
    updater = mx.optimizer.get_updater(optimizer)
    theta = mx.random.normal(0, 1, (2,), mx.cpu())
    grad = nd.empty((2,), mx.cpu())
    samples = numpy.zeros((2, total_iter_num))
    start = time.time()
    for i in xrange(total_iter_num):
        if (i + 1) % 100000 == 0:
            end = time.time()
            print("Iter:%d, Time spent: %f" % (i + 1, end - start))
            start = time.time()
        ind = numpy.random.randint(0, X.shape[0])
        synthetic_grad(X[ind], theta, sigma1, sigma2, sigmax, rescale_grad=
        X.shape[0] / float(minibatch_size), grad=grad)
        updater('theta', grad, theta)
        samples[:, i] = theta.asnumpy()
    plt.hist2d(samples[0, :], samples[1, :], (200, 200), cmap=plt.cm.jet)
    plt.colorbar()
    plt.show() 
Example #3
Source File: vistools.py    From rl_swiss with MIT License 5 votes vote down vote up
def plot_2dhistogram(x, y, num_bins, title, save_path, ax_lims=None):
    fig, ax = plt.subplots(1)
    ax.set_title(title)
    plt.hist2d(x, y, bins=num_bins)
    if ax_lims is not None:
        ax.set_xlim(ax_lims[0])
        ax.set_ylim(ax_lims[1])
    ax.set_aspect('equal')    
    plt.savefig(save_path, bbox_inches='tight')
    plt.close() 
Example #4
Source File: utils.py    From segmentator with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def prep_2D_hist(ima, gra, discard_zeros=True):
    """Prepare 2D histogram related variables.

    Parameters
    ----------
    ima : np.ndarray
        First image, which is often the intensity image (eg. T1w).
    gra : np.ndarray
        Second image, which is often the gradient magnitude image
        derived from the first image.

    Returns
    -------
    counts : integer
    volHistH : TODO
    d_min : float
        Minimum of the first image.
    d_max : float
        Maximum of the first image.
    nr_bins : integer
        Number of one dimensional bins (not the pixels).
    bin_edges : TODO

    Notes
    -----
    This function is modularized to be called from the terminal.

    """
    if discard_zeros:
        gra = gra[~np.isclose(ima, 0)]
        ima = ima[~np.isclose(ima, 0)]
    d_min, d_max = np.round(np.nanpercentile(ima, [0, 100]))
    nr_bins = int(d_max - d_min)
    bin_edges = np.arange(d_min, d_max+1)
    counts, _, _, volHistH = plt.hist2d(ima, gra, bins=bin_edges, cmap='Greys')
    return counts, volHistH, d_min, d_max, nr_bins, bin_edges 
Example #5
Source File: viz_utils.py    From Benchmarks with MIT License 5 votes vote down vote up
def plot_density_observed_vs_predicted(Ytest, Ypred, pred_name=None, figprefix=None):
    """Functionality to plot a 2D histogram of the distribution of observed (ground truth)
       values vs. predicted values. The plot generated is stored in a png file.

    Parameters
    ----------
    Ytest : numpy array
      Array with (true) observed values
    Ypred : numpy array
      Array with predicted values.
    pred_name : string
      Name of data colum or quantity predicted (e.g. growth, AUC, etc.)
    figprefix : string
      String to prefix the filename to store the figure generated.
      A '_density_predictions.png' string will be appended to the
      figprefix given.
    """

    xbins = 51

    fig = plt.figure(figsize=(24,18)) # (30,16)
    ax = plt.gca()
    plt.rc('xtick', labelsize=16)    # fontsize of the tick labels
    ax.plot([Ytest.min(), Ytest.max()], [Ytest.min(), Ytest.max()], 'r--', lw=4.)
    plt.hist2d(Ytest, Ypred, bins=xbins, norm=LogNorm())
    cb = plt.colorbar()
    ax.set_xlabel('Observed ' + pred_name, fontsize=38, labelpad=15.)
    ax.set_ylabel('Mean ' + pred_name + ' Predicted', fontsize=38, labelpad=15.)
    ax.axis([Ytest.min()*0.98, Ytest.max()*1.02, Ytest.min()*0.98, Ytest.max()*1.02])
    plt.setp(ax.get_xticklabels(), fontsize=32)
    plt.setp(ax.get_yticklabels(), fontsize=32)
    cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=28)
    plt.grid(True)
    plt.savefig(figprefix + '_density_predictions.png')
    plt.close()
    print('Generated plot: ', figprefix + '_density_predictions.png') 
Example #6
Source File: viz_utils.py    From Benchmarks with MIT License 5 votes vote down vote up
def plot_2d_density_sigma_vs_error(sigma, yerror, method=None, figprefix=None):
    """Functionality to plot a 2D histogram of the distribution of 
       the standard deviations computed for the predictions vs. the
       computed errors (i.e. values of observed - predicted).
       The plot generated is stored in a png file.

    Parameters
    ----------
    sigma : numpy array
      Array with standard deviations computed.
    yerror : numpy array
      Array with errors computed (observed - predicted).
    method : string
      Method used to comput the standard deviations (i.e. dropout, 
      heteroscedastic, etc.).
    figprefix : string
      String to prefix the filename to store the figure generated.
      A '_density_sigma_error.png' string will be appended to the 
      figprefix given.
    """
    
    xbins = 51
    ybins = 31

    fig = plt.figure(figsize=(24,12)) # (30,16)
    ax = plt.gca()
    plt.rc('xtick', labelsize=16)    # fontsize of the tick labels
    plt.hist2d(sigma, yerror, bins=[xbins,ybins], norm=LogNorm())
    cb = plt.colorbar()
    ax.set_xlabel('Sigma (' + method + ')', fontsize=38, labelpad=15.)
    ax.set_ylabel('Observed - Mean Predicted', fontsize=38, labelpad=15.)
    ax.axis([sigma.min()*0.98, sigma.max()*1.02, -yerror.max(), yerror.max()])
    plt.setp(ax.get_xticklabels(), fontsize=28)
    plt.setp(ax.get_yticklabels(), fontsize=28)
    cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=22)
    plt.grid(True)
    plt.savefig(figprefix + '_density_sigma_error.png')
    plt.close()
    print('Generated plot: ', figprefix + '_density_sigma_error.png') 
Example #7
Source File: graph_func.py    From MMD-GAN with Apache License 2.0 5 votes vote down vote up
def hist2d(self, x=None, x0=None, x1=None, bins=10, data_range=None, log_norm=False, fig_def=None):
        """

        :param x: either x or x0, x1 is given
        :param x0:
        :param x1:
        :param bins:
        :param data_range:
        :param log_norm: if log normalization is used
        :param fig_def:
        :return:
        """
        from matplotlib.colors import LogNorm
        # check inputs
        self._reset_fig_def_(fig_def)
        if x is not None:
            x0 = x[:, 0]
            x1 = x[:, 1]
        if data_range is None:
            data_range = [[-1.0, 1.0], [-1.0, 1.0]]
        num_instances = x0.shape[0]
        if num_instances > 200:
            count_min = np.ceil(num_instances/bins/bins*0.05)  # bins under this value will not be displayed
            print('hist2d; counts under {} will be ignored.'.format(count_min))
        else:
            count_min = None

        # plot figure
        self.new_figure()
        if log_norm:
            plt.hist2d(x0, x1, bins, range=data_range, norm=LogNorm(), cmin=count_min)
        else:
            plt.hist2d(x0, x1, bins, range=data_range, cmin=count_min)
        self._add_figure_labels_()
        plt.colorbar()
        self.show_figure() 
Example #8
Source File: bdk_demo.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def run_synthetic_SGLD():
    theta1 = 0
    theta2 = 1
    sigma1 = numpy.sqrt(10)
    sigma2 = 1
    sigmax = numpy.sqrt(2)
    X = load_synthetic(theta1=theta1, theta2=theta2, sigmax=sigmax, num=100)
    minibatch_size = 1
    total_iter_num = 1000000
    lr_scheduler = SGLDScheduler(begin_rate=0.01, end_rate=0.0001, total_iter_num=total_iter_num,
                                 factor=0.55)
    optimizer = mx.optimizer.create('sgld',
                                    learning_rate=None,
                                    rescale_grad=1.0,
                                    lr_scheduler=lr_scheduler,
                                    wd=0)
    updater = mx.optimizer.get_updater(optimizer)
    theta = mx.random.normal(0, 1, (2,), mx.cpu())
    grad = nd.empty((2,), mx.cpu())
    samples = numpy.zeros((2, total_iter_num))
    start = time.time()
    for i in xrange(total_iter_num):
        if (i + 1) % 100000 == 0:
            end = time.time()
            print("Iter:%d, Time spent: %f" % (i + 1, end - start))
            start = time.time()
        ind = numpy.random.randint(0, X.shape[0])
        synthetic_grad(X[ind], theta, sigma1, sigma2, sigmax, rescale_grad=
        X.shape[0] / float(minibatch_size), grad=grad)
        updater('theta', grad, theta)
        samples[:, i] = theta.asnumpy()
    plt.hist2d(samples[0, :], samples[1, :], (200, 200), cmap=plt.cm.jet)
    plt.colorbar()
    plt.show() 
Example #9
Source File: viz_utils.py    From Benchmarks with MIT License 4 votes vote down vote up
def plot_histogram_error_per_sigma(sigma, yerror, method=None, figprefix=None):
    """Functionality to plot a 1D histogram of the distribution of
       computed errors (i.e. values of observed - predicted) observed 
       for specific values of standard deviations computed. The range of
       standard deviations computed is split in xbins values and the 
       1D histograms of error distributions for the smallest six
       standard deviations are plotted.
       The plot generated is stored in a png file.

    Parameters
    ----------
    sigma : numpy array
      Array with standard deviations computed.
    yerror : numpy array
      Array with errors computed (observed - predicted).
    method : string
      Method used to comput the standard deviations (i.e. dropout, 
      heteroscedastic, etc.).
    figprefix : string
      String to prefix the filename to store the figure generated.
      A '_histogram_error_per_sigma.png' string will be appended to 
      the figprefix given.
    """
    
    xbins = 21
    ybins = 31

    H, xedges, yedges, img = plt.hist2d(sigma, yerror,# normed=True,
                                        bins=[xbins,ybins])

    fig = plt.figure(figsize=(14,16))
    legend = []
    for ii in range(6):#(H.shape[0]):
        if ii is not 1:
            plt.plot(yedges[0:H.shape[1]], H[ii,:]/np.sum(H[ii,:]), marker='o',
                 markersize=12, lw=6.)
        legend.append(str((xedges[ii] + xedges[ii+1])/2))
    plt.legend(legend, fontsize=16)
    ax = plt.gca()
    plt.title('Error Dist. per Sigma for ' + method, fontsize=40)
    ax.set_xlabel('Observed - Mean Predicted', fontsize=38, labelpad=15.)
    ax.set_ylabel('Density', fontsize=38, labelpad=15.)
    plt.setp(ax.get_xticklabels(), fontsize=28)
    plt.setp(ax.get_yticklabels(), fontsize=28)
    plt.grid(True)
    plt.savefig(figprefix + '_histogram_error_per_sigma.png')
    plt.close()
    print('Generated plot: ', figprefix + '_histogram_error_per_sigma.png')