简体   繁体   English

平滑绘制数据框的所有列

[英]smooth plotting all columns of a data-frame

I have a data frame of:我有一个数据框:

Index   Date        AA   BB   CC     DD    EE   FF
0       2019-01-15  0.0  -1.0  0.0   0.0   0.0  2.0
1       2019-01-17  0.0  -1.0  -1.0  -1.0  0.0  2.0
2       2019-01-22  1.0  -1.0  1.0   -1.0  0.0  2.0
3       2019-01-24  0.0  0.0   0.0   0.0   0.0  2.0
4       2019-01-29  1.0  0.0   -1.0  0.0   -1.0 2.0
5       2019-01-31  0.0  -1.0  0.0   0.0   0.0  2.0
6       2019-02-05  1.0  1.0   1.0   0.0   1.0  2.0
7       2019-02-12  2.0  1.0   1.0   0.0   2.0  2.0

which I'm plotting with:我正在策划:

dfs = dfs.melt('Date', var_name = 'cols', value_name = 'vals')
ax = sns.lineplot(x = "Date", y = 'vals', hue = 'cols', 
                  style = 'cols', markers = True, dashes = False, data = dfs)
ax.set_xticklabels(dfs['Date'].dt.strftime('%d-%m-%Y'))
plt.xticks(rotation = -90)
plt.tight_layout()
plt.show()

resulting:结果:

which is ugly.这是丑陋的。 I want to have the markers in the exact place as what is in the data-frame but the lines to be smoothed.我想将标记放在与数据框中的确切位置相同的位置,但要平滑线条。 I'm aware of scipy -> spline (eg here ), however that seems to be too much hassle to convert all the columns.我知道scipy -> spline (例如这里),但是转换所有列似乎太麻烦了。 There is also Pandas -> resample -> interpolate (eg here ) which is very close to what I want but I have to turn the Date column to index which I don't want to do...还有Pandas -> resample -> interpolate (eg here ) 这非常接近我想要的但我必须将Date列转换index我不想做的index ...

I would appreciate if you could help me know what is the best Pythonic way to do this.如果您能帮助我知道什么是最好的 Pythonic 方法,我将不胜感激。


PS A complete version of my code can be seenhere . PS我的代码的完整版本可以在这里看到。

I think you need to write a custom plotting function that iterates over all columns and plots interpolated data to specified axes instance.我认为您需要编写一个自定义绘图函数,该函数遍历所有列并将插值数据绘制到指定的轴实例。 Look at the following code:看下面的代码:

import pandas as pd
import numpy as np

# data = pd.read_clipboard()
# data.drop(['Index'], axis=1, inplace=True)

def add_smooth_plots(df, ax,  timecolumn='Date', interpolation_method='cubic', colors='rgbky'):
    from itertools import cycle
    ind = pd.to_datetime(df.loc[:, timecolumn])
    tick_labels =ind.dt.strftime("%Y-%m-%d")
    color = cycle(colors)
    for i, col in enumerate(df.columns):
        if col != timecolumn:
            c = next(color)
            s = pd.Series(df.loc[:, col].values, index=ind)
            intp = s.resample('0.5D').interpolate(method=interpolation_method)
            true_ticks = intp.index.isin(ind)
            vals = intp.values
            intp = intp.reset_index()
            ticks = intp.index[true_ticks]
            ax.plot(np.arange(len(vals)), vals, label=col, color=c)
            ax.set_xticks(ticks)
            ax.set_xticklabels(tick_labels.values, rotation=45)
            ax.legend(title='Columns')
    return ax

from matplotlib import pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)

add_smooth_plots(data, ax)

plt.show()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM