From 94f848ed457696f63630277015f4e2d73d979aea Mon Sep 17 00:00:00 2001 From: jimmy Date: Wed, 9 Feb 2022 13:48:40 -0700 Subject: [PATCH 1/4] Chris suggested this PR for plotting this 3d heat map type plot. --- cmeutils/plotting.py | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 cmeutils/plotting.py diff --git a/cmeutils/plotting.py b/cmeutils/plotting.py new file mode 100644 index 0000000..e662b07 --- /dev/null +++ b/cmeutils/plotting.py @@ -0,0 +1,65 @@ +import matplotlib.pyplot as plt + +def threedplot( + x = [], + y = [], + z = [], + xlabel = "xlabel", + ylabel = "ylabel", + zlabel = "zlabel", + plot_name = "plot_name" + ): + + '''Plot a 3d heat map from 3 lists of numbers. This function is useful + for plotting a dependent variable as a function of two independent variables. + In the example below we use f(x,y)= -x^2 - y^2 +6 because it looks cool. + + Example + + ------- + + We create two indepent variables and a dependent variable in the z axis and + plot the result. Here z is the equation of an elliptic paraboloid. + + import random + + x = [] + for i in range(0,1000): + n = random.uniform(-20,20) + x.append(n) + + y = [] + for i in range(0,1000): + n = random.uniform(-20,20) + y.append(n) + + z = [] + for i in range(0,len(x)): + z.append(-x[i]**2 - y[i]**2 +6) + + threedplot(x,y,z) + + Parameters + + ---------- + + x,y,z : list of int + + xlabel, ylabel, zlabel : str + + plot_name : str + + + ''' + x = x + y = y + z = z + fig = plt.figure(figsize = (10, 10)) + ax = plt.axes(projection='3d') + ax.set_xlabel(xlabel,fontdict=dict(weight='bold'),fontsize=12) + ax.set_ylabel(ylabel,fontdict=dict(weight='bold'),fontsize=12) + ax.set_zlabel(zlabel,fontdict=dict(weight='bold'),fontsize=12) + p = ax.scatter(x, y, z, c=z, cmap='rainbow', linewidth=7); + plt.colorbar(p, pad = .1, aspect = 2.3) + fig.show() + fig.savefig(plot_name, bbox_inches = "tight", facecolor = "white") From bf1b39168612b899a381be25e2d9b1ccb5e2e3cf Mon Sep 17 00:00:00 2001 From: jimmy Date: Wed, 9 Feb 2022 14:13:39 -0700 Subject: [PATCH 2/4] addressed chris' comments --- cmeutils/plotting.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/cmeutils/plotting.py b/cmeutils/plotting.py index e662b07..506f189 100644 --- a/cmeutils/plotting.py +++ b/cmeutils/plotting.py @@ -1,9 +1,9 @@ import matplotlib.pyplot as plt def threedplot( - x = [], - y = [], - z = [], + x, + y, + z, xlabel = "xlabel", ylabel = "ylabel", zlabel = "zlabel", @@ -15,7 +15,6 @@ def threedplot( In the example below we use f(x,y)= -x^2 - y^2 +6 because it looks cool. Example - ------- We create two indepent variables and a dependent variable in the z axis and @@ -37,10 +36,10 @@ def threedplot( for i in range(0,len(x)): z.append(-x[i]**2 - y[i]**2 +6) - threedplot(x,y,z) + fig = threedplot(x,y,z) + fig.show() Parameters - ---------- x,y,z : list of int @@ -51,15 +50,12 @@ def threedplot( ''' - x = x - y = y - z = z - fig = plt.figure(figsize = (10, 10)) + fig = plt.figure(figsize = (10, 10), facecolor = 'white') ax = plt.axes(projection='3d') ax.set_xlabel(xlabel,fontdict=dict(weight='bold'),fontsize=12) ax.set_ylabel(ylabel,fontdict=dict(weight='bold'),fontsize=12) ax.set_zlabel(zlabel,fontdict=dict(weight='bold'),fontsize=12) p = ax.scatter(x, y, z, c=z, cmap='rainbow', linewidth=7); plt.colorbar(p, pad = .1, aspect = 2.3) - fig.show() - fig.savefig(plot_name, bbox_inches = "tight", facecolor = "white") + + return fig From 15bfa34b36c9ece591a3cecdef73757364b4b995 Mon Sep 17 00:00:00 2001 From: jimmy Date: Wed, 16 Feb 2022 11:41:22 -0700 Subject: [PATCH 3/4] added space to smooth merge --- cmeutils/plotting.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/cmeutils/plotting.py b/cmeutils/plotting.py index 506f189..2f8477b 100644 --- a/cmeutils/plotting.py +++ b/cmeutils/plotting.py @@ -1,5 +1,40 @@ import matplotlib.pyplot as plt + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + def threedplot( x, y, From df53cfc8c98533836b3a8a392862eb6d3d7655ee Mon Sep 17 00:00:00 2001 From: jimmy Date: Wed, 16 Feb 2022 11:54:58 -0700 Subject: [PATCH 4/4] added a test for threedplot --- cmeutils/tests/test_plotting.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cmeutils/tests/test_plotting.py b/cmeutils/tests/test_plotting.py index fdfe70d..0bc69a3 100644 --- a/cmeutils/tests/test_plotting.py +++ b/cmeutils/tests/test_plotting.py @@ -2,6 +2,8 @@ import pytest from cmeutils.plotting import get_histogram +from cmeutils.plotting import threedplot + from base_test import BaseTest @@ -16,3 +18,9 @@ def test_histogram_normalize(self): bin_c, bin_h = get_histogram(sample, normalize=True) assert all(bin_h <= 1) + def test_3dplot(self): + x = [1,2,3,4,5] + y = [1,2,3,4,5] + z = [1,2,3,4,5] + threedplot(x,y,z) +