Seaborn 因子图的对数网格线

Mec*_*nic 4 python matplotlib pandas seaborn

我正在尝试在数据帧上使用 seaborn factorplot 绘制对数图,如下所示

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

l1 = [0.476, 0.4427, 0.378, 0.2448, 0.13, 0.004, 0.012, 0.0933, 3.704e-05, 
    1.4762e-06, 4.046e-08, 2.99e-10, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

df = pd.DataFrame(l1, columns=["y"])
df.reset_index(inplace=True)

g = sns.factorplot(x='index',y='y', data=df, aspect=2, size=8)
g.fig.get_axes()[0].set_yscale('log')
plt.grid(True,which="both",ls="--",c='gray')  

plt.show()
Run Code Online (Sandbox Code Playgroud)

我得到下图。 在此处输入图片说明

尽管我将 Y 轴比例更改为 log 并使用了两条网格线,但最终的数字没有对数刻度刻度。但是,当与另一组值一起使用时,相同的代码给出了下图。在这种情况下,最小值限制为 10^-7

l2 = [0.29, 0.111, 0.0285, 0.0091, 0.00045, 5.49759e-05, 1.88819e-06, 0.0, 0.0, 0.0]
df = pd.DataFrame(l2, columns=["y"])
# same code as above
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

知道我哪里错了吗?


更新 1

我遵循了 Diziet 的回答,并按如下方式强制进行了主要和次要滴答声

g.ax.yaxis.set_minor_locator(tkr.LogLocator(base=10, subs='all'))
g.ax.yaxis.set_minor_formatter(tkr.NullFormatter())
g.ax.set_yscale('log')


g.ax.grid(True,which="both",ls="--",c='gray')  
Run Code Online (Sandbox Code Playgroud)

但是还是没有解决问题

Imp*_*est 5

问题是,为了在自动选择的主要刻度彼此相距十多年的情况下设置刻度位置似乎需要设置subs定位器的参数以及numticks手动。在这里,mticker.LogLocator(base=10, subs=np.arange(0.1,1,0.1),numticks=10)

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import seaborn as sns

l1 = [0.476, 0.4427, 0.378, 0.2448, 0.13, 0.004, 0.012, 0.0933, 3.704e-05, 
    1.4762e-06, 4.046e-08, 2.99e-10, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] 
df = pd.DataFrame(l1, columns=["y"])
df.reset_index(inplace=True)

g = sns.factorplot(x='index',y='y', data=df, aspect=2, size=8)
g.ax.set_yscale('log')

plt.grid(True,which="both",ls="--",c='gray')  

locmin = mticker.LogLocator(base=10, subs=np.arange(0.1,1,0.1),numticks=10)  
g.ax.yaxis.set_minor_locator(locmin)
g.ax.yaxis.set_minor_formatter(mticker.NullFormatter())

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

更一般的也看看这个问题