python - Seaborn Factorplot 的对数网格线

标签 python pandas matplotlib 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()

我得到下图。 enter image description here

尽管我将 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

enter image description here

知道我哪里错了吗?


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

enter image description here

更一般的也看this question .

关于python - Seaborn Factorplot 的对数网格线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48985874/

相关文章:

python - 如何在matplotlib中的子图下方添加图例?

python - 如何管理 PyQt5 QTableWidgetItem 中数据的可见性

python - Django 模型中域名的正则表达式匹配

Python pandas 删除具有列值 "NaN"的重复行

python - 使用 Pandas 搜索文本中的所有匹配项

python - 如何计算 pandas 中分类变量的滚动计数

python - 哪些版本的 NumPy 与 Python 3.3 兼容?

python - 找到字符串 X 的最长子序列,它是字符串 Y 的子字符串

python - 如何衡量覆盖率(在生产系统中)?

python - 使用 matplotlib 绘制大量点并耗尽内存