X轴在Seaborn中的间距不正确 [英] X-Axis is not correctly spaced in Seaborn
问题描述
我有一个试图在Seaborn中显示的多级索引数据框.该图显示得很好,但是x轴的值被视为文本标签,而不是实际的x值.以下代码段显示了如何制作和绘制示例数据:
I have a multi-level indexed dataframe that I am trying to display in Seaborn. The plot is showing up fine, but the values of the x-axis are being treated as text labels instead of actual x-values. The snippet below shows how sample data is made and plotted:
>>> import numpy, pandas, seaborn
>>> from matplotlib import pyplot
>>> index = pandas.MultiIndex.from_product((list('abc'), [10**x for x in range(4)]), names=['letters', 'powers'])
>>> index
MultiIndex(levels=[['a', 'b', 'c'], [1, 10, 100, 1000]],
labels=[[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]],
names=['letters', 'powers'])
>>> df = pandas.DataFrame(numpy.random.randn(12, 2), index=index, columns=['x', 't'])
>>> df
x t
letters powers
a 1 1.764052 0.400157
10 0.978738 2.240893
100 1.867558 -0.977278
1000 0.950088 -0.151357
b 1 -0.103219 0.410599
10 0.144044 1.454274
100 0.761038 0.121675
1000 0.443863 0.333674
c 1 1.494079 -0.205158
10 0.313068 -0.854096
100 -2.552990 0.653619
1000 0.864436 -0.742165
>>> seaborn.factorplot(x='powers', y='t', hue='letters', data=df.reset_index())
>>> pyplot.show()
该图显示:
但是,x轴使用数值作为文本标签.我希望x轴显示从值所期望的指数级数(即1000应该是距100的10倍比100距10的距离大10倍).我该如何解决?
However, the x-axis is using the numerical values as text labels. I would like the x-axis to show an exponential progression as expected from the values (i.e., 1000 should be 10 times farther from 100 than 100 is from 10). How can I fix that?
我怀疑多索引与问题无关,但也许它被解释为有意义的数据类型.这里似乎发生了类似的问题:沿x轴的所需距离处的季节性箱形图.我不认为这是重复的,但如果社群不同意,我将对如何将其应用于我的案件进行简短的解释.
I suspect that the multi-index is not relevant to the problem, but perhaps the datatype it is being interpreted as is significant. A similar issue seems to be happening here: seaborn boxplots at desired distances along the x axis. I do not think it is a duplicate, but if the community disagrees, I would appreciate a brief explanation of how to apply it to my case.
推荐答案
factorplot
将您的[1, 10, 100, 1000]
视为类别(或因素).这些不是Seaborn的数字,只是标签.
这就是为什么它们要均匀分布的原因(在内部它会将这些标签以从0到3的线性间隔比例放置).这样做的副作用是,它模仿了可能要保留的对数比例表示.
factorplot
is treating your [1, 10, 100, 1000]
as categories (or factors). Those are not numbers for seaborn - just labels.
That's why they are spaced evenly (and internally it places those labels on a linear spaced scale from 0 to 3). The side effect from this is that it mimics the log-scaled representation, which you might want to keep.
如果我正确理解正在尝试做的事情,则可以毫无困难地实现,但是如果它的样式是您仍然可以将其导入并随后执行以下操作,则可以:
If I understand correctly what are trying to do, this can be achieved without seaborn, but if it is styling you are after you can still import it and do something like this afterwards:
fig, ax = plt.subplots(figsize=(5,3))
for l in df.index.get_level_values(0).unique():
ax.plot(df.loc[l, 'x'], 'o-', label=l)
ax.legend(loc=0)
ax.set_xlim([-10, 1001])
ax.set_xticks(df.index.get_level_values(1).unique())
这将产生如下图表:
我不确定这是否真的是您需要的,因为在x轴上表示线性比例会使左侧不可读. 您当前的图表具有"log"比例的x轴外观,这似乎是一种更具可读性的表示形式.
And I am not sure this is really what you need since representing linear scale on x-axis is making left side unreadable. You current chart has appearance of a 'log' scaled x-axis, which seems to be a more readable representation.
这篇关于X轴在Seaborn中的间距不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!