9

I have this code:

plist = ['p5', 'p14', 'p23', 'p32', 'p41', 'p50', 'p59', 'p68', 'p77', 'p85', 'p95']


for pltcount in range(len(plist)):
    plt.plot(data1[pltcount], np.exp(data2)[pltcount], marker='o', label=str(plist[pltcount]))
plt.legend()
plt.show()

This is using the plt.style.use('fivethirtyeight') to make the plots nicer. I have found examples where I manually assign the colors. What if I want it to be automatic and from some well-known palettes?

enter image description here

cs95
  • 379,657
  • 97
  • 704
  • 746
maximusdooku
  • 5,242
  • 10
  • 54
  • 94

1 Answers1

10

How about the colors of the rainbow? The key here is to use ax.set_prop_cycle to assign colors to each line.

NUM_COLORS = len(plist)

cm = plt.get_cmap('gist_rainbow')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_prop_cycle('color', [cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
# Or,
# ax.set_prop_cycle(color=[cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)])
for i, p in enumerate(plist):
    ax.plot(data1[i], np.exp(data2)[i], marker='o', label=str(p))

plt.legend()
plt.show()

Borrowed from here. Other options possible.

cs95
  • 379,657
  • 97
  • 704
  • 746