remove frame & set colors for extreme values

#medium

Did you already wanted to remove some unused frame in your subplots?

Or maybe set some special colors for the values you consider outliers?

If the answer is not, then you'll probably need it! (soon)

If the answer is yes, then I have something for you


Having a subplot array when not all the frames (the "boxes") are needed is not something new. Neither is to use a colorbar when you want to clearly distinguish a value of 5000 from 5,000,000 by its color, when your range of colors has a maximum value well below these values.

Here I'll use 3 simple methods/attributes of matplotlib:

  • from mpl_toolkits.axes_grid1 import make_axes_locatable
  • plt.colorbar(im, cax=caxis, extend='both', extendfrac=0.2 ...)
  • axis.axis('off')

The first, make_axes_locatable, allows us to see the frame positioning and take advantage of it. By having its location, we can create an attached axis to it. The spacing between the original axis ("box") and the newly created will be seen as the separation between the plot and its colorbar: this is set by the pad argument. Other arguments can lead you to finer tuning, all of them listed here (matplotlib API web is your ally, kind of weird but loyal)

After that we will need to tell the colorbar which colors to use for the values above/below the maximum/minimum of the plot. These limits are managed by the attributes vmin and vmax of the imshow/scatter and other plotting varieties. Happily, the implementation is quite simple, as the following extract shows,

Now, don't forget we want to also experiment in removing a frame from our grid of subplots. This is far more simple: just use ax.axis('off') Easy, isn't?

Putting this all together allows us to create a plot on which the colorbars have customized colors for extrema values, and the unused frames will not bother us saying "did you forgot to plot some data, hum?"

The entire code for this can be found under my Github: https://github.com/paztronomer/blog/blob/master/code/p02_frame_and_colorbar/plot_colorbars_rmFrame.ipynb