Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib's "symlog" option: how to prevent curves that "come back"?

I'm plotting data which extent, in x, from -1000 to 1000. But I'm only interested in the values between x = -1 and 0.

I also want to plot on an x log scale. But since the x-values are negative, I cannot use xscale("log"). I can use xscale("symlog") though and this is the behaviour I want.

Unfortunately, "symlog" seems to be broken. I cannot use the linthreshx argument with a value of less then 2 (the default?). But since I'm interested in x values from -1 to 0, I have to use that argument and set it to something like 1e-5 or even smaller.

If I set linthreshx to something smaller then 1, the plot breaks. Here is a simple example, taken from What is the difference between 'log' and 'symlog'?:

import numpy
from matplotlib import pyplot

# Enable interactive mode
pyplot.ion()

# Draw the grid lines
pyplot.grid(True)

# Numbers from -50 to 50, with 0.1 as step
xdomain = numpy.arange(-50,50, 0.1)

# Plots a simple linear function 'f(x) = x'
pyplot.plot(xdomain, xdomain)
# Plots 'sin(x)'
pyplot.plot(xdomain, numpy.sin(xdomain))

pyplot.axis([-60,60,-1.5,1.5])

pyplot.xscale('symlog', linthreshx=0.1)

Running that you'll see what I mean by the curve "coming back"... Here is the resulting image: result

The problem seems to be that, on the x-axis, 0 is actually 10^0 = 1, not 0. Putting something smaller then 1 will make the line go back and the axis values are wrong (when hovering with the mouse and getting the x value).

I might not be using the right tool though, but how to achieve what I want? I want the x axis to look like: -10^2 -10^1 -10^0 -10^-1 -10^-2 -10^-3 ... [up to my defined minimum exponent] ... 10^-3 10^-2 10^-1 10^0 10^1 10^2

Thank you

like image 427
big_gie Avatar asked Feb 23 '23 14:02

big_gie


1 Answers

This was actually a bug in matplotlib.

See https://github.com/matplotlib/matplotlib/issues/396

It is now fixed. I'm attaching a picture showing the correct result of the previous snippet. Correct result

like image 85
big_gie Avatar answered Feb 26 '23 09:02

big_gie