If you look at the density table, you will see that ozone has the highest density among other gases, so why is it in the upper layer of the atmosphere, in the picture I schematically drew how the earth's atmosphere should look in my view.
Atmospheric Science – Why Is Ozone (O3) in the Earth’s Upper Atmosphere?
airatmospheric scienceearthgeophysicsmolecules
Related Solutions
Gases can only scatter light strongly if it matches a quantum transition (Rayleigh scattering doesn't involve quantum transitions but it's relatively weak). Quantum transitions can be rotational, vibrational or electronic (strictly speaking rotational and vibrational transitions are usually combined). Rotational/vibrational transitions have an energy that is generally in the IR range (which is why CO$_2$ scatters IR light) and electronic transitions have energies in the UV range (which is why ozone scatters UV). So there's a gap in the visible range. You say:
I also notice other planets in our neighbourhood weren't quite so lucky
but assuming you're talking about Venus, Jupiter, Titan etc, the scattering is from particles not from gases. After all, Earth's atmosphere isn't particularly transparent on a cloudy day.
It is a co-incidence that the Sun's light peaks in the visible region. Planets have been found around all sorts of stars, and if the star is cooler or hotter than the Sun it's spectrum will peak at a different wavelength. However it may not be a co-incidence that we evolved on a planet whose starlight does peak in the visible wavelengths. After all, if it didn't, life on Earth would probably be different and we probably would be here.
You are right: if the gas you are studying is not in a container, it is difficult to attribute a volume to it.
The key here is to realize that on the scale of the atmosphere, temperature, pressure and density change - by a lot. So you can't think of "all of the atmosphere" as a single body of air with uniform properties - the properties change locally. As such, you want to express the behavior in terms of "local" properties only. This makes volume not a suitable candidate - but temperature, pressure and density are all locally defined, so ideal for describing such a system. As @t.c. points out n another answer, the formal name for such "locally defined" properties is "intensive", to contrast with "extensive" properties which apply to a system. Note that while the wiki entry says
An intensive property is a bulk property, meaning that it is a physical property of a system that does not depend on the system size or the amount of material in the system.
that doesn't mean it cannot change with location in the system - and indeed in the atmosphere they change a lot, which is why they are useful for describing the system.
update
Per David Hammen's suggestion, going from the ideal gas law formulation:
$$pV = nRT$$
to the formulation in terms of density, you replace $n$ with $\frac{m}{M}$, then divide both sides by $V$:
$$p = \frac{m}{V} \frac{R}{M} T$$
We recognize $\frac{m}{V}$ as the density $\rho$, and $\frac{R}{M}$ as the specific gas constant, sometimes written as $R^\ast$. This leads to
$$p = \rho R^\ast T$$
Now all parameters ($p, \rho, T$) are intensive quantities.
Best Answer
There is ozone production at all altitudes, but a majority of it is in the uppermost regions because at these heights, ozone is produced at much higher rates.
Ozone is produced when high enough energy light strikes oxygen molecules O$_2$ producing ionic oxygen that will then bond with other oxygen molecules forming ozone O$_3$ That is, $$O_2 + {\bf\large\gamma} \rightarrow O + O \\ \rightarrow O+O + 2O_2 \rightarrow2 O_3$$
There is less ozone production at lower altitudes because the once higher energy light at greater altitudes gets scattered on its journey down, loosing energy on the way, so that at lower heights ozone production occurs at a much smaller rate. Also note that the ozone molecule has a short lifetime (from 30 minutes to 2 hours before turning back into oxygen). That is, $$O + O_3\rightarrow 2 O_2$$