# -*- coding: utf-8 -*-
"""
Based on a script from phanikumar.ds@gmail.com
"""
import numpy as np
import matplotlib as mpl
mpl.use("Qt5Agg") # or TkAgg or any other available backend
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.ion()
# Read the selected file: Numpy knows rather well how to do that :)
X, Y, Z = np.loadtxt('./Plasma.csv', delimiter=",", unpack=True)
# Getting ready for the dispaly
fig = plt.figure(num="plasma_v2", clear=True)
ax = fig.add_subplot(1, 1, 1, projection='3d')
#ax = fig.add_axes(MyAxes3D(ax, 'left'))
# Set the labels
ax.set_xlabel('X (mm)')
ax.set_ylabel('Y (mm)')
ax.set_zlabel('CPD (mV)')
# Setting the places properly...
#
# It would be nice if an mplot3d expert could tell if there is another way
# to set the z-axis label and tick on the left side (rather than the right
# one) that would not rely on private attributes...
#
#tmp_planes = ax.zaxis._PLANES
#ax.zaxis._PLANES = (tmp_planes[2], tmp_planes[3],
# tmp_planes[0], tmp_planes[1],
# tmp_planes[4], tmp_planes[5])
#
# NB: actually the approach above has issues with the grid lines...
# So instead I would lean towar rather bearing with the z-axis being
# labelled automatically, and possibly tweaking the azimut value to
# be in a position where the labels end up on the left side:
ax.azim *= -1 # to 'flip' the plot in azimut value, or use ax.azim = 60
# Reshape the data from 1D to 2D because of `ax.plot_surf` signature
# Find the correct shape (could also be done manually by looking at the file)
nb_unique_x = len(np.unique(X))
nb_unique_y = len(np.unique(Y))
shape_xy = (nb_unique_y, nb_unique_x)
xx = X.reshape(shape_xy)
yy = Y.reshape(shape_xy)
zz = Z.reshape(shape_xy)
# Surface plot
surf = ax.plot_surface(xx, yy, zz, cmap="inferno",
linewidth=0.8, # in points
edgecolor='tab:blue', # or "Blue" or any other color
)
cbar = fig.colorbar(surf, ax=ax, label="CPD (mV)")
fig.tight_layout()
fig.savefig(fig.get_label() + ".png")