user35510
user35510

Reputation: 179

How to set log scale for values less than one in Matplotlib

I am graphing the following data using matplot lib:

x_val = [-(2**24), -(2**23), -(2**22), -2(21)]
y_val = [-1/2, -1/4, -1/8, -1/16]

I data relation is linear, but because of the step patten I want to use a log scale, base 2, for both the x and y axis of the graph.

I problem is that the y values are negative and between 0 and -1. This means that I can't use symlog because it uses a linear scale for values less than 10. I am wondering if there is another way to scale the axis that will give a log for decimal numbers. This is my code so far:

ax1.set_xscale('symlog', basex=2)
ax1.set_yscale('symlog', nonposy='clip')

the resulting graph

Upvotes: 2

Views: 3930

Answers (1)

tmdavison
tmdavison

Reputation: 69228

You can change the value at which the scale changes to linear using linthreshy (or linthreshx on the x axis). See the documentation for set_yscale or this example for more information.

Here's an example, changing it to 0.01, which plots your data as a straight line:

import matplotlib.pyplot as plt

x_val = [-(2**24), -(2**23), -(2**22), -(2**21)]
y_val = [-1./2., -1./4., -1./8., -1./16.]

fig,ax1 = plt.subplots(1)

ax1.plot(x_val,y_val,'bo-')

ax1.set_xscale('symlog', basex=2)
ax1.set_yscale('symlog', nonposy='clip', linthreshy=0.01)

plt.show()

enter image description here

Upvotes: 3

Related Questions