Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot negative values on a log scale

I am doing some analysis to calculate the value of log_10(x) which is a negative number. I am now trying to plot these values, however, since the range of the answers is very large I would like to use a logarithmic scale for this. If I simply use plt.yscale('log') I get a message telling me UserWarning: Data has no positive values, and therefore cannot be log-scaled. I also cannot supply the values of x to plt.plot as the result of log_10(x) is so large and negative that the answer of x**(log_10(x)) is simply 0.

What might be the most straightforward way of plotting this data?

like image 216
P-M Avatar asked Apr 12 '17 14:04

P-M


People also ask

How do you get negative log values?

A common technique for handling negative values is to add a constant value to the data prior to applying the log transform. The transformation is therefore log(Y+a) where a is the constant. Some people like to choose a so that min(Y+a) is a very small positive number (like 0.001). Others choose a so that min(Y+a) = 1.

What does a negative log graph mean?

The blue line represents the reflected line's function: y=-log_3(x). Although logarithms can not be negative, if a negative sign is put in front of a log function that still has its independent variable "x", it can produce a negative log graph. A flip across the x axis produces a negative log graph.

Why are negative values and zero missing from graphs using a logarithmic axis?

Why are negative values (and zero) missing from graphs using a logarithmic axis? The logarithm is simply not defined mathematically for zero or any negative value. Since a logarithmic axis essentially plots the logarithms of the values, it is only possible to plot positive values on logarithmic axes.

How do you plot zeros on a log scale?

The logarithm of zero is not defined -- its mathematically impossible to plot zero on a log scale. Instead of entering zero, you can enter a low value (say -10 on the log scale), and then use custom ticks to label the graph correctly (so it is labeled "0" rather than "-10".


1 Answers

You can use

plt.yscale('symlog')

to set the scale to a symmetic log scale. This means that it will scale logarithmically to both sides of 0. Only using the negative part of the symlog scale would work just fine.

like image 148
ImportanceOfBeingErnest Avatar answered Sep 21 '22 08:09

ImportanceOfBeingErnest