I want to plot a surface 3D graph for my website with matplotlib, but I am getting an error. Here is the source code:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.array([ '4K', '8K' ])
Y = np.array([ 1, 4 ])
Z = np.array([[0.1925, 0.1848], [0.1807, 0.1966]])
ax.plot_wireframe(X, Y, Z)
plt.show()
The error I am getting is
Traceback (most recent call last):
File "wire3d.py", line 21, in <module>
ax.plot_wireframe(X, Y, Z)
File "/usr/local/lib/python3.8/dist-packages/matplotlib/_api/deprecation.py", line 415, in wrapper
return func(*inner_args, **inner_kwargs)
File "/usr/local/lib/python3.8/dist-packages/mpl_toolkits/mplot3d/axes3d.py", line 1908, in plot_wireframe
self.auto_scale_xyz(X, Y, Z, had_data)
File "/usr/local/lib/python3.8/dist-packages/mpl_toolkits/mplot3d/axes3d.py", line 652, in auto_scale_xyz
self.xy_dataLim.update_from_data_xy(
File "/usr/local/lib/python3.8/dist-packages/matplotlib/transforms.py", line 967, in update_from_data_xy
path = Path(xy)
File "/usr/local/lib/python3.8/dist-packages/matplotlib/path.py", line 129, in __init__
vertices = _to_unmasked_float_array(vertices)
File "/usr/local/lib/python3.8/dist-packages/matplotlib/cbook/__init__.py", line 1298, in _to_unmasked_float_array
return np.asarray(x, float)
ValueError: could not convert string to float: '4K'
What is the problem?
CodePudding user response:
You have to specify what 4K and 8K means:
X = np.array([ 4000, 8000 ])
If you are interest in show "4K" and "8K" in the x axis you can add ax.set_xticks(X, [ '4K', '8K' ])
before showing the figure.
This is the right code:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.array([ 4000, 8000 ])
Y = np.array([ 1, 4 ])
Z = np.array([[0.1925, 0.1848], [0.1807, 0.1966]])
ax.plot_wireframe(X, Y, Z)
ax.set_xticks(X, [ '4K', '8K' ])
plt.show()