Logscale graphs with null values ​​in matplotlib

I am currently using logscale to have more options for building my data. However, my data also contains null values. I know that these null values ​​will not work in logscale since log (0) is undefined.

For example,

fig = plt.figure() ax = fig.add_subplot(111) ax.plot([0,1,2],[10,10,100],marker='o',linestyle='-') ax.set_yscale('log') ax.set_xscale('log') 

completely omits the zero value. Is this behavior acceptable? At least there should be some warning. I found out by chance. Could there be a way to build data with a null value in logscale?

Thanks!

PS: Hope this matches stackoverflow. I did not find matplotlib mailing list.

+7
source share
1 answer

The easiest way is to use the symlog plot for this purpose. An interval of about 0 will be on a linear scale, so 0 may be displayed.

 import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([0,1,2],[10,10,100],marker='o',linestyle='-') ax.set_yscale('symlog') ax.set_xscale('symlog') plt.show() 

enter image description here

Symlog sets a small interval near zero (both above and below) to use the linear scale. This allows things to cross 0 without causing log(x) explode (or go to -inf, rather).

There is a good visual comparison as an SO answer here: https://stackoverflow.com/a/166778/

+17
source

All Articles