When I create a DataFrame, then sort by a column it appears to be sorted in the iteractive display (i.e. whatever repr gives), but when I call the DataFrame.plot() function it plots the unsorted array. Calling matplotlib.pylab.plot works fine though. I suspect it's something to do with clever pointer rearrangement not being passed to whatever the plot function is calling to access the data ... or maybe I'm just doing something dumb. I've tried this on pandas 0.8.1 (osx and linux with python2.7.something) and pandas 0.9.0 (osx with python3.something).
import pandas
import numpy
from matplotlib.pylab import *
a = numpy.random.randn(100,10)
df = pandas.DataFrame(a)
df.shape
df.sort(column=0)
df.columns
df.sort(column=0, inplace=True)
df[0]
df[0].plot()
Plotting is by default your data versus the index. In case of a numerical index, look at the index after sorting on a column. you might want to use plot(use_index=False). See example below.
In [2]: df = pd.DataFrame(np.random.randn(10, 2), columns=['A', 'B'])
In [3]: df
Out[3]:
A B
0 -0.938196 2.220319
1 -0.022503 0.564602
2 0.033094 -0.717969
3 2.466486 1.229651
4 -0.641596 -1.016921
5 0.094125 1.531526
6 0.579631 1.398635
7 -0.854799 -0.930904
8 -1.177894 -1.501657
9 0.341655 -0.917243
In [4]: df.sort(columns='A', inplace=True)
Out[4]:
A B
8 -1.177894 -1.501657
0 -0.938196 2.220319
7 -0.854799 -0.930904
4 -0.641596 -1.016921
1 -0.022503 0.564602
2 0.033094 -0.717969
5 0.094125 1.531526
9 0.341655 -0.917243
6 0.579631 1.398635
3 2.466486 1.229651
In [5]: df['A'].plot(use_index=False)
Out[5]: <matplotlib.axes.AxesSubplot at 0xb56ac6c>

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With