Let's say I have traffic jams data like this:
How to visualize my data with x = unique of time, y = speed. And I have multiple plot of unique street 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.
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. Not very good with pandas data manipulation but hopefully it works. 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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.