Plt.A scatter plot is a data visualisation that displays the relationship between two variables. # formats them so they're labeled according to the meaning of the value. # Add a colorbar which positions ticks at the center of each color band and # (so that discontinous values map to the correct colors) So we must also put the ticks at the center of each bin # But, as-is, the ticks will be at the boundary limits, which will appear to # color index, and then use that to pick from our label list. # We use the normalizer we created to take land use values and convert to the # We need a tick formatter that takes the value `x` and maps it to a label. # Order should be respected here ! Or using another dict maybe could help. # Let's also define the description of each category : 1 (blue) is Sea 2 (red) is burnt, etc. Norm = (norm_bins, len(col_dict), clip=True) Norm_bins = np.insert(norm_bins, 0, np.min(norm_bins) - 1.0) # add one below the minimum # either side of a value to map that value to a particular color. This is because `BoundaryNorm` needs bins on # We must also add a bin at the bottom doesn't matter really where as long as # We create bins by adding 0.5 to each value (this assumes no two values are to do that, we have to normalize the image. # Note the colormap `cm` has no information as to the values we want each color import numpy as npĬm = ListedColormap(colors=list(col_dict.values())) I broke it down a bit to see what's what. Plt.scatter(x, y, answer has a lot of good stuff in it. In order to set cm as the default colormap with plt.set_cmap, I found it to be necessary to give it a name and register it, viz: cm = from_list('Set15', plt.cm.Set1(range(0,n)), n) Specifies to construct a map with n colors (the default being 256). The first n colors of that colormap, and the last n in the call to from_list import matplotlib, numpy as np, matplotlib.pyplot as pltįrom_list = _listĬm = from_list(None, plt.cm.Set1(range(0,n)), n)Ĭb = plt.colorbar(ticks=range(0,n), label='Group') The example also produces a colorbar with n discrete colors approprately labelled. With these maps it could be natural to use their first n colors to color scatter plots with n categories, as the following example does. Set1, e.g., has 9 easily distinguishable colors, and tab20 could be used for 20 colors. Some background is that matplotlib provides so-called qualitative colormaps, intended to use with discrete data. However I have found no way of eliminating the rather long-winded call to _list. It avoids calling BoundaryNorm as well as specifying norm as an argument to scatter and colorbar. I have been investigating these ideas and here is my five cents worth. Norm = (norm_bins, len_lab, clip=True)įmt = (lambda x, pos: labels)Ĭb = fig.colorbar(im, format=fmt, ticks=tickz) Norm_bins = np.insert(norm_bins, 0, np.min(norm_bins) - 1.0) Order should be respected here ! Or using another dict maybe could help. # We create a colormar from our list of colorsĬm = ListedColormap( for x in col_dict.keys()]) # Let's also design our color mapping: 1s should be plotted in blue, 2s in red, etc. It is not complicated but as it took me some time, it might help others not lossing as much time as I did :) import numpy as npįrom lors import ListedColormapĪ = np.reshape(, (2, 3)) This topic is well covered already but I wanted to add something more specific : I wanted to be sure that a certain value would be mapped to that color (not to any color). I personally think that with 20 different colors its a bit hard to read the specific value, but thats up to you of course. Spacing='proportional', ticks=bounds, boundaries=bounds, format='%1i')Īx.set_title('Well defined discrete colors')Īx2.set_ylabel('Very custom cbar ', size=12) Tag = 0 # make sure there are some 0 values to show up as greyĬmaplist = Ĭmap = _list( import numpy as npįig, ax = plt.subplots(1, 1, figsize=(6, 6)) # setup the plot That would be much easier to make 0 grey, but i couldnt get this to work with the scatter or the custom cmap.Īs an alternative you can make your own cmap from scratch, or read-out an existing one and override just some specific entries. The quirky bit (in my method) is making 0 showup as grey.įor images i often use the t_bad() and convert my data to a numpy masked array. You can create a custom discrete colorbar quite easily by using a BoundaryNorm as normalizer for your scatter.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |