matplotlib 如何在3D条形图中可视化小值

ubby3x7f  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(128)

谁能帮我找到在下面的3D条形图中可视化小值的解决方案?

import matplotlib.pyplot as plt
import numpy as np
np.random.seed(10)

#data
time = [[148.64793017553907, 47.00162830693381, 1.3599472795213974, 1.0770502873829435, 0.2416407755443028, 0.051920437812805136],
        [100.7717864097111, 11.489065728868756, 0.5487183400562831, 0.12449462073189865, 0.15135425840105324, 0.030407779557364272],
        [10.223643741910418, 1.6037633759634835, 0.3846410546983991, 0.09999658720833912, 0.07089985779353546, 0.029794696399143727],
        [0.9271023046402703, 0.1371803828648158, 0.3223802804946899, 0.09767089911869589, 0.024286287171500026, 0.029627582005092024],
        [0.11088135128929497, 0.06808395726340152, 0.224113655090332, 0.08966402666909352, 0.022294637135096912, 0.029868837765284897]]

colors = ['r', 'g', 'b', 'y','m']
yticks = [1,2,3,4,5]
fig = plt.figure()
ax = fig.add_subplot(111 ,projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_yticks(yticks)
xs=np.array([1,2,3,4,5,6])
for i in range(5):
    ax.bar(xs, time[i], zs=i+1, zdir='y', color=colors[i], alpha=0.8)

plt.tight_layout()
plt.savefig("3d.png")
plt.show()

我曾尝试在matplotlib的3d条形图中可视化数据,但小值在图表中不可见。
我正在寻找解决方案,使小价值在图表中清楚地表示。

kknvjkwl

kknvjkwl1#

日志缩放存在log对于0不是有限的问题。您可以尝试使用不同的次线性缩放,例如平方根(或任何k〉1的k次方根)。然后,可以设置z标记标签,使其反映此缩放。下面是一个使用5次方根的例子(scalepower = 1/5)。

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np

# given the time array from the OP

scalepower = 1/5

colors = ['r', 'g', 'b', 'y','m']
yticks = [1,2,3,4,5]
fig = plt.figure()
ax = fig.add_subplot(111 ,projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_yticks(yticks)
xs=np.array([1,2,3,4,5,6])
for i in range(5):
    ax.bar(xs, np.array(time[i])**scalepower, zs=i+1, zdir='y', color=colors[i], alpha=0.8)

plt.tight_layout()
    
def format_ticks(z, pos):
    return f'{z**scalepower:.2f}'

formatter = ticker.FuncFormatter(format_ticks)
ax.zaxis.set_major_formatter(formatter)

plt.savefig("3d.png")
plt.show()

相关问题