X-Axis is incorrectly positioned in Seaborn

I have a multi-level index frame that I am trying to display in Seaborn. The plot shows thin, but the x-axis values ​​are treated as text labels instead of actual x-values. The snippet below shows how the sample data is produced and processed:

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

      

The plot shows:

sample data plot

However, the x-axis uses numeric values ​​as text labels. I would like the x-axis to display an exponential progression as expected from values ​​(i.e. 1000 should be 10 times farther from 100 than 100 should be 10). How can I fix this?

I suspect the multi-index is not relevant to the problem, but perhaps the data type it interprets is significant. A similar problem seems to be happening here: sea ​​boxes at desired distances along the x axis . I don't think this is a duplicate, but if the community disagrees, I would appreciate a brief explanation on how to apply it to my case.

+1


source to share


1 answer


factorplot

treats yours [1, 10, 100, 1000]

as categories (or factors). These are not seabed numbers - just labels. Therefore, they are evenly spaced (and internally they place these marks on a linear spaced scale of 0 to 3). A side effect of this is that it mimics a scaled log view that you might want to keep.

If I understand correctly what you are trying to do, this can be achieved without maritime use, but if it is a style, you can then import it and do something like this:

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

      



Which will create a graph like this:

enter image description here

And I'm not sure if this is really what you want, since the linear scale representation on the x axis makes the left side unreadable. The current chart is in the form of a scaled "log" axis, which appears to be more readable.

+4


source







All Articles