Reputation: 33
I am plotting multiple plots generated from a for
loop. I want each plot to be in a different shade of the same color so that it can be easily identified. I don't know how many plots, because it depends on a user selection (There can be 5 plots or 7 or 8 or etc.).
In this MWE, I used matplotlib blues
to plot the graphs in different shads of blue. As you can see, the shades are not very different from each other. What parameter do I have to change so that the shades are noticeably different from each other?
(Now, I can "not" use colors at all and let the matplotlib use its default colors by removing color = blues(xaxis[j])
. But I am looking for shades, not totally different colors.)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
plt.close("all")
blues = cm.get_cmap('Blues', 10)
fig, ax = plt.subplots()
l1 = np.array([1,2,3,4,5,6,7,8,9,10])
j=0
xaxis = np.linspace(0.5,1, 1000)
for i in range(0,10):
l0 = l1*i
print(l0)
ax.plot(l1, l0,color = blues(xaxis[j]),label='plot')
j+=1
ax.legend()
Upvotes: 0
Views: 1476
Reputation: 13417
By specifying cm.get_cmap('Blues', 10)
you are creating a colormap of 10 distinct shades of blue. You can access each specific shade like so:
from matplotlib import cm
blues = cm.get_cmap("Blues", 10)
for i in range(blues.N):
print(f'RGBA {i}:' *(f"{v:.2f}" for v in blues(i)))
# RGBA 0: 0.97 0.98 1.00 1.00
# RGBA 1: 0.88 0.93 0.97 1.00
# RGBA 2: 0.80 0.87 0.94 1.00
# RGBA 3: 0.67 0.81 0.90 1.00
# RGBA 4: 0.51 0.73 0.86 1.00
# RGBA 5: 0.35 0.63 0.81 1.00
# RGBA 6: 0.22 0.53 0.75 1.00
# RGBA 7: 0.11 0.42 0.69 1.00
# RGBA 8: 0.03 0.30 0.59 1.00
# RGBA 9: 0.03 0.19 0.42 1.00
The issue here is that the steps of your xaxis
variable are too small and are converging to the same exact shade of blue due to rounding.
import numpy as np
from matplotlib import cm
blues = cm.get_cmap("Blues", 10)
xaxis = np.linspace(.5, 1, 1000)
for i in range(10):
print(f"RGBA {xaxis[i]:.3f}:", *(f"{v:.2f}" for v in blues(xaxis[i])))
# RGBA 0.500: 0.35 0.63 0.81 1.00
# RGBA 0.501: 0.35 0.63 0.81 1.00
# RGBA 0.501: 0.35 0.63 0.81 1.00
# RGBA 0.502: 0.35 0.63 0.81 1.00
# RGBA 0.502: 0.35 0.63 0.81 1.00
# RGBA 0.503: 0.35 0.63 0.81 1.00
# RGBA 0.503: 0.35 0.63 0.81 1.00
# RGBA 0.504: 0.35 0.63 0.81 1.00
# RGBA 0.504: 0.35 0.63 0.81 1.00
# RGBA 0.505: 0.35 0.63 0.81 1.00
Therefore, your xaxis
variable should not intermediate the color selection instead, you can simplify your code like so:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
blues = cm.get_cmap("Blues", 10)
fig, ax = plt.subplots()
l1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
for i in range(blues.N):
ax.plot(l1, l1 * i, color=blues(i), label=f"line {i}")
ax.legend()
plt.show()
Upvotes: 1