问题描述:

I have a pandas DataFrame with index label named as 'RowNumber'.

import pandas as pd

import matplotlib.pyplot as plt

df = pd.DataFrame(data=np.random.rand(5,3), index=np.arange(5), columns=['A', 'B', 'C'])

df.index.name='RowNumber'

df

Out[1]:

A B C

RowNumber

0 0.994063 0.068659 0.158425

1 0.116639 0.109248 0.160374

2 0.086689 0.269934 0.060204

3 0.794417 0.988650 0.306970

4 0.193915 0.230677 0.414131

I plot a errorbar plot, with legend.

df.plot(y='B', yerr='C', label='Test')

plt.legend(loc=0)

But this changes the label/name of index_column to 'Test'.

 A B C

Test

0 0.994063 0.068659 0.158425

1 0.116639 0.109248 0.160374

2 0.086689 0.269934 0.060204

3 0.794417 0.988650 0.306970

4 0.193915 0.230677 0.414131

Why ? I want to plot a dataframe, not change it.

This happens even with the simple line plot, not only errorplot, as long as one specify y=... and label=....

网友答案:

Setting the label parameter in the call to df.plot alters the DataFrame's index name (source code):

        label = kwds.pop('label', label)
        ser = frame[y]
        ser.index.name = label

You might want to raise this as an issue, but given that this is the way the plot method works, you could instead set the label(s) in the call to plt.legend:

df.plot(y='B', yerr='C')
plt.legend( ('Test',), loc=0 )
plt.show()

This leaves df.index.name unchanged.

In [10]: df.index.name
Out[10]: 'RowNumber'
相关阅读:
Top