2

I want to plot in log scale in both axis, when using the default base, the graph plotted is correct, but since the y variation of my data is small I would like to change for base=2, when I does this the grid is no longer in log scale but evenly spaced with no small ticks (which makes easier to see that this is a log scale plot)

where is my code for default base

import numpy as np
import matplotlib.pyplot as plt

x = np.array([8, 16, 32, 64, 128, 256, 512, 1024])
y = np.array([2.412,  3.424,  5.074,  7.308, 11.444, 18.394, 30.644, 48.908])
base = 2 # Base for logarithmic scale

fig, ax = plt.subplots(figsize=(8,6))

# log x and y axis
ax.plot(x, y, 'o--')
ax.set_xscale('log')
ax.set_yscale('log')
ax.set(title='loglog')
ax.grid()
ax.grid(which="minor", color="0.9")

enter image description here

when I change the base, by using the argument base in ax.set_xscale()

import numpy as np
import matplotlib.pyplot as plt

x = np.array([8, 16, 32, 64, 128, 256, 512, 1024])
y = np.array([2.412,  3.424,  5.074,  7.308, 11.444, 18.394, 30.644, 48.908])
base = 2 # Base for logarithmic scale

fig, ax = plt.subplots(figsize=(8,6))

# log x and y axis
ax.plot(x, y, 'o--')
ax.set_xscale('log', base=base)
ax.set_yscale('log', base=base)
ax.set(title='loglog')
ax.grid()
ax.grid(which="minor", color="0.9")

enter image description here

The graphs I created are correct, what I want is aesthetic. Keep these little ticks "logarithmly" spaced.
I have tried to use ax.loglog(x, y, '0--', base=base) instead of ax.plot(x, y, 'o--'), and also tried to manually set the tickers with

def ticks(y, pos):
    return r'$2^{:.0f}$'.format(np.log(y)/np.log(base)) if y > 0 else '0'

ax.xaxis.set_major_formatter(ticker.FuncFormatter(ticks))
ax.yaxis.set_major_formatter(ticker.FuncFormatter(ticks))

None of that work.

What I want is aesthetics, both graphs are correct, but these little ticks "logarithmly" spaced makes the graph easier to interpret as log-log plot.

5
  • Are you looking for minor ticks? stackoverflow.com/questions/30887920/… With a minor locator I mean. Commented Jul 11 at 21:11
  • 1
    With base 10, you get major ticks at 10, 100, 1000, ... and minor ticks at 20, 30, 40, .... . With base 2 you get major ticks at 2, 4, 8, 16, ... . There aren't logical positions to place multiple minor ticks between the major ticks. Commented Jul 11 at 21:22
  • thanks @AndrasDeak--СлаваУкраїні, this is what I was looking for, but as JohanC pointed out, there is no logical positions to place minor ticks for base 2, I solved my problem by using base 4 in y axis and base 8 in x axis. Commented Jul 11 at 22:30
  • 3
    Since you soulved your problem, it would be nice to 1. Edit your question to include image illustrating what you were expecting, and what you get. 2. Add an answer to your own question, showing what you achieve by using base 4 and 8 (and also why: so that, I guess, that you have some logical position for minor ticks, and therefore, that the graph looks visually the same as in base 10, with major and minor ticks). Commented Jul 11 at 22:58
  • @IgorSoares solutions should go in answers, not in edits to the question. Commented Jul 13 at 7:07

1 Answer 1

0

As @JohanC answered with base=2 there are no logical positions to place these minor ticks, so my solution was to use `basex=8` for `x` axis and `basey=4` for `y` axis.


basex = 8 # Base for logarithmic scale

basey = 4 # Base for logarithmic scale

fig, ax = plt.subplots(figsize=(8,6))

ax.plot(x, y, 'o--')

ax.set_xscale('log',base = basex)

ax.set_yscale('log', base = basey)

ax.set(title='loglog')

ax.grid()

ax.grid(which="minor", color="0.9")

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.