0

I have a lot of points in dataframe. at first, I've grouped them based on their MMSI(each object has different MMSI) now I would like to plot them whereas the color of each plot varies according to its MMSI. after grouping, I have 1024 different MMSI thus I will have 1024 line. I want the color of these lines to be different.

def pl(x):
    display = plt.plot(x['X'],x['Y'])
    return display
Final_data.groupby('MMSI').apply(pl)

My output is like this but I think It can be better

My sample data is like this

Danial
  • 7
  • 4
  • what do you use for plotting? matplotlib? what is wrong with the information provided in the documentation? – Piglet Aug 19 '19 at 07:06
  • @Piglet. yes, I'm using matplotlib. nothing is wrong. I don't know how to visualize my data properly – Danial Aug 19 '19 at 07:09

2 Answers2

0

If you have seaborn:

import seaborn as sns
sns.lineplot(x='X', y='Y',
              hue='MMSI', data=Final_data); 

or, using base matplotlib (from this answer):

import matplotlib.pyplot as plt

groups = Final_data.groupby('MMSI')

# Plot
fig, ax = plt.subplots()
for name, group in groups:
    ax.plot(group.X, group.Y, '-', label=name)
ax.legend()

plt.show()
Ted
  • 1,189
  • 8
  • 15
  • I've used your first option but I get the bellow error. AttributeError: module 'seaborn' has no attribute 'lineplot' – Danial Aug 19 '19 at 07:44
  • Ok, you'll have to upgrade your seaborn to version 0.9 (`!conda install -y -c anaconda seaborn=0.9.0` inside a jupyter notebook) – Ted Aug 19 '19 at 08:03
0

If your goal is to set different colours to visualize, what i can suggest is to use the parameter of c in the plt.scatter, the parameter c can take in an array, so what you can do is to pass in the array of MMSI to c.

import matplotlib.pyplot as plt

X = [230030.4, 231587.71, 233648]
Y = [4000858,4000155, 3999243]

plt.scatter(X,Y, c = X)
plt.show()

enter image description here

Axois
  • 1,961
  • 2
  • 11
  • 22