Note

Go to the endto download the full example code.

Symlog scale#

The symmetric logarithmic scale is an extension of the logarithmic scale thatalso covers negative values. As with the logarithmic scale, it is particularlyuseful for numerical data that spans a broad range of values, especially when thereare significant differences between the magnitudes of the numbers involved.

Example use of symlog (symmetric log) axis scaling.

importmatplotlib.pyplotaspltimportnumpyasnpdt=0.01x=np.arange(-50.0,50.0,dt)y=np.arange(0,100.0,dt)fig,(ax0,ax1,ax2)=plt.subplots(nrows=3)ax0.plot(x,y)ax0.set_xscale('symlog')ax0.set_ylabel('symlogx')ax0.grid()ax0.xaxis.grid(which='minor')# minor grid on tooax1.plot(y,x)ax1.set_yscale('symlog')ax1.set_ylabel('symlogy')ax2.plot(x,np.sin(x/3.0))ax2.set_xscale('symlog')ax2.set_yscale('symlog',linthresh=0.015)ax2.grid()ax2.set_ylabel('symlog both')fig.tight_layout()plt.show()
symlog demo

Linear threshold#

Since each decade on a logarithmic scale covers the same amount of visual spaceand there are infinitely many decades between a given number and zero, the symlogscale must deviate from logarithmic mapping in a small range(-linthresh, linthresh), so that the range is mapped to a finite visual space.

defformat_axes(ax,title=None):"""A helper function to better visualize properties of the symlog scale."""ax.xaxis.get_minor_locator().set_params(subs=[2,3,4,5,6,7,8,9])ax.grid()ax.xaxis.grid(which='minor')# minor grid on toolinthresh=ax.xaxis.get_transform().linthreshlinscale=ax.xaxis.get_transform().linscaleax.axvspan(-linthresh,linthresh,color='0.9')iftitle:ax.set_title(title.format(linthresh=linthresh,linscale=linscale))x=np.linspace(-60,60,201)y=np.linspace(0,100.0,201)fig,(ax1,ax2)=plt.subplots(nrows=2,layout="constrained")ax1.plot(x,y)ax1.set_xscale('symlog',linthresh=1)format_axes(ax1,title='Linear region: linthresh={linthresh}')ax2.plot(x,y)ax2.set_xscale('symlog',linthresh=5)format_axes(ax2,title='Linear region: linthresh={linthresh}')
Linear region: linthresh=1, Linear region: linthresh=5

Generally,linthresh should be chosen so that no or only a fewdata points are in the linear region. As a rule of thumb,\(linthresh \approx \mathrm{min} |x|\).

Linear scale#

Additionally, thelinscale parameter determines how much visual space should beused for the linear range. More precisely, it defines the ratio of visual spaceof the region (0, linthresh) relative to one decade.

fig,(ax1,ax2)=plt.subplots(nrows=2,layout="constrained")ax1.plot(x,y)ax1.set_xscale('symlog',linthresh=1)format_axes(ax1,title='Linear region: linthresh={linthresh}, linscale={linscale}')ax2.plot(x,y)ax2.set_xscale('symlog',linthresh=1,linscale=0.1)format_axes(ax2,title='Linear region: linthresh={linthresh}, linscale={linscale}')
Linear region: linthresh=1, linscale=1, Linear region: linthresh=1, linscale=0.1

The suitable value for linscale depends on the dynamic range of data. As most datawill be outside the linear region, you typically the linear region only to covera small fraction of the visual area.

Limitations and alternatives#

The coordinate transform used bysymlog has a discontinuous gradient at thetransition between its linear and logarithmic regions. Depending on data andscaling, this will be more or less obvious in the plot.

fig,ax=plt.subplots()ax.plot(x,y)ax.set_xscale('symlog',linscale=0.05)format_axes(ax,title="Discontinuous gradient at linear/log transition")
Discontinuous gradient at linear/log transition

Theasinh axis scale is an alternative transformation that supports a widedynamic range with a smooth gradient and thus may avoid such visual artifacts.SeeAsinh scale.

Total running time of the script: (0 minutes 3.750 seconds)

Gallery generated by Sphinx-Gallery