0

I am a bit surprised by plot legends color assignment. I am plotting values and assigning a color in loop. The code is pasted below.

   import matplotlib.pyplot as plt
    angles2plot = [0.5, 1.0, 2.0, 3.0, 5.0, 8.0, 10.0, 15.0, 30.0,
                   150.0, 165.0, 170.0, 172.0, 175.0, 177.0, 178.0, 179.0, 180.0]
    find_ang_step = {}
    step = 0
    for i in angles2plot:
        find_ang_step[str(i)] = step
        step += 1

    fig, ax = plt.subplots(figsize=[16, 2])
    for ang, val in find_ang_step.items():
        ax.scatter(val, 0, s=6, c=val, cmap='jet_r', vmin=0, vmax=len(angles2plot),
                   label=ang)
    ax.set_ylim(-1, 1)
    ax.legend(ncol=len(angles2plot), markerscale=2.5, labelspacing=0,
              handletextpad=0, columnspacing=0.1, fancybox=True,
              loc=[0.01, 0.7])
    plt.tight_layout()

The issue is that the legend shows all colors as the same if I run the entire code at once. However, the funny part is that when I first make the plot (until lines 1-14) and then put legend later (lines 15 till end), it is coming out to be right. Any idea?

Daniel_Knights
  • 7,940
  • 4
  • 21
  • 49
Vinod Kumar
  • 1,383
  • 1
  • 12
  • 26

0 Answers0