gpt4 book ai didi

python - 如何根据连续数据的存在来绘制线条

转载 作者:行者123 更新时间:2023-11-30 22:37:49 25 4
gpt4 key购买 nike

我有一个如下所示的数据集:

+------------+--------+
| trend_name | date |
+------------+--------+
| dogs | 5/3/17 |
| cats | 5/3/17 |
| owls | 5/3/17 |
| dogs | 5/4/17 |
| cats | 5/4/17 |
| tigers | 5/4/17 |
| cats | 5/5/17 |
| bears | 5/5/17 |
| giraffes | 5/5/17 |
+------------+--------+

我想创建一个在 y 轴上具有 trend_name 和在 x 轴上具有 date 的绘图,并使用连接持续 >1 个周期的趋势的线趋势的同一平面和仅在单个时期存在的趋势的点,如果特定时期不存在趋势则什么也不存在。

情节看起来像这样: enter image description here

我简单地尝试了 t.plot(x='date', y='trend_name') 但当然没有数据,所以它抛出了错误。

这种类型的图是否有一个特定的名称,以便我可以找到更好的资源,或者有人对如何完成此任务有建议吗?

更新:

t 是一个像这样的 pandas 数据框,但遵循与上面的模拟数据框类似的模式:

enter image description here

t.plot(x='datetime_collected', y='name') 产量:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-95-d2a37de17ec0> in <module>()
----> 1 t.plot(x='datetime_collected', y='name')

/usr/local/lib/python2.7/site-packages/pandas/tools/plotting.pyc in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
3772 fontsize=fontsize, colormap=colormap, table=table,
3773 yerr=yerr, xerr=xerr, secondary_y=secondary_y,
-> 3774 sort_columns=sort_columns, **kwds)
3775 __call__.__doc__ = plot_frame.__doc__
3776

/usr/local/lib/python2.7/site-packages/pandas/tools/plotting.pyc in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds)
2641 yerr=yerr, xerr=xerr,
2642 secondary_y=secondary_y, sort_columns=sort_columns,
-> 2643 **kwds)
2644
2645

/usr/local/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _plot(data, x, y, subplots, ax, kind, **kwds)
2468 plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds)
2469
-> 2470 plot_obj.generate()
2471 plot_obj.draw()
2472 return plot_obj.result

/usr/local/lib/python2.7/site-packages/pandas/tools/plotting.pyc in generate(self)
1039 def generate(self):
1040 self._args_adjust()
-> 1041 self._compute_plot_data()
1042 self._setup_subplots()
1043 self._make_plot()

/usr/local/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _compute_plot_data(self)
1148 if is_empty:
1149 raise TypeError('Empty {0!r}: no numeric data to '
-> 1150 'plot'.format(numeric_data.__class__.__name__))
1151
1152 self.data = numeric_data

TypeError: Empty 'DataFrame': no numeric data to plot

最佳答案

这可能不是最优雅的解决方案,特别是因为我对 pandas 不太熟悉。但无论如何,这是一个为您的绘图限制创建辅助数据框的解决方案(如果您想忽略当前时间窗口中未表示的数据点,这是不可避免的):

import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

# dummy data
dat = pd.DataFrame({'beast': ['dog','cat','owl','dog','cat','tiger','cat','bear','giraffe','unicorn'],
'collected': pd.to_datetime(['2016-03-09']*3 + ['2016-04-05']*3 + ['2016-05-05']*3 + ['2016-06-06'])})

# plotting date interval
t1,t2 = (pd.to_datetime(t) for t in ('2016-03-09','2016-05-05'))

# create auxiliary dataframe for plotting
dat_tmp = dat[(t1<=dat.collected) & (dat.collected<=t2)] # filtered between t1 and t2
beast_id,beasts = zip(*enumerate(dat_tmp.beast.unique()))

# indexing step: see http://stackoverflow.com/a/22346955
dat_tmp = dat_tmp.merge(pd.DataFrame({'beast': beasts, 'beast_id': beast_id}),on='beast',how='left')
dat_tmp = dat_tmp.pivot(index='collected',columns='beast',values='beast_id')

# plot
dat_tmp.plot(style='.-')

def format_fn(tick_val, tick_pos):
'''uses items in the list `beasts` to set yticklabels'''
if int(tick_val) in beast_id:
return beasts[int(tick_val)]
else:
return ''

plt.gca().yaxis.set_major_formatter(FuncFormatter(format_fn))
plt.show()

result

如您所见,格式改进仍有很大空间:隐藏不相关的 x 刻度、缩小一点以完全显示所有点、移动图例等,但这些都是微不足道的整容。

至于我整理的虚拟示例(我建议您下次也这样做,让其他人更容易解决您的问题),我们从这个数据框开始:

     beast  collected
0 dog 2016-03-09
1 cat 2016-03-09
2 owl 2016-03-09
3 dog 2016-04-05
4 cat 2016-04-05
5 tiger 2016-04-05
6 cat 2016-05-05
7 bear 2016-05-05
8 giraffe 2016-05-05
9 unicorn 2016-06-06

请注意图中完全缺失的 unicorn 数据点。在索引/合并步骤之后,我们最终得到

     beast  collected  beast_id
0 dog 2016-03-09 0
1 cat 2016-03-09 1
2 owl 2016-03-09 2
3 dog 2016-04-05 0
4 cat 2016-04-05 1
5 tiger 2016-04-05 3
6 cat 2016-05-05 1
7 bear 2016-05-05 4
8 giraffe 2016-05-05 5

正如您所看到的,每个点都用给定动物的整数索引进行了注释。我们需要这个,因为这是我们绘图的 y 轴所需的数据。旋转后最终结果为

beast       bear  cat  dog  giraffe  owl  tiger
collected
2016-03-09 NaN 1.0 0.0 NaN 2.0 NaN
2016-04-05 NaN 1.0 0.0 NaN NaN 3.0
2016-05-05 4.0 1.0 NaN 5.0 NaN NaN

其中的列将绘制为单独的线。可能有更短的操作过程可以产生相同或同等有用的数据帧,但这就是我所拥有的。好处是数据集中的 NaN 将自动强制执行“数据连续可用的行”规则。

关于python - 如何根据连续数据的存在来绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43765031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com