Home > OS >  Can any one help me fix this legend issue?
Can any one help me fix this legend issue?

Time:07-08

Can any expert help me merge the following legend? I would like all the two legends to be merged

Can any expert help me merge the following legend? I would like all the two legends to be merged

def dominates(bestRouteTime, num_buses):
    dominating = False
    for i in range(len(bestRouteTime)):
        if bestRouteTime[i] > num_buses[i]:
    return False
        if bestRouteTime[i] < num_buses[i]:
        dominating = True
    return dominating


def is_dominated(bestRouteTime,front):
    for num_buses in front:
        if dominates(num_buses,bestRouteTime):
            return True
    return False

def pareto_front(cand):
    front = set([])
    for i in cand:
       add_i = True
        for j in list(front):
            if dominates(i,j):
                front.remove(j)
            if dominates(j,i):
            add_i = False
        if add_i:
            front.add(i)
    front = list(front)
    front.sort()
    return front

if __name__ == "__main__":
    import random
    #random.seed(1)
    cand = [(random.random()**.25,random.random()**.25) for i in range(1000)]
    fig, ax = plt.subplots(figsize=(10, 8))
    ax.xlabel = "Travel Time (seconds)"
    ax.ylabel = "Number of Walking Buses"
    for (bestRouteTime, num_buses) in cand:
        x,y = (bestRouteTime*360, num_buses*10) 
        ax.plot(x,y,"bo")
    
    front = pareto_front(cand)
    ax.plot([x for (x,y) in front], [y for (x,y) in front], "ro", label="Pareto Font")
    fig.canvas.draw()
    ax.set_ylabel('Number of Walking Buses')
    ax.set_xlabel('Travel Time (seconds)')
    plt.title = ("Pareto Optimal Fronts (TSPTW))")
    legend = ax.legend(loc='best', shadow=True, fontsize='large')
    plt.show()

This is what got from my code

enter image description here

CodePudding user response:

Without complicating my life too much, I would capture the line artists (line1, line2 in the code below) and use them on the legend. In particular, use the last artist of the blue dots:

#random.seed(1)
cand = [(random.random()**.25,random.random()**.25) for i in range(1000)]
fig, ax = plt.subplots(figsize=(10, 8))
ax.xlabel = "Travel Time (seconds)"
ax.ylabel = "Number of Walking Buses"
for (bestRouteTime, num_buses) in cand:
    x,y = (bestRouteTime*360, num_buses*10) 
    line1, = ax.plot(x,y,"bo", label="Number of walking buses")

front = pareto_front(cand)
line2, = ax.plot([x for (x,y) in front], [y for (x,y) in front], "ro", label="Pareto Font")
fig.canvas.draw()
ax.set_ylabel('Number of Walking Buses')
ax.set_xlabel('Travel Time (seconds)')
plt.title = ("Pareto Optimal Fronts (TSPTW))")
legend = ax.legend(handles=(line1, line2), loc='best', shadow=True, fontsize='large')
plt.show()

CodePudding user response:

if __name__ == "__main__":
       # random.seed(1)
      cand = [(random.random()**.25,random.random()**.25) for i in range(1000)]
       #  cand  = np.random.randn(1000)
          fig, ax = plt.subplots(figsize=(10, 8))

      for (bestRouteTime, num_buses) in cand:
           x,y = (bestRouteTime*360, num_buses*10) 
          TSPTW, = ax.plot(x,y,"bo")

       front = pareto_front(cand)
       Font, = ax.plot([x for (x,y) in front], [y for (x,y) in front], "ro")
       fig.canvas.draw()
       ax.set_ylabel('Number of Walking Buses')
       ax.set_xlabel('Travel Time (seconds)')
       plt.title = ("Pareto Optimal Fronts (Modified TSPTW))")
       legend1 = plt.legend((TSPTW,Font), ["Generated Solutions","Pareto Set"],
       loc="upper left", shadow=True, fontsize='large')
       plt.gca().add_artist(legend1)
       plt.show()

New output

  • Related