Search code examples
pythonmatplotliblogarithm

Draw connecting line to points with a zero ordinate on a log scale with matplotlib?


Is it possible to plot the connecting line to points whose y value is zero on a log scale in matplotlib?

I have some data that I want to plot with a log scale on the y-axis. The y values for some of the data lie at zero. I realize it's not possible for matplotlib to plot these points on a log scale, but I really wish it would draw the connecting line from the previous point or to the next point (if either are non-zero).

One solution would be to simply replace all zeros with some TINY number. I'd rather not do this.

What matplotlib draws: Log Plot with No Connecting Line

What I'd like it to draw: Log Plot with Connecting Line


Solution

  • I'd be looking to solve this by using the 'symlog' option on the y axis instead of 'log'. There's then a linthreshy arg which lets you specify

    "The range within which the plot is linear (to avoid having the plot go to infinity around zero).".

    In fact it's exactly this sort of issue the option seems designed to deal with. It can look a bit goofy having this weird linear zone along the bottom of your log scale plot, but you can make it pretty small.