简体   繁体   English

如何使用Python在单个图形上绘制多种数据?

[英]How can I plot multiple kind of data on a single graph with Python?

I'm aware this may be a newbie question but I was forced to learn data analysis through Python without a proper background knowledge (I tried vb.net 10 years ago, that's all), and I need to solve a problem very fast. 我知道这可能是一个新手问题,但是我被迫在没有适当背景知识的情况下通过Python学习数据分析(我10年前尝试过vb.net,仅此而已),我需要非常快速地解决问题。

I was asked to plot a graph about the speed of a vehicle during a period starting from a dataset like this: 我被要求从这样的数据集开始绘制一段时间内车辆速度的图表:

ID      CITY   SPEED          TIME
0       Milan   45   25/05/2018 17:35:30
1       Milan   60   25/05/2018 17:35:45
2       Milan   80   25/05/2018 17:36:00
....
2000    Rome    0    25/05/2018 21:05:15
2001    Rome    0    25/05/2018 21:05:30
2002    Rome    0    25/05/2018 21:05:45

Every dataset I need to analyze have an average of 3000/4000 rows and contain columns I don't need. 我需要分析的每个数据集平均有3000/4000行,并且包含我不需要的列。

Now I was able to import the dataset from a file Excel, I created 3 objects with data took from the columns I need: 现在,我能够从文件Excel导入数据集,我创建了3个对象,这些数据取自我需要的列:

speed = Dataset[['speed']]
time = Dataset[['time']]
city = Dataset[['city']]

What I need is to plot a graph that has the speed as the main data to be visualized, and then the time (on x-axes). 我需要绘制一个图形,该图形具有要作为可视化主要数据的速度,然后是时间(在x轴上)。 When the speed is 0 I also need to show the city. 当速度为0时,我还需要显示城市。

I know how to plot the graph of the speed, but I don't know what to do in order to get time and city where I need them. 我知道如何绘制速度曲线图,但是我不知道该怎么做才能在需要的地方获得时间和城市。

It should be something like this: graph I need to obtain 它应该是这样的: 我需要获取的图形

Any kind of help is really appreciated since I don't know what I need to do to reach this result. 真的很感谢任何帮助,因为我不知道该怎么做才能达到这个结果。

Thanks in advance! 提前致谢!

At first I assume that your time data is only in string format? 首先,我假设您的时间数据仅是字符串格式? Then you should transform it into a datetime format like: 然后,您应该将其转换为日期时间格式,例如:

Dataset['time'] = pd.to_datetime(Dataset['time'])

With this done you could plot your speed data explicitly over time like: 完成此操作后,您可以随时间显式绘制速度数据,例如:

plt.plot(Dataset['time'], Dataset['speed'])

Afterwards, I'd iterate over 然后,我要遍历

Dataset[Dataset['speed']==0]

and call plt.annotate() 并调用plt.annotate()

Over what datasubset to iterate exactly depends on your zero speed data: it looks like there are several entries one after another, so you should filter first, perhaps by some Dataset.groupby and just picking the first() entry of each group, possibly. 究竟要迭代的数据子集完全取决于您的零速度数据:看起来有一个接一个的多个条目,因此您应该首先进行过滤(也许通过一些Dataset.groupby过滤),并可能只选择每个组的first()条目。

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

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