#!/usr/bin/env python # coding: utf-8 # Trying to figure out the shape of information entropy in 3D. # # 2D case is clear as seen in https://en.wikipedia.org/wiki/Entropy_(information_theory) # In[1]: ''' ====================== 3D surface (color map) ====================== Demonstrates plotting a 3D surface colored with the coolwarm color map. The surface is made opaque by using antialiased=False. Also demonstrates using the LinearLocator and custom formatting for the z axis tick labels. ''' from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import numpy as np get_ipython().run_line_magic('matplotlib', 'notebook') # In[4]: fig = plt.figure() ax = fig.gca(projection='3d') Xs = np.arange(0.01, 1, 0.01) Ys = np.arange(0.01, 1, 0.01) Xs, Ys = np.meshgrid(Xs, Ys) Z = - (Xs * np.log(Xs) + Ys * np.log(Ys) + (1 - Xs - Ys) * np.log(1 - Xs - Ys)) ax.contour(Xs, Ys, Z) # ax.plot_surface(Xs, Ys, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False) # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[14]: res = [] p1s = [] p2s = [] zs = [] for p1 in np.arange(0.01, 1, 0.01): for p2 in np.arange(0.01, 1 - p1, 0.01): p3 = 1 - p1 - p2 z = - np.sum(p1 * np.log(p1) + p2 * np.log(p2) + p3 * np.log(p3)) p1s.append(p1) p2s.append(p2) zs.append(z) p1s = np.array(p1s) p2s = np.array(p2s) zs = np.array(zs) # Y = np.arange(0, X, 0.01) # X, Y = np.meshgrid(X, Y) # Z = np.sum(X * np.log(X) + Y * np.log(Y) + (1 - X - Y) * np.log(1 - X - Y)) # # Plot the surface. # surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, # linewidth=0, antialiased=False) # # Customize the z axis. # ax.set_zlim(-1.01, 1.01) # ax.zaxis.set_major_locator(LinearLocator(10)) # ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) # # Add a color bar which maps values to colors. # fig.colorbar(surf, shrink=0.5, aspect=5) # plt.show() # In[15]: p1s.shape # In[16]: p2s.shape # In[17]: zs.shape # In[18]: X, Y = np.meshgrid(p1s, p2s) # In[19]: X.shape # In[31]: # In[ ]: # In[ ]: # In[ ]: # In[20]: np.meshgrid([1, 2], [3, 4]) # In[ ]: