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")
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")
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.
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")