Reputation: 6668
A follow-up question from my question here. I created red contourlines every 10 lines, but now I want to have these lines in the colorbar. I know that you can add the lines in the colobar by:
CS2 = pyplot.contour(X,Y,Z,locator=ticker.LogLocator(), colors=color_levels, levels=levels, lw=2,norm=colors.LogNorm(),vmin=1,vmax =Z.max())
cbar.add_lines(CS2)
(I don't think I need to specify all the variables here, it's just that you add the result from the contour
function in the cbar
thingy.
However, that will result in adding all contourlines to the colorbar, but I just want to have the red ones. Slicing them does not work:
>>> CS2=CS2[5::10]
AttributeError: QuadContourSet instance has no attribute '__getitem__'
Is it possible to only add certain lines to the colorbar?
Upvotes: 0
Views: 326
Reputation: 6668
I figured it out by myself, but not in the nicest way imaginable. I created two different contourplots, one for black and one for red lines. Only the red ones are given to the colorbar. It works, but it can be better, I think. The relevant part of the code is the following:
levels = np.logspace(0,np.log10(Z.max()), 100 )[30:80]
levels_black = [level for i,level in enumerate(levels) if (i+5) % 10 != 0]
levels_red = [level for i,level in enumerate(levels) if (i+5) % 10 == 0]
CS2_black = pyplot.contour(X,Y,Z,locator=ticker.LogLocator(), colors='k', levels=levels_black, lw=2,norm=colors.LogNorm(),vmin=1,vmax =Z.max())
CS2_red = pyplot.contour(X,Y,Z,locator=ticker.LogLocator(), colors='r', levels=levels_red, lw=2,norm=colors.LogNorm(),vmin=1,vmax =Z.max())
cbar.add_lines(CS2_red)
This will produce the following image, exactly as I wanted:
Upvotes: 1