It is because these two examples use the word investment in different meaning.
In the top example, investment means all investment whether it is investment into something productive or inventory investment.
In the second graph, the word “investment” is applied to investment minus inventory investment.
This is why in the second picture the difference between supply and demand is called undesired inventory investment (inventory investment is unproductive so typically society would want to avoid it).
So to sum up, the difference there stems from different use of the word investment. In the top picture investment denotes the total investment, whereas on the bottom picture investment is shown as investment minus inventory investment.
Total investment will always be equal total savings. Investment minus inventory investment will only be equal to savings in an equilibrium where (subject to various assumption on perfectness of the market) inventory investment will be zero.
The key here is to distinguish between ex ante and ex post concepts of saving and investment.
These concepts were defined by the economist Gunnar Myrdal as follows (quoted here):
An important distinction exists between prospective and retrospective methods of calculating economic quantities such as incomes, savings, and investments; and [...] a corresponding distinction of great theoretical importance must be drawn between two alternative methods of defining these quantities. Quantities defined in terms of measurements made at the end of the period in question are referred to as ex post; quantities defined in terms of action planned at the beginning of the period in question are referred to as ex ante.
In the ex post sense, saving and investment must be equal in a closed economy with no government sector. This is an accounting identity, arising from the fact that national income from an expenditure perspective equals consumption plus investment, while for households in aggregate, who receive the whole of national income via wages and salaries, rents, dividends, etc., national income equals consumption plus savings.
In the ex ante sense, investment and saving can be unequal because they represent plans or desired levels which may or may not be actualized.
In simple macro models it is commonly assumed that planned investment is largely independent of current income. This is reflected in fig 3.4 by the approximately horizontal investment demand line. The assumption can be justified by noting that normally the majority of investment is in fixed capital (buildings, plant and equipment) with an operational life of many periods, so decisions to invest are based mainly on expectations of demand in future periods.
Planned savings on the other hand are assumed to vary with income: hence the sloping savings line in fig 3.4. As a consequence, there will be a level of income at which planned investment and planned savings are in equilibrium.
Whether, and how quickly, such an equilibrium will be achieved is a further question. If income has not adjusted to bring about such an equilibrium, investment must still equal saving in the ex post sense. If, in a stuation of disequilibrium, both fixed capital investment and saving are at their planned levels for the actual level of income, then the accounting identity can (and must) be satisfied by an unplanned adjustment in the other component of investment, that is, investment in inventory.