Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chris suggested this PR for plotting this 3d heat map type plot. #34

Merged
merged 5 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion cmeutils/plotting.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import matplotlib.pyplot as plt
import numpy as np


def get_histogram(data, normalize=False, bins="auto"):
"""Bins a 1-D array of data into a histogram using
the numpy.histogram method.
Expand Down Expand Up @@ -31,3 +31,63 @@ def get_histogram(data, normalize=False, bins="auto"):
bin_widths = np.diff(bin_borders)
bin_centers = bin_borders[:-1] + bin_widths / 2
return bin_centers, bin_heights

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)

fig = threedplot(x,y,z)
fig.show()

Parameters
----------

x,y,z : list of int/floats

xlabel, ylabel, zlabel : str

plot_name : str


'''
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)

return fig
8 changes: 8 additions & 0 deletions cmeutils/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pytest

from cmeutils.plotting import get_histogram
from cmeutils.plotting import threedplot

from base_test import BaseTest


Expand All @@ -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)