在熊猫DataFrame plot中指定y-array和plot标签可以更改DataFrame的索引标签/名称?

时间:2021-03-27 23:48:25

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

我有一只熊猫DataFrame,它的索引标签名为“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)

在熊猫DataFrame plot中指定y-array和plot标签可以更改DataFrame的索引标签/名称?

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

但是这会将index_column的标签/名称更改为“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=....

为什么?我想画一个dataframe,而不是改变它。即使简单的行图也会发生这种情况,不仅是errorplot,只要指定y=…和标签= ....

1 个解决方案

#1


2  

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

在调用df时设置标签参数。plot更改DataFrame的索引名称(源代码):

        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:

您可能想把这个问题提出来,但考虑到这是plot方法的工作方式,您可以在call to plt中设置label(s)。

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

在熊猫DataFrame plot中指定y-array和plot标签可以更改DataFrame的索引标签/名称?

This leaves df.index.name unchanged.

这使得df.index.name不变。

In [10]: df.index.name
Out[10]: 'RowNumber'

#1


2  

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

在调用df时设置标签参数。plot更改DataFrame的索引名称(源代码):

        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:

您可能想把这个问题提出来,但考虑到这是plot方法的工作方式,您可以在call to plt中设置label(s)。

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

在熊猫DataFrame plot中指定y-array和plot标签可以更改DataFrame的索引标签/名称?

This leaves df.index.name unchanged.

这使得df.index.name不变。

In [10]: df.index.name
Out[10]: 'RowNumber'