33

I am currently using logscale in order to have greater possibilities of plotting my data. Nevertheless, my data consists also of zero values. I know that these zero values will not work on logscale as log(0) is not defined.

So e.g.,

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 kind of warning. I only recognized it by accident. Is there maybe also a way of plotting zero value data in logscale?

Thanks!

P.S.: I hope this fits to stackoverflow. I did not find a mailing list of matplotlib.

fsociety
  • 1,791
  • 4
  • 22
  • 32
  • 1
    mpl mailing lists -> http://sourceforge.net/mail/?group_id=80706 – tacaswell Jun 03 '13 at 19:53
  • 2
    possible duplicate of [Matplotlib logarithmic scale with zero value](http://stackoverflow.com/questions/16382917/matplotlib-logarithmic-scale-with-zero-value) – Benjamin Bannier Jun 03 '13 at 19:55
  • Above question has two possible solutions for this. – Benjamin Bannier Jun 03 '13 at 19:55
  • @honk - Actually, that question doesn't answer the OP's question. – Joe Kington Jun 03 '13 at 19:59
  • @JoeKington: I wasn't aware of `symlog` which is also a nice answer to the question I linked to. When using it one still needs to keep in mind that the displayed plot isn't strictly a logplot anymore though. – Benjamin Bannier Jun 03 '13 at 20:13
  • @honk - True! The other answers are "true" log plots, but specifying new formatters is overkill, i.m.o., as the linear portion of the plot is very small (and can be set if it's too large). – Joe Kington Jun 03 '13 at 20:15
  • Add the following two commands, and you will see that matplotlib is not omitting the zero value. You will see the line extend all the way off the plot toward zero. plt.xlim(1e-6, 1e6) plt.ylim(1e-6, 1e6) see also: http://stackoverflow.com/questions/18697417/not-plotting-zero-in-matplotlib-or-change-zero-to-none-python – poleguy Jul 17 '15 at 14:48

1 Answers1

54

It's easiest to use a "symlog" plot for this purpose. The interval near 0 will be on a linear scale, so 0 can 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 a linear scale. This allows things to cross 0 without causing log(x) to explode (or go to -inf, rather).

There's a nice visual comparison as an SO answer here: https://stackoverflow.com/a/3513150/325565

Community
  • 1
  • 1
Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • Great answer. Thanks for that. Could you elaborate what symlog is exactly doing? – fsociety Jun 03 '13 at 20:24
  • 1
    Sure! See the updates. There are also a few examples in the matplotlib gallery (e.g. http://matplotlib.org/examples/pylab_examples/symlog_demo.html ), but they're not as clear as the SO answer I linked to. – Joe Kington Jun 03 '13 at 20:33