Issue
I have this code:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import EngFormatter, LogLocator
fig, ax0 = plt.subplots(figsize=(10, 8))
fmin, fmax = 1, 1e9
zmin, zmax = 1e-3, 1e6
ax0.set_xscale('log', base=10)
ax0.set_yscale('log', base=10)
ax0.set_xlim(fmin, fmax)
ax0.set_ylim(zmin, zmax)
ax0.xaxis.set_major_formatter(EngFormatter(unit='Hz'))
ax0.yaxis.set_major_formatter(EngFormatter(unit='Ω'))
ax0.xaxis.set_major_locator(LogLocator(base=10, numticks=100))
locmin = LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8))
ax0.xaxis.set_minor_locator(locmin)
ax0.yaxis.set_minor_locator(locmin)
ax0.grid(which='both')
plt.show()
It produces the follwoing output.
Anyone's guess why the minor grid lines are missing on the last two decades?
I am on matplotlib 3.4.3.
Solution
You shouldn't reuse the LogLocator
object for both x&y axis, assigning it will modify it's properties. The docstring already hints at this:
numticks : None or int, default: None The maximum number of ticks to allow on a given axis. The default of
None
will try to choose intelligently as long as this Locator has already been assigned to an axis using~.axis.Axis.get_tick_space
, but otherwise falls back to 9.
So in this specific case, first assigning to y before x would make it "work". I assume that means the last assignment determines some of the properties.
But of course it's best to simply create a separate instance of the LogLocator
object for each axis (both x/y & major/minor).
I'm not sure why the minor-x ticks won't show when leaving numticks=None
.
So:
fig, ax0 = plt.subplots(figsize=(10, 8), facecolor='w')
fmin, fmax = 1, 1e9
zmin, zmax = 1e-3, 1e6
ax0.set_xscale('log', base=10)
ax0.set_yscale('log', base=10)
ax0.set_xlim(fmin, fmax)
ax0.set_ylim(zmin, zmax)
ax0.xaxis.set_major_formatter(EngFormatter(unit='Hz'))
ax0.yaxis.set_major_formatter(EngFormatter(unit='Ω'))
ax0.xaxis.set_major_locator(LogLocator(base=10, subs=(1.0,), numticks=100))
ax0.xaxis.set_minor_locator(LogLocator(base=10.0, subs=(0.2, 0.4, 0.6, 0.8), numticks=100))
ax0.yaxis.set_minor_locator(LogLocator(base=10.0, subs=(0.2, 0.4, 0.6, 0.8), numticks=100))
ax0.grid(which='major', axis='both', color='k', alpha=0.6)
ax0.grid(which='minor', axis='both', color='k', alpha=0.2)
plt.show()
Answered By - Rutger Kassies
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.