1

This question is highly related to that one.

I'm trying to plot a graph in which the x-axis is

[0] + [2**(x-4) for x in xrange(8)]

The answer to the other question allows matplotlib to have an axis with [0, 2**(0), 2**(1), 2**(2), 2**(3)], but it does not add the negative powers.

Basically, I want a log-scale plot with another point (placed at where 2**(-5) would be horizontal if it existed instead) for x=0.

Any ideas?

enter image description here

R B
  • 543
  • 9
  • 25

1 Answers1

1

You can plot it with matplotlib's loglog. Here is a basic example:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0] + [2**(x-4) for x in range(8)])
f = lambda x: 10**x

plt.loglog(x, f(x), basex=2, basey=10)

As you can see, you can pass the x and y base seperately and it also gives 2**(-1)...

Here is a picture of the example:

plot

I don't know if I understand the second part of your question.

EDIT I think i might understand now. Since it is not possible to have a 0 on a log scale, here is a symbolic approach:

On your scale, the smallest number on the x-axis was 2**(-4) so I use 2**(-5) as a symbolic zero an rename the ticks accordingly. This is done via xticks:

plt.xticks([2**i for i in range(-5,4)], [r"0"] + [r"$2^{%d}$" %(int(i)) for i in range(-4,4)])

The first argument [2**i for i in range(-5,4)] creates the positions of the ticks and the second argument the labels.

It now looks like this:

plot2

Remember: A "zero" here is actually a 2**(-5)!

user8408080
  • 2,428
  • 1
  • 10
  • 19
  • This is nice, but it doesn't have a tick for 0 in the x axis. I want to have a tick for 0, and ticks for 2^(-4),... – R B Nov 12 '18 at 19:52
  • And on the 0 tick you want to plot data, that is negative or zero? I mean a log scale for negative or zero numbers doesn't really make sense. There simply is no 0 on a log scale, because the log diverges at 0 – user8408080 Nov 12 '18 at 19:53
  • There are no negative values. x values are just 0 and powers of 2 and the y values are all positive (as in the figure I had in the question). – R B Nov 12 '18 at 20:08
  • I hope this fits your needs now – user8408080 Nov 12 '18 at 20:30