简体   繁体   English

Python 中的数据可视化

[英]Data Visualization in Python

Let's say I have traffic jams data like this:假设我有这样的交通拥堵数据:

交通拥堵数据

How to visualize my data with x = unique of time, y = speed.如何使用 x = 时间的唯一性,y = 速度来可视化我的数据。 And I have multiple plot of unique street A,B,C,D?我有多个独特街道 A、B、C、D 地块?

And if you have some recommendations for visualizing or serving my data with other parameters (like road type, alert, etc) I will really appreciate it.如果您有一些关于使用其他参数(如道路类型、警报等)可视化或提供我的数据的建议,我将非常感激。

Thank you!谢谢!

I can assure you this is not very elegant, but it gets the job done I think.我可以向你保证这不是很优雅,但我认为它完成了工作。 You will face issues for singular time values.您将面临奇异时间值的问题。 Added starting values to data because singular values + line plots don't do too good.向数据添加了起始值,因为奇异值 + 线图效果不太好。 Experimented with using scatter but that just screws labelling up.尝试使用 scatter 但这只会搞砸标签。

data = {'street': ['street A','street B','street C','street D','street A', 'street B', 'street C', 'street A', 'street C', 'street D'], 'time': [0,0,0,0,1, 1, 1, 2, 2, 2], 'speed': [0,0,0,0,3.22, 1.2, 2.3, 2.3, 2.1, 1.9], 'jams_level': [0,0,0,0,3, 1, 2, 2, 2, 1]}
df = pd.DataFrame(data,columns = ["street","time","speed","jams_level"])
street = list(df.street.unique()) #I am much more comfortable with lists

subdf = []
for i in street:
    subdf.append(df.loc[df["street"] == i].sort_values(by=["time"])) #Grouping dataframes by street

fig,ax = plt.subplots()

for i in subdf:
    i.plot(x="time",y="speed",ax=ax) #use pandas.plot method

ax.legend(street) #rename legend

This is a very hack and slash approach and probably isn't good enough.这是一种非常 hack and slash 的方法,可能还不够好。 Not very good with pandas data manipulation but hopefully it works.对 pandas 数据操作不是很好,但希望它能工作。 What it does is essentially split your original dataframe into "streets".它所做的基本上是将您的原始数据框拆分为“街道”。 And then plot speed vs time on the same subplot before renaming the legend for better reading.然后在重命名图例以便更好地阅读之前,在同一子图中绘制速度与时间的关系图。 Coloring can also be done but would require a little more logic.着色也可以完成,但需要更多的逻辑。 Then again, I am sure there is a much more easier solution out there.再说一次,我确信那里有一个更简单的解决方案。 Resulting plot:结果图:

在此处输入图像描述

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

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