Home > Back-end >  How to set in 3d plot the axis values to specific values
How to set in 3d plot the axis values to specific values

Time:06-06

I want to generate a heatcube. The axis are specific values for 3 different parameters and the actual value of a point (x,y,z) is a goodness of fit value. That means (x,y,z) should be read as categorical variables. However, I struggle to get the axis on the right scale so that it only considers the given values to be plotted and not a continuum of points

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')
    
lam = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 1, 10]
alpha = [0.0001, 0.001, 0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2]
delta = [0, 0.000001, 0.00001, 0.0001, 0.001, 0.01, 1, 10]
    
X, Y, Z = np.meshgrid(lam, alpha, delta)
    
c = np.random.randn(len(lam), len(alpha), len(delta))
ax.scatter(X,Y,Z, c=c,cmap="brg")
plt.show()

which gives a misleading picture:

enter image description here

As one can see the plot has by far too large spaces on the different axis. I would like to have only points shown the actual values of lam, alpha, delta like in this plot

enter image description here

Better solution (maybe thats what you wanted after all) is to make meshgrid of indices and substitute labels of ticks:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

lam = [0.000001, 0.00001, 0.0001, 0.001, 0.01, 1, 10]
alpha = [0.0001, 0.001, 0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2]
delta = [0, 0.000001, 0.00001, 0.0001, 0.001, 0.01, 1, 10]

# X, Y, Z = np.meshgrid(lam, alpha, delta)
X, Y, Z = np.meshgrid(np.arange(len(lam)), np.arange(len(alpha)), np.arange(len(delta)))

c = np.random.randn(len(lam), len(alpha), len(delta))
ax.scatter(X, Y, Z, c=c, cmap="brg",)
ax.set_xticklabels(lam)
ax.set_yticklabels(alpha)
ax.set_zticklabels(delta)
plt.show()

enter image description here

  • Related