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?
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.
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? 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.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With