1

sorry if this is a stupid question, but I can't find the solution for my problem. I have some points I want to plot and each of this points correspond to one variable. My question is: How can I plot each point in the same plot with unique color each and plot a legend.

This is the code I have so far:

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

function=['a', 'b', 'c', 'd', 'e']
acc_scores = [0.879, 0.748, 0.984, 0.944, 0.940]

fig, ax = plt.subplots()
colors= ['b', 'r', 'g', 'c', 'y'] #Colors I wanted to use for each data point
plt.plot([1,2,3,4,5], acc_scores, 'ro')
plt.axis([0, 6, 0.5, 1])
ax.set_xlabel('Functions', size=18)
ax.set_ylabel('Accuracy', size=18)
plt.show()

This code gives me the points, but all the same color.

Thanks for your help!

Leonor
  • 53
  • 6

1 Answers1

0

You're plotting all of your data as one line plot, and they're all the same color because you specified ro. If you want each point to be different, you can loop over each point and plot it individually. The label parameter is helpful for building a legend.

Try this:

import matplotlib.patches as mpatches
import matplotlib.pyplot as plt

function=['a', 'b', 'c', 'd', 'e']
acc_scores = [0.879, 0.748, 0.984, 0.944, 0.940]

fig, ax = plt.subplots()
colors= ['b', 'r', 'g', 'c', 'y'] #Colors I wanted to use for each data point
for x, y, c, f in zip([1,2,3,4,5], acc_scores, colors, function):
    plt.scatter(x, y, c=c, label=f)

plt.axis([0, 6, 0.5, 1])
ax.set_xlabel('Functions', size=18)
ax.set_ylabel('Accuracy', size=18)
plt.legend()
plt.show()

Output

Example Output

pault
  • 41,343
  • 15
  • 107
  • 149