Skip to content

Instantly share code, notes, and snippets.

@blaylockbk
Created July 13, 2020 21:45
Show Gist options
  • Save blaylockbk/aeedd342376ef725fa66f6b3d7f46321 to your computer and use it in GitHub Desktop.
Save blaylockbk/aeedd342376ef725fa66f6b3d7f46321 to your computer and use it in GitHub Desktop.
Matplotlib's standard color cylce is ok if your data lines are distinct, but if each line is related to the others, then it is best to use a sequential color cycle.
a = np.arange(0, 3*np.pi, .1)
loops = range(5)
# Choose a colormap and create a list of colors with same length as our loop.
cmap = plt.get_cmap("YlGnBu")
colors = [cmap(i) for i in np.linspace(.15, .85, len(loops))]
fig, (ax1, ax2) = plt.subplots(1,2)
for i, color in zip(loops, colors):
ax1.plot(np.sin(a)*(i-1.5), label=i)
ax2.plot(np.sin(a)*(i-1.5), color=color, label=i)
ax1.set_title('No apparent relation')
ax2.set_title('Colors are sequential')
ax1.legend()
ax2.legend()
@blaylockbk
Copy link
Author

Matplotlib's standard color cylce is ok if each line represents distinct, separate entities. But if each line is related to the others, then it is best to use a sequential color cycle.

Consider this example where each line is the same function, just multiplied by a different constant in each loop. The left image is the standard colormap, and the colors do not indicate any apparent relationship between each line. But the right image has a sequential colormap that helps illustrate there is a relationship between each line.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment