1

Let's say I have traffic jams data like this:

traffic jams data

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!

2
  • I would think about first classifying the types of streets, then simply a speed vs time plot. Don't think this question should be here but visualizations of data is entirely dependent on what you want to find out or show. Commented Jan 21, 2020 at 8:43
  • Hello, thanks for your response. But, could we make multiple line plots of unique streets in one chart? I mean, street A,B,C,D as a label? Commented Jan 22, 2020 at 2:44

1 Answer 1

1

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:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Is this working for you or do you need a cleaner solution?
sorry for my late reply. I understood your explanation, thank you a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.