我正尝试在数据框上使用 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()
尽管我将 Y 轴刻度更改为对数并使用了两条网格线,但最终图形没有对数刻度。但是,当与另一组值一起使用时,相同的代码会给出下图。在这种情况下,最小值限制为 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
知道我哪里错了吗?
更新 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')
但是还是没有解决问题
最佳答案
问题是,为了在自动选择的主要刻度线彼此相距十多年的情况下设置刻度线的位置,似乎需要设置定位器的 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()
更一般的也看this question .
关于python - Seaborn Factorplot 的对数网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48985874/