0

I want to make two legend which each contains 2 scatter object. The reason I question it is I will make pick_event to on/off specific matplotlib objects, since there are much objects to show on legend I thought this is the best way to make it

import numpy as np
import matplotlib.pylab as plt
import matplotlib.cm as cm
import seaborn as sns
import pandas as pd
data={
"Latitude":[35.6, 35.67, 35.61, 35.72, 35.78, 35.79, 35.95, 37.28867, 
37.31241, 37.33485, 37.3558, 37.3651, 37.38354, 37.40094, 37.41306, 
37.4241, 37.43555, 37.44525, 37.06979, 37.60126, 37.61556, 37.62994, 
37.63994, 37.64905, 37.66211, 37.66667, 37.665, 37.65491, 37.64287, 
37.62533, 37.61658, 37.60723, 37.59922, 35.6196, 35.62939, 35.63406, 
35.641, 35.66185, 35.68741, 35.71321, 35.73033, 35.74544, 35.76791, 
35.78028, 35.79574, 35.80316, 35.81076, 34.96701, 34.96326, 34.95957, 
34.95561, 34.95204, 34.94641, 34.94101, 34.93452, 34.91613, 34.89137, 
37.20236, 37.20992, 37.22294, 37.23477, 37.2453, 37.26677, 37.2795, 
37.29514, 37.31581, 37.56294, 37.57109, 37.58647, 37.60167, 37.60941, 
37.60169, 37.59192, 37.57231, 37.55709, 37.54173, 37.52888, 37.52115, 
37.51189, 37.50355]
,"Longitude":[127.8, 127.85, 127.83, 127.86, 127.9, 127.9, 127.91, 
126.61343, 126.59248, 126.57253, 126.55402, 126.54579, 126.52942, 
126.51394, 126.50321, 126.49343, 126.4832, 126.47455, 126.94527, 126.567, 
126.56943, 126.5834, 126.59569, 126.60709, 126.62382, 126.64011, 
126.65541, 126.66862, 126.68107, 126.70383, 126.71476, 126.72658, 
126.73673, 128.75347, 128.78952, 128.80686, 128.82837, 128.84983, 
128.87214, 128.89475, 128.90352, 
128.89961, 128.88096, 128.86227, 128.83462, 128.82122, 128.80766, 
128.88168, 128.85657, 128.83284, 128.80797, 128.78499, 128.74895, 
128.7124, 128.69139, 128.67857, 128.67531, 126.50506, 126.5244, 126.54793, 
126.56916, 126.57851, 126.57653, 126.57384, 126.56943, 126.55492, 
126.22403, 126.23852, 126.26604, 126.29345, 126.31262, 126.33167, 
126.34301, 126.36128, 126.37496, 126.38869, 126.4001, 126.40702, 
126.41526, 126.42266]
,"flightid":['kor_fli156', 'kor_fli156', 'kor_fli156', 'kor_fli156', 
'kor_fli156', 'kor_fli156', 'kor_fli156', 'cha_fli133', 'cha_fli133', 
'cha_fli133', 'cha_fli133', 'cha_fli133', 'cha_fli133', 'cha_fli133', 
'cha_fli133', 'cha_fli133', 'cha_fli133', 'cha_fli133', 'cha_fli144', 
'jap_fli146', 'jap_fli146', 'jap_fli146', 'jap_fli146', 'jap_fli146', 
'jap_fli146', 'jap_fli146', 'jap_fli146', 'jap_fli146', 'jap_fli146', 
'jap_fli146', 'jap_fli146', 'jap_fli146', 'jap_fli146', 'usa_fli136', 
'usa_fli136', 'usa_fli136', 'usa_fli136', 'usa_fli136', 'usa_fli136', 
'usa_fli136', 'usa_fli136', 'usa_fli136', 'usa_fli136', 'usa_fli136', 
'usa_fli136', 'usa_fli136', 'usa_fli136', 'fran_fli56', 'fran_fli56', 
 'fran_fli56', 'fran_fli56', 'fran_fli56', 'fran_fli56', 'fran_fli56', 
'fran_fli56', 'fran_fli56', 'fran_fli56', 'uk_fli2', 'uk_fli2', 'uk_fli2', 
'uk_fli2', 'uk_fli2', 'uk_fli2', 'uk_fli2', 'uk_fli2', 'uk_fli2', 
'denmak_fli49', 'denmak_fli49', 'denmak_fli49', 'denmak_fli49', 
'denmak_fli49', 'denmak_fli49', 'denmak_fli49', 'denmak_fli49', 
'denmak_fli49', 'denmak_fli49', 'denmak_fli49', 'denmak_fli49', 
'denmak_fli49', 'denmak_fli49']
}
data=pd.DataFrame(data)
np.random.seed(1)
names=np.random.choice(data["flightid"].unique(),5)
fig,ax=plt.subplots()
times=0
colors=cm.rainbow(np.linspace(0,1,5))
for dex,name in enumerate(names):
    temp=data.query("flightid==@name")
    a=ax.scatter(x=temp["Latitude"],
    y=temp["Longitude"],marker="v",color=colors[dex])
plt.show()

The side is the picture resulted from above code enter image description here

TAEK
  • 1
  • 2
  • 2
    we can't see your image. And we can't run your code because we don't have your file `csv`. Better put example data directly in code. – furas Jul 27 '22 at 18:37
  • There is a way to determine this by using a counter in the loop process as described in [this answer](https://stackoverflow.com/questions/19385639/duplicate-items-in-legend-in-matplotlib). – r-beginners Jul 28 '22 at 02:00

0 Answers0