I am currently using matplotlib to plot a lot of lines on a graph, and I am using a colorbar as legend for the colors of the lines.
With this (minimal reproducible) code:
import matplotlib.pyplot as plt
import matplotlib.colors as col
intervals = [40,110,200,500,900]
lookbacks = [5,10,20,30,50,100,200,300,500,750,1000]
all_probas = [[100,90,80,70,70,60,60,55,50,50,45],
[90,80,80,70,70,65,62,60,60,60,55],
[10,50,80,70,65,60,60,58,59,50,48],
[0,5,10,20,80,90,95,90,80,70,60],
[0,0,2,3,5,8,20,40,60,80,60]]
fig = plt.figure(figsize=(19, 11))
ax = plt.gca()
ax.set_xlim([min(lookbacks), 1000])
ax.set_ylim([0, 105])
colormap = plt.cm.get_cmap('cool')
sm = plt.cm.ScalarMappable(cmap=colormap)
sm.set_clim(vmin=min(intervals), vmax=max(intervals))
cbar = plt.colorbar(sm, pad=0.02)
cbar.set_ticks(intervals)
cbar.ax.get_yaxis().labelpad = 25
cbar.ax.set_ylabel('Injection interval', rotation=270, fontdict={"fontsize": 16})
for p,l in zip(all_probas,intervals):
plt.plot(lookbacks, p, color=colormap((l-min(intervals))/max(intervals)))
plt.show()
The plot I am getting looks something like this:
Is it possible to change the "axis" of the colorbar legend to be logarithmic ? That way, the colors of the different lines would be more different and easier to read.
This would especially be more useful when I am plotting more lines.
To clarify, I would like something that looks like this (approximately):
Solution
import matplotlib.pyplot as plt
import matplotlib.colors as col
import matplotlib
intervals = [40,110,200,500,900]
lookbacks = [5,10,20,30,50,100,200,300,500,750,1000]
all_probas = [[100,90,80,70,70,60,60,55,50,50,45],
[90,80,80,70,70,65,62,60,60,60,55],
[10,50,80,70,65,60,60,58,59,50,48],
[0,5,10,20,80,90,95,90,80,70,60],
[0,0,2,3,5,8,20,40,60,80,60]]
fig = plt.figure(figsize=(19, 11))
ax = plt.gca()
ax.set_xlim([min(lookbacks), 1000])
ax.set_ylim([0, 105])
colormap = plt.cm.get_cmap('cool')
# Changes:
norm = col.SymLogNorm(0.01, vmin=min(intervals), vmax=max(intervals))
sm = plt.cm.ScalarMappable(norm=norm,cmap=colormap)
sm.set_array([])
cbar = plt.colorbar(sm, ticks=intervals, format=matplotlib.ticker.ScalarFormatter(),
shrink=1.0, fraction=0.1, pad=0.02, label="Injection interval")
# End of changes
for p,l in zip(all_probas,intervals):
plt.plot(lookbacks, p, color=colormap(norm(l)))
plt.show()
with resulting plot: