matplotlib 制作双截面/双色3D条形图?

pieyvz9o  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(795)
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4. # create some sample data
  5. x = np.array([1, 2, 3])
  6. y = np.array([4, 5, 6])
  7. z1 = np.array([1, 2, 3])
  8. z2 = np.array([4, 5, 6])
  9. z3 = np.array([7, 8, 9])
  10. color1 = np.array(['r', 'g', 'b'])
  11. color2 = np.array(['y', 'm', 'c'])
  12. # create a figure and a set of subplots
  13. fig = plt.figure()
  14. ax = fig.add_subplot(111, projection='3d')
  15. # plot the first set of bars
  16. for i in range(len(x)):
  17. ax.bar(x[i], z1[i], y[i], zdir='y', color=color1[i], alpha=0.8)
  18. ax.bar(x[i], z1[i] - z1[i], y[i], zdir='y', color=color2[i], alpha=0.8)
  19. # plot the second set of bars
  20. for i in range(len(x)):
  21. ax.bar(x[i], z2[i], y[i]+0.5, zdir='y', color=color1[i], alpha=0.8)
  22. ax.bar(x[i], z2[i] - z2[i], y[i]+0.5, zdir='y', color=color2[i], alpha=0.8)
  23. # plot the third set of bars
  24. for i in range(len(x)):
  25. ax.bar(x[i], z3[i], y[i]+1.0, zdir='y', color=color1[i], alpha=0.8)
  26. ax.bar(x[i], z3[i] - z3[i], y[i]+1.0, zdir='y', color=color2[i], alpha=0.8)
  27. # set the axis labels and title
  28. ax.set_xlabel('X axis')
  29. ax.set_ylabel('Y axis')
  30. ax.set_zlabel('Z axis')
  31. ax.set_title('3D Bar Chart')
  32. # show the plot
  33. plt.show()

Running this code in VSCode on Windows 11 (Python 3.10) results in the following error:
Traceback (most recent call last): File "c:\Users\16168\Documents\delta-scan-master\delta-scan-master\test\eval_test_display.py", line 73, in ax.bar(x[i], z1[i], y[i], zdir='y', color=color1[i], alpha=0.8) File "C:\Users\16168\Documents\delta-scan-master\delta-scan-master\env\lib\site-packages\matplotlib_init_.py", line 1459, in inner return func(ax, *map(sanitize_sequence, args), **kwargs) File "C:\Users\16168\Documents\delta-scan-master\delta-scan-master\env\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 2318, in bar zs = np.broadcast_to(zs, len(left)) TypeError: object of type 'numpy.int32' has no len()
I'm trying to create a set of 3d bar charts where each bar chart is bicolored (representing two different values along the vertical axis). I don't understand why the error is in line 73, rather than 72 where the len() function appears. I read the error as essentially saying that I'm calling the length function on an integer data type - but x is (should be?) a numpy array as declared above. Any ideas? Or is there a mismatch between the type of i and the type returned by len(x)?

trnvg8h3

trnvg8h31#

  • 第一个ax.bar()调用说明,但所有其他调用都会出现错误。*
  1. for i in range(len(x)):
  2. ax.bar(x[i], z1[i], y[i], zdir='y', color=color1[i], alpha=0.8)

我不明白为什么错误出现在第73行,而不是len()函数出现的第72行。
错误确实不在for i in range(len(x))上,而是在ax.bar()调用上。
Axes3D.bar需要一个xyz值的列表,但您传递的是单个标量值x[i]z[i]y[i]
代替for循环,您可以直接传递值列表(color也是如此)。它将为每个i创建一个条形图。

  1. ax.bar(x, z1, y, zdir='y', color=color1, alpha=0.8)

补充说明:

  • 这将在3D轴上创建一个2D条。如果您想要一个实际的3D条,请使用ax.bar3d
  • 对于每组条形图,在第二个ax.bar调用中,作为第二个参数zX - zX传递,这可能是错误的(0高度条形图;不可见)。

修复ax.bar()调用,这是您的图目前的样子:

  1. # [...]
  2. # create a figure and a set of subplots
  3. fig = plt.figure()
  4. ax = fig.add_subplot(111, projection='3d')
  5. # plot the first set of bars
  6. ax.bar(x, z1, y, zdir='y', color=color1, alpha=0.8)
  7. ax.bar(x, z1 - z1, y, zdir='y', color=color2, alpha=0.8)
  8. # plot the second set of bars
  9. ax.bar(x, z2, y+0.5, zdir='y', color=color1, alpha=0.8)
  10. ax.bar(x, z2 - z2, y+0.5, zdir='y', color=color2, alpha=0.8)
  11. # plot the third set of bars
  12. ax.bar(x, z3, y + 1.0, zdir='y', color=color1, alpha=0.8)
  13. ax.bar(x, z3 - z3, y + 1.0, zdir='y', color=color2, alpha=0.8)
  14. # [...]

展开查看全部

相关问题