From 0cf8e39b2b5e9c4e6fb30b88f180f73ca48dec80 Mon Sep 17 00:00:00 2001
From: Nikolai Hartmann <Nikolai.Hartmann@physik.uni-muenchen.de>
Date: Tue, 31 Jul 2018 18:28:23 +0200
Subject: [PATCH] switching pixel pots to pcolormesh

---
 plotting.py | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/plotting.py b/plotting.py
index 50e5888..4340f7a 100644
--- a/plotting.py
+++ b/plotting.py
@@ -6,7 +6,7 @@ import math
 import matplotlib.pyplot as plt
 import matplotlib.colors
 from matplotlib.ticker import LogFormatter
-from mpl_toolkits.axes_grid1 import ImageGrid
+from mpl_toolkits.axes_grid1 import ImageGrid, make_axes_locatable
 import numpy as np
 
 from .keras_visualize_activations.read_activations import get_activations
@@ -48,7 +48,9 @@ def plot_NN_vs_var_2D(plotname, means,
                       var2_label=None,
                       logscale=False,
                       ncontours=20,
-                      black_contourlines=False):
+                      only_pixels=False,
+                      black_contourlines=False,
+                      cmap="inferno"):
 
     print("Creating varied events (2d)")
     # example: vary var1 vs var2
@@ -79,14 +81,23 @@ def plot_NN_vs_var_2D(plotname, means,
     zmax = np.max(scores)
 
     if logscale:
+        if zmin <= 0:
+            zmin = 1e-5
+            print("Setting zmin to {}".format(zmin))
         lvls = np.logspace(math.log10(zmin), math.log10(zmax), ncontours)
-        pcm = ax.contourf(sequence1, sequence2, scores, levels=lvls, norm=matplotlib.colors.LogNorm(vmin=zmin, vmax=zmax))
+        if only_pixels:
+            pcm = ax.pcolormesh(sequence1, sequence2, scores, norm=matplotlib.colors.LogNorm(vmin=zmin, vmax=zmax), cmap=cmap)
+        else:
+            pcm = ax.contourf(sequence1, sequence2, scores, levels=lvls, norm=matplotlib.colors.LogNorm(vmin=zmin, vmax=zmax), cmap=cmap)
         if black_contourlines:
             ax.contour(sequence1, sequence2, scores, levels=lvls, colors="k", linewidths=1)
         l_f = LogFormatter(10, labelOnlyBase=False, minor_thresholds=(np.inf, np.inf))
         cbar = fig.colorbar(pcm, ax=ax, extend='max', ticks=lvls, format=l_f)
     else:
-        pcm = ax.contourf(sequence1, sequence2, scores, ncontours, norm=matplotlib.colors.Normalize(vmin=0, vmax=1))
+        if only_pixels:
+            pcm = ax.pcolormesh(sequence1, sequence2, scores, norm=matplotlib.colors.Normalize(vmin=zmin, vmax=zmax), cmap=cmap)
+        else:
+            pcm = ax.contourf(sequence1, sequence2, scores, ncontours, norm=matplotlib.colors.Normalize(vmin=zmin, vmax=zmax), cmap=cmap)
         if black_contourlines:
             ax.contour(sequence1, sequence2, scores, ncontours, colors="k", linewidths=1)
         cbar = fig.colorbar(pcm, ax=ax, extend='max')
@@ -107,7 +118,9 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
                           var1_label=None,
                           var2_label=None,
                           zrange=None, logz=False,
-                          plot_last_layer=False):
+                          plot_last_layer=False,
+                          log_default_ymin=1e-5,
+                          cmap="inferno"):
 
     "Similar to plot_NN_vs_var_2D, but creates a grid of plots for all neurons."
 
@@ -130,8 +143,6 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
 
     acts = get_activations(model, events, print_shape_only=True)
 
-    aspect = (var1_vals[-1]-var1_vals[0])/(var2_vals[-1]-var2_vals[0])
-
     if plot_last_layer:
         n_neurons = [len(i[0]) for i in acts]
     else:
@@ -142,8 +153,11 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
     fig = plt.figure(1, figsize=nrows_ncols)
     grid = ImageGrid(fig, 111, nrows_ncols=nrows_ncols[::-1], axes_pad=0,
                      label_mode="1",
+                     aspect=False,
                      cbar_location="top",
-                     cbar_mode="single",)
+                     cbar_mode="single",
+                     cbar_pad=.2,
+                     cbar_size="5%",)
     grid_array = np.array(grid)
     grid_array = grid_array.reshape(*nrows_ncols[::-1])
 
@@ -158,11 +172,10 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
     output_max_default = 1
 
     if global_min <= 0 and logz:
-        min_exponent = -5
-        global_min = 10**min_exponent
-        output_min_default = global_min
-        print("Changing global_min to {}".format(global_min))
+        global_min = log_default_ymin
+        print("Changing global_min to {}".format(log_default_ymin))
 
+    ims = []
     for layer in range(layers):
         for neuron in range(len(acts[layer][0])):
             acts_neuron = acts[layer][:,neuron]
@@ -179,12 +192,13 @@ def plot_NN_vs_var_2D_all(plotname, model, means,
                     extra_opts["norm"] = norm(vmin=zrange[0], vmax=zrange[1])
                 else:
                     extra_opts["norm"] = norm(vmin=global_min, vmax=global_max)
-            im = ax.imshow(acts_neuron, origin="lower", extent=[var1_vals[0], var1_vals[-1], var2_vals[0], var2_vals[-1]], aspect=aspect, cmap="jet", **extra_opts)
+            im = ax.pcolormesh(var1_vals, var2_vals, acts_neuron, cmap=cmap, linewidth=0, rasterized=True, **extra_opts)
+            ax.set_facecolor("black")
             if var1_label is not None:
                 ax.set_xlabel(var1_label)
             if var2_label is not None:
                 ax.set_ylabel(var2_label)
-            ax.text(0., 0.5, "{}, {}".format(layer, neuron), transform=ax.transAxes)
+            ax.text(0., 0.5, "{}, {}".format(layer, neuron), transform=ax.transAxes, color="white")
 
     cb = fig.colorbar(im, cax=grid[0].cax, orientation="horizontal")
     cb.ax.xaxis.set_ticks_position('top')
-- 
GitLab