简体   繁体   English

Seaborn因子图的对数网格线

[英]Logarithmic Gridlines for Seaborn Factorplot

I am trying to plot a logarithmic plot using seaborn factorplot on a dataframe as follows 我正在尝试使用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()

I get the following figure. 我得到下图。 在此处输入图片说明

Even though I changed the Y-axis scale to log and used both the gridlines, the final figure doesnt have the log scale ticks. 即使我将Y轴比例更改为对数并使用了两条网格线,最终图形也没有对数比例刻度。 However, the same code when used with another set of values gives me the following figure. 但是,相同的代码与另一组值一起使用时,如下图所示。 In this case, the minimum value is limited to 10^-7 在这种情况下,最小值限制为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

在此处输入图片说明

Any idea where I am being wrong? 知道我错了吗?


Update 1 更新1

I follwed Diziet's answer and forced the major and minor ticks as follows 我追随了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') 

But it still doesnt solve the problem 但是它仍然不能解决问题

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. 问题在于,在自动选择的主要刻度线彼此相距十多年的情况下,要设置刻度线的位置,似乎需要手动设置定位器的subs参数和numticks Here, mticker.LogLocator(base=10, subs=np.arange(0.1,1,0.1),numticks=10) . 在这里, 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()

在此处输入图片说明

More generally also look at this question . 更普遍地也看这个问题

I do not think you are doing anything wrong. 我认为您没有做错任何事情。 It seems to me that, in your first example, matplotlib decided (for a reason unknown to me) not to show any minor ticks, while it does for the second example. 在我看来,在您的第一个示例中,matplotlib决定(出于我不知道的原因)决定不显示任何较小的滴答声,而在第二个示例中则是如此。

One way to solve your issue is to force the display of minor ticks: 解决问题的一种方法是强制显示较小的刻度线:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM