Matplotlib: color strip on a contour without stripes

In matplotlib, I am looking to create an insert color bar to show the scale of my contour plot, but when I create a contour using a path, white bars run through the color bar, while when I use the path the color bar has the correct โ€œsmoothโ€ look :

Contour comparisons

How can I get this nice smooth color panel from the filled outline on my regular outline graph? I would also be fine with a filled path, where, as I think, the zero level can be set to white.

Here is the code to generate this example:

from numpy import linspace, outer, exp from matplotlib.pyplot import figure, gca, clf, subplots_adjust, subplot from matplotlib.pyplot import contour, contourf, colorbar, xlim, ylim, title from mpl_toolkits.axes_grid1.inset_locator import inset_axes # Make some data to plot - 2D gaussians x = linspace(0, 5, 100) y = linspace(0, 5, 100) g1 = exp(-((x-0.75)/0.2)**2) g2 = exp(-((y-4.25)/0.1)**2) g3 = exp(-((x-3.5)/0.15)**2) g4 = exp(-((y-1.75)/0.05)**2) z = outer(g1, g2) + outer(g3, g4) figure(1, figsize=(13,6.5)) clf() # Create a contour and a contourf for ii in range(0, 2): subplot(1, 2, ii+1) if ii == 0: ca = contour(x, y, z, 125) title('Contour') else: ca = contourf(x, y, z, 125) title('Filled Contour') xlim(0, 5) ylim(0, 5) # Make the axis labels yt = text(-0.35, 2.55, 'y (units)', rotation='vertical', size=14); xt = text(2.45, -0.4, 'x (units)', rotation='horizontal', size=14) # Add color bar ains = inset_axes(gca(), width='5%', height='60%', loc=2) colorbar(ca, cax=ains, orientation='vertical', ticks=[round(xx*10.0)/10.0 for xx in linspace(0, 1)]) if ii ==1: ains.tick_params(axis='y', colors='#CCCCCC') subplots_adjust(left=0.05, bottom=0.09, right=0.98, top=0.94, wspace=0.12, hspace=0.2) show() 

Change Now I understand that at lower resolutions the behavior of white stripes is difficult to distinguish from some transparency of light. Here is an example with only 30 contour lines, which makes the problem more obvious:

Fewcontours

Change 2 . Although Iโ€™m still interested in figuring out how to do this in the general general case (for example, if there are negative values), in my particular case I decided that I could effectively create something similar to what I want by simply setting the levels of the filled path to start at level zero:

ca = contourf(x, y, z, levels=linspace(0.05, 1, 125))

Which basically looks like what I want:

Klugecontours

+7
python matplotlib
source share
1 answer

A simple hack is to set the thickness of the lines in the color bar to some higher value. For example. saving the colorbar object as cb and adding the following lines to your example

 for line in cb.lines: line.set_linewidth(3) 

gives enter image description here

+4
source share

All Articles