首页 > 解决方案 > matplotlib bar3d 中的可视化错误

问题描述

我正在 matplotlib 中绘制 3D 条形图。

我发现了一些怪癖,其中当相邻条的条高度很大时,可视化是错误的。有人知道对此有任何解决方案吗?

隐形酒吧

使用此示例代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.cm as cm   

power = 5

n = 5
x = [1, 2, 3, 1, 2, 3, 1, 2, 3]  # x coordinates of each bar
x = []
x = [x+1 for x in range(n)]
x = x * n 
y = []
for item in range(n):
    y = y + [item]*n
#y = f  # y coordinates of each bar
z = [0] * (n**2)  # z coordinates of each bar
dx = [1] * (n**2)  # Width of each bar
dy = [1] * (n**2)  # Depth of each bar
dz = list(range(n**2))
dz = [x**power for x in dz] 
fig = plt.figure(dpi=400)          #create a canvas, tell matplotlib it's 3d
ax = fig.add_subplot(111, projection='3d')

max_height = np.max(dz)   # get range of colorbars so we can normalize
min_height = np.min(dz)
# scale each z to [0,1], and get their rgb values
rgba = [cmap((k-min_height)/max_height) for k in dz] 

ax.bar3d(x, y, z, dx, dy, dz, color=rgba, zsort='average')
plt.title(r"plot title")
plt.xlabel("x axis")
plt.ylabel("y axis")
plt.show()

power变量设置为3,情节很好。在power=5处,您可以看到数据点 (5, 4) 不可见,显示后面的条形。我相信这是由于酒吧高度之间的巨大差异。当我用高但相似的高度进行测试并且问题不会发生时。

标签: matplotlibplotvisualizationmplot3d

解决方案


推荐阅读