如何绘制许多具有不同颜色的点?

时间:2019-08-19 06:54:33

标签: python pandas plot

我在数据框架中有很多要点。首先,我根据它们的MMSI(每个对象具有不同的MMSI)将它们分组,现在我想绘制它们,而每个图的颜色根据其MMSI而变化。 分组后,我有1024个不同的MMSI,因此我将有1024行。我希望这些线条的颜色不同。

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

2 个答案:

答案 0 :(得分:0)

如果您有seaborn

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

或使用基础matplotlib(来自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()

答案 1 :(得分:0)

如果您的目标是设置不同的颜色以使其可视化,我建议使用c中的plt.scatter参数,参数c可以采用数组,因此,您可以做的是将MMSI数组传递给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

相关问题