我有這樣的代碼:
importmatplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import engFormatter, logLocator
fig, ax0 = plt.subplots(figsize=(10, 8)
fmin, fmax = 1, 1e91e-3, 1e6'log'/span>, 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'/span>)
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.y軸.set_minor_locator(locmin)
ax0.grid(which='both')
plt.show()
誰能猜到為什么在過去的二十年里,小網格線會丟失?
我使用的是matplotlib 3.4.3.
。uj5u.com熱心網友回復:
你不應該為x&y軸重復使用LogLocator物件,分配它將修改它的屬性。檔案串已經暗示了這一點:
numticks : None 或 int, default: 無
在一個給定的軸上允許的最大刻度數。默認的
None將嘗試智能地選擇,只要這個
定位器已經被分配給一個軸,使用
~.axis.Axis.get_tick_space,但否則就會退回到 9.
所以在這個特定的情況下,先賦值給y再賦值給x會使它 "作業"。我認為這意味著最后的賦值決定了某些屬性。
當然,最好是簡單地為每個軸(包括 x/y & major/minor)創建一個單獨的 LogLocator 物件的實體。
我不確定為什么當留下numticks=None時,minor-x的刻度不會顯示。
所以:
fig, ax0 = plt.subplots(figsize=(10, 8), facecolor='w')
fmin, fmax = 1, 1e9.
zmin, zmax = 1e-3, 1e6'log'/span>, base=10)
ax0.set_yscale('log'/span>, base=10)
ax0.set_xlim(fmin, fmax)
ax0.set_ylim(zmin, zmax)
ax0.xaxis.set_major_formatter(EngFormatter(unit='Hz'/span>)
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'/span>, axis='both'/span>, color='k'/span>, alpha=0.6)
ax0.grid(which='minor'/span>, axis='both'/span>, color='k'/span>, alpha=0.2)
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/312385.html
標籤:


