Seaborn Factorplot的对数网格线

时间:2018-02-26 10:07:40

标签: python pandas matplotlib seaborn

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

a, b = list(map(str, input().split()))
v = int(b)

我得到了下图。 enter image description here

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

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()

enter image description here

知道我哪里错了吗?

<小时/> 更新1

我接下来Diziet的答案,强制主要和次要的滴答如下

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

但它仍然无法解决问题

2 个答案:

答案 0 :(得分:2)

The problem is that in order to set the locations of ticks for cases where the automatically chosen major ticks are more than a decade away from each other seems to require to set the subs parameter of the locator as well as the numticks manually. Here, 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()

enter image description here

More generally also look at this question.

答案 1 :(得分:1)

我认为你做错了什么。在我看来,在你的第一个例子中,matplotlib决定(出于我不知道的原因)不显示任何小的滴答声,而它确实用于第二个例子。

解决问题的一种方法是强制显示次要刻度:

g = sns.factorplot(...)

ax = g.axes.flatten()[0]
ax.set_yscale('log')
ax.yaxis.set_minor_locator(matplotlib.ticker.LogLocator(base=10.0, subs='all'))
ax.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

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