pandas 熊猫 - 绘制排序列以增加整数索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31974942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:45:47  来源:igfitidea点击:

pandas - plot sorted column to increasing integer index

pythonpandas

提问by nivniv

Let's say I have a pandas series with numerical values. What's the shortest way to plot the sorted series against an increasing integer index?

假设我有一个带有数值的Pandas系列。根据递增的整数索引绘制排序序列的最短方法是什么?

The plot should show:

该图应显示:

x-axis: 0,1,2,3,4,...

x 轴:0,1,2,3,4,...

y-axis: the sorted values of the series.

y 轴:系列的排序值。

(please notice that I cannot plot it against the series' index, because the index is not necessarily an increasing index. In my case it's some id that I use for different reasons)

(请注意,我无法根据系列的索引绘制它,因为该索引不一定是递增的索引。在我的情况下,它是我出于不同原因使用的某个 id)

Thanks

谢谢

回答by paulperry

This will first sort the series and then plot, ignoring the index of the series:

这将首先对系列进行排序,然后绘制,忽略系列的索引:

ts = pd.Series(np.random.randn(100), index=pd.date_range('1/1/2000', periods=100)) ts.sort_values().plot(use_index=False)

ts = pd.Series(np.random.randn(100), index=pd.date_range('1/1/2000', periods=100)) ts.sort_values().plot(use_index=False)

回答by chris-sc

Quick and easy: You can add a column with increasing integers and use this as x-values:

快速简便:您可以添加一个整数递增的列,并将其用作 x 值:

# some dataframe df
df['int_index'] = range(len(df))

df.plot(x='int_index', y='sorted_values')

If you don't want to keep the index, drop it afterwards:

如果您不想保留索引,请在之后删除它:

df.drop('int_index', axis=1, inplace=True)

Helper function:

辅助功能:

The Pandas plotfunction does not take "external" data as indices. You can use matplotlib directly to plot as tnknepp showed, or keep the Pandas plotting (and formatting) with a helper function:

Pandasplot函数不会将“外部”数据作为索引。您可以直接使用 matplotlib 进行绘图,如 tnknepp 所示,或使用辅助函数保持 Pandas 绘图(和格式化):

def plot_sorted(series, **kwargs):
    df = pd.DataFrame({'data': series}).reset_index(drop=True)
    df.plot(**kwargs)

Using a wrapper like this you can quickly plot any Seriesand customize the plot by calling with the appropriate arguments that will be used to call the plotmethod. Examples:

使用这样的包装器,您可以快速绘制任何Series图形,并通过使用将用于调用plot方法的适当参数进行调用来自定义绘图。例子:

ts = pd.Series(np.random.randn(100), index=pd.date_range('1/1/2000', periods=100))
# default pandas plot (with integer indices)
plot_sorted(ts)

# scatter plot (using `data` for x and y)
plot_sorted(ts, x='data', y='data', kind="scatter")

Default plotScatter plot

默认绘图散点图

回答by tnknepp

For one line:

对于一行:

import pandas as pd
from pylab import *

fig = figure( figsize=(5,5) )
ax  = fig.add_subplot(111)

ax.plot( np.arange(df.shape[0]), df.sort(['A']).A )

This is assuming your dataframe is called df and you want to plot the data stored in column "A".

这是假设您的数据框称为 df 并且您想要绘制存储在“A”列中的数据。