Matplotlib not plotting logscale properly when using stacked bar chart

Viewed 63

I have 5 lists you can see below

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]

I want to create a stacked bar chart so that the second bar I plot compared to all_test_tt is a combination of (always_passing_list_tt + always_failing_list_tt + keep_list_tt)

I do that using the code below:

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()

plotting without logscale

plotting without log scale

plotting with log scale

plotting with log scale

As you can see the always_failing_list_tt is not getting show even in log scale even though time is being reported. Could anybody tell me what the issue is? Would really appreciate the help

1 Answers

You code work fine. In both cases the bars are plotted correctly, however they are either too small with respect to other bars, or they are outside the plot area.

Without log scale

Zooming in the bottom part of the plot: ax1.set_ylim(0, 1):

enter image description here

Without zooming you are not able to see it because they are too small compared to other bars.

With log scale

Zooming in the bottom part of the plot: ax1.set_ylim(1e-1, 1.5e2):

enter image description here

Without zooming you are not able to see it because the auto-resizing of the y axis cuts the bottom blue bar out of the plot.


Be cautious using log scale: you bars start from 0, this means in log scale they lengthen, starting from -∞. So in log scale there is not a defined value from which to start your plot: you could choose arbitrarily to start from 1e-1, 1e-2, 1e-3 and so on. Whatever value you choose, will affect the plot aspect and the relative proportions of the bars heights:

ax1.set_ylim(1e-5, 1.5e2) enter image description here

Related