如何使用熊猫在python中生成平滑曲线

时间:2018-08-20 12:55:57

标签: python pandas plot

我正在尝试绘制一条平滑曲线,其中x轴是一天中的时间,y轴是登录尝试的次数,我在一个计数器中具有登录尝试的次数和尝试的时间,该计数器转换为一个熊猫数据框,我正在使用以下代码,但未生成所需的图形

d = Counter(times)
key = d.keys()
df = pd.DataFrame(d, key)
df.drop(df.columns[1:], inplace=True)
df.plot()
plt.show()

这反过来会产生下图

enter image description here

2 个答案:

答案 0 :(得分:0)

看看下面的代码(这是函数图的代码),我想说的是,如果您不指定x和y,则不会绘制任何东西。尝试指定x和y,或至少指定y。它应该给你一些东西

class FramePlotMethods(BasePlotMethods):
"""DataFrame plotting accessor and method
Examples
--------
>>> df.plot.line()
>>> df.plot.scatter('x', 'y')
>>> df.plot.hexbin()
These plotting methods can also be accessed by calling the accessor as a
method with the ``kind`` argument:
``df.plot(kind='line')`` is equivalent to ``df.plot.line()``
"""

def __call__(self, x=None, y=None, kind='line', ax=None,
             subplots=False, sharex=None, sharey=False, layout=None,
             figsize=None, use_index=True, title=None, grid=None,
             legend=True, style=None, logx=False, logy=False, loglog=False,
             xticks=None, yticks=None, xlim=None, ylim=None,
             rot=None, fontsize=None, colormap=None, table=False,
             yerr=None, xerr=None,
             secondary_y=False, sort_columns=False, **kwds):
    return plot_frame(self._data, kind=kind, x=x, y=y, ax=ax,
                      subplots=subplots, sharex=sharex, sharey=sharey,
                      layout=layout, figsize=figsize, use_index=use_index,
                      title=title, grid=grid, legend=legend, style=style,
                      logx=logx, logy=logy, loglog=loglog, xticks=xticks,
                      yticks=yticks, xlim=xlim, ylim=ylim, rot=rot,
                      fontsize=fontsize, colormap=colormap, table=table,
                      yerr=yerr, xerr=xerr, secondary_y=secondary_y,
                      sort_columns=sort_columns, **kwds)

答案 1 :(得分:0)

使用matplotlib,seaborn,bokeh等库时,数据的可视化非常不直观。感谢Jake VanderPlas,这里有Altair,这是一个非常简单的声明性库。只需将未汇总的日期数据加载到df。假设您有一列非汇总的日期,则可以执行以下操作:

#pip install altair 
import altair as alt

alt.Chart(df).transform_aggregate(
    count='count()',
    groupby=['date']
).mark_line().encode(
    x='date',
    y='count()',\
    tooltip='count:Q'
)