Skip to content

Instantly share code, notes, and snippets.

@natecostello
Created May 22, 2024 00:25
Show Gist options
  • Save natecostello/f24846c2c5ca62c4897ac00299393f33 to your computer and use it in GitHub Desktop.
Save natecostello/f24846c2c5ca62c4897ac00299393f33 to your computer and use it in GitHub Desktop.
A script that shows how to produce color schemes for plotting distinct traces from two populations (e.g., pass, fall) #matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np
# Generate some data
np.random.seed(0)
x = np.linspace(0, 10, 100)
y1 = [np.sin(x + i) for i in range(5)] # Periodic dataset
y2 = [np.exp(-0.1 * (x + i)) for i in range(5)] # Non-periodic dataset
def plot_with_colormaps(color1, color2, color3, color4):
# Define two custom colormaps
cmap1 = mcolors.LinearSegmentedColormap.from_list("colormap1", [color1, color2], N=5)
cmap2 = mcolors.LinearSegmentedColormap.from_list("colormap2", [color3, color4], N=5)
# Plot the traces for each dataset
for i in range(5):
plt.plot(x, y1[i], color=cmap1(i / 4), label=f'Dataset 1 - Trace {i+1}') # Normalize i to [0, 1]
for i in range(5):
plt.plot(x, y2[i], color=cmap2(i / 4), label=f'Dataset 2 - Trace {i+1}') # Normalize i to [0, 1]
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.show()
# Call the function with different color pairs
plot_with_colormaps("purple", "pink", "cyan", "yellow")
plot_with_colormaps("darkred", "yellow", "darkblue", "lightgreen")
plot_with_colormaps("navy", "aqua", "maroon", "lime")
plot_with_colormaps("darkgreen", "aqua", "darkred", "orange")
plot_with_colormaps("black", "white", "red", "blue")
plot_with_colormaps("darkviolet", "lavender", "teal", "mintcream")
plot_with_colormaps("midnightblue", "skyblue", "darkolivegreen", "honeydew")
plot_with_colormaps("indigo", "violet", "darkslategray", "aquamarine")
plot_with_colormaps("darkmagenta", "plum", "darkcyan", "paleturquoise")
plot_with_colormaps("saddlebrown", "wheat", "navy", "aliceblue")
plot_with_colormaps("darkgoldenrod", "lightyellow", "darkblue", "lightcyan")
plot_with_colormaps("maroon", "mistyrose", "darkgreen", "palegreen")
plot_with_colormaps("darkred", "salmon", "darkslateblue", "lightsteelblue")
plot_with_colormaps("crimson", "pink", "royalblue", "lightblue")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment