绘制不同的组 [英] Plotting Different Groups with Plotly

查看:74
本文介绍了绘制不同的组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中有一个数据框,看起来像这样:

I have a dataframe in python that looks a bit like this:

Device   Date      Reading
Device1  1/02/17   100.33
Device1  2/02/17   300.23
Device1  3/02/17   99.00
Device2  1/02/17   11.24
Device2  2/02/17   654.00
Device2  3/02/17   4543.4
Device3  1/02/17   3243.5
Device3  2/02/17   545.43
Device3  3/02/17   4545.0

它基本上具有设备,日期和读数.它比我给的片段大得多.

It basically has devices, dates and a reading. It's much larger than the snippet I've given.

我正在尝试绘制图形,最好使用plotly,因为它是交互式的并且可以缩放等,这对于这种类型的数据集是理想的.

I am trying to plot, preferably using plotly because it is is interactive and allows me to zoom, etc, which is ideal for this type of dataset.

我想在X轴上绘制日期,在Y轴上读取日期,并为每个带有图例的设备设置单独的行(颜色不同).但是,所有内容都在一个数据框中,并且我不想手动添加跟踪,这会因为数据集的大小而变得很久.

I want to plot the date on the X axis, reading on the Y axis, and have individual lines (which are different coloured) for each device with a legend. However, everything is in one data frame and I don't want to manually add traces, which would take ages because of the size of my dataset.

我一直在寻找解决方案,似乎没有任何效果.我已经尝试过密密麻麻的网站,袖扣,但它似乎很复杂,在线信息很少(袖扣).在R中,我会做类似colour=device的操作来表示我希望按设备对它进行排序,但是我似乎无法在Python中解决它.

I have searched high and low for a solution and nothing seems to work. I have tried the plotly site, cufflinks, but it seems quite complex and there's little info online (for cufflinks). In R, I would do something like colour=device to signify that I want different it to be sorted by device, but I can't seem to work it out in Python.

有人可以建议(记住所有内容都在一个数据框中)吗?

Could anybody advise (remember everything is in one data frame)?

推荐答案

我发现使用pd.groupby方法是在循环中生成跟踪的一种非常方便的方法.

I found out that using pd.groupby method is a pretty convenient way to generate traces in loops.

import pandas as pd
import plotly.graph_objs as go
from plotly.offline import iplot, init_notebook_mode

init_notebook_mode()

# Copy OP's example and load from clipboard
df = pd.read_clipboard()

grouped = df.groupby('Device')
data = [go.Trace(dict(x=values['Date'], y=values['Reading'], name=key,
                       mode='line')) for key, values in grouped]
iplot(data)

您还可以通过预先定义设备的字典来设置所需的颜色:例如,颜色.

You may also set the color you want by defining beforehand a dict of device: color, for example.

这篇关于绘制不同的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆