使用堆积条形图时 Matplotlib 未正确绘制对数刻度

Matplotlib not plotting logscale properly when using stacked bar chart

我有 5 个列表,您可以在下面查看

always_failing_list_tt = [0.221,0.221,0.221, 0.221]
always_passing_list_tt = [0.335,0.335,0.335, 0.335]
all_test_tt = [108.281]
keep_list_tt = [93.104,85.011,76.111,70.932]
eppm_plot = [0,35482,70517,101074]

我想创建一个堆叠条形图,以便我绘制的第二个条形图与 all_test_tt 相比是 (always_passing_list_tt + always_failing_list_tt + keep_list_tt) 的组合

我使用下面的代码来做到这一点:

from matplotlib.ticker import ScalarFormatter
x = np.arange(len(eppm_plot)) 
width = 0.35  # the width of the bars
fig, ax1 = plt.subplots(figsize = (25,5))

rects1 = ax1.bar(x - width/2, all_test_tt, width, label='full_block_coverage_tt', color = 'y')
rects4 = ax1.bar(x+ width/2, keep_list_tt, width,bottom =list(np.add(always_failing_list_tt,always_passing_list_tt)) ,label = 'lean_coverage_tt', color='#fbb4ae', edgecolor='#fbb4ae')
rects2 = ax1.bar(x+ width/2, always_failing_list_tt ,width,label = 'always_failing_list', color='#b3cde3', edgecolor='#b3cde3')
rects3 = ax1.bar(x + width/2, always_passing_list_tt,width,bottom = always_failing_list_tt, label = 'always_passing_list',color='#ccebc5', edgecolor='#ccebc5')

ax1.set_ylabel('Test time in seconds')
ax1.set_xlabel('EPPM in e6')
ax1.set_title('Full, Lean coverage TT vs EPPM for ATPG,FUNC and MBIST blocks combined')
ax1.set_xticks(x)
ax1.set_xticklabels(eppm_plot)
ax1.legend()
ax1.bar_label(rects1)
ax1.bar_label(rects4)
ax1.bar_label(rects2, padding =-3)
ax1.bar_label(rects3, padding = 12)
ax1.set_yscale("log")
fig.tight_layout()
plt.show()

没有对数刻度的绘图

用对数刻度绘图

如您所见,即使报告了时间,always_failing_list_tt 也没有以对数刻度显示。谁能告诉我问题出在哪里?非常感谢您的帮助

您的代码工作正常。在这两种情况下,条形图都正确绘制,但它们要么相对于其他条形图太小,要么位于绘图区域之外。

无对数刻度

放大图的底部:ax1.set_ylim(0, 1):

如果不缩放,您将看不到它,因为它们与其他条形图相比太小了。

对数刻度

放大图的底部:ax1.set_ylim(1e-1, 1.5e2):

如果不缩放,您将看不到它,因为 y 轴的自动调整大小会将底部的蓝色条从图中切掉。


谨慎使用对数刻度:您的金条从 0 开始,这意味着它们从 -∞ 开始在对数刻度中变长。因此,在对数刻度中,没有定义的值来开始您的绘图:您可以任意选择从 1e-11e-21e-3 等开始。无论您选择什么值,都会影响绘图方面和条形高度的相对比例:

ax1.set_ylim(1e-5, 1.5e2)