0

I would like to replace some xticks with self-label ticks, my codes is the following:

from mpl_toolkits.mplot3d import Axes3D  
import matplotlib.pyplot as plt  
from matplotlib import cm  
from matplotlib.ticker import LinearLocator  
import numpy as np  
import matplotlib.colors  

fig = plt.figure()  
ax = fig.gca(projection='3d')  
X = np.arange(3)  
Z = np.arange(4)  
X, Z = np.meshgrid(X, Z)  

V1 = np.array([[0.5,0.5],[0,0.9],[0,0.9]])  
V2 = np.array([[0,0.5],[0,0.5],[0,0.5]])  
V3 = np.array([[0,0.5],[0,0.5],[0,0.5]])  
V_list = [V1,V2,V3]  

norm = matplotlib.colors.Normalize(vmin=0, vmax=1)  
for i in range(3):  
    ax.plot_surface(X, np.ones(shape=X.shape)+i, Z, facecolors=plt.cm.ocean(norm(V_list[i])), linewidth=0, edgecolor="black")  

x_ticks = [(X[i] + X[i+1])/2 for i in range(len(X)-1)]  
ax.set_xticks(x_ticks)  
ax.set_xticklabels(['a','b'])  
ax.set_xlabel('x')  
m = cm.ScalarMappable(cmap=plt.cm.ocean, norm=norm)  
m.set_array([])  
plt.colorbar(m)  
plt.show()  

These codes end up with ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() However, if I change

x_ticks = [(X[i] + X[i+1])/2 for i in range(len(X)-1)]  
ax.set_xticks(x_ticks)  
ax.set_xticklabels(['a','b'])  

to

ax.set_xticks([0.5,1.5])  
ax.set_xticklabels(['a','b'])

then, the codes work. I was wondering why this happened since [(X[i] + X[i+1])/2 for i in range(len(X)-1)] is the same list as [0.5,1.5] and how can I use [(X[i] + X[i+1])/2 for i in range(len(X)-1)] for set_xticks. Thanks in advance!

Kun

Kun Fang
  • 25
  • 7
  • 2
    This is what `x_ticks` contains: `[array([0., 1., 2.]), array([0., 1., 2.]), array([0., 1., 2.])]` Probably not what you expected. The reason is that `X` is an array of shape (4, 3). – Mr. T Jan 20 '21 at 17:43
  • 1
    Personally, I like this implementation of a rolling average: https://stackoverflow.com/a/54628145/8881141 – Mr. T Jan 20 '21 at 17:54
  • 1
    Oh..understand, I somehow ignore this X, Z = np.meshgrid(X, Z), thanks a lot for your help and information! – Kun Fang Jan 20 '21 at 19:44

0 Answers0