Change colormap freemat7/13/2023 ![]() #speed = np.ma.masked_where(speed < 0.4, speed)Ĭs = map.contourf(x,y,speed,levels, cmap='jet')Ĭbar = plt.colorbar(cs, orientation='horizontal', cmap='jet', spacing='proportional',ticks=ticks)Ĭbar.set_label('850 mb Vector Wind Anomalies (m/s)') Result with set_bad (problem: no smooth transition to white):Ĭode so far: from netCDF4 import Dataset as NetCDFFile Result with continuous colormap (problem: no white): I tried masking those values and using set_bad but I ended up with a real blocky appearance, losing the nice smooth contours seen in the original image. I'm close but can't quite figure out how to modify a matplotlib colormap to make values <0.4 go to white. This was achieved using # Using imports from aboveĬdict = matplotlib.cm.get_cmap('spectral_r').I'm trying to produce a similar version of this image using Python: What I wanted to do was change the gray at the end of the spectral_r colormap to pure white. Then use this cmap in your plotting function. My_cmap = LinearSegmentedColormap('name', cdict) If you want to change the beginning and end colors, try import matplotlib.cm as cmįrom lors import LinearSegmentedColormapĬdict = (0, 0.5, 0.5) # x=0 for bottom color in colormapĬdict = (0, 0.5, 0.5) # y=0.5 grayĬdict = (0, 0.5, 0.5) # y1=y for simple interpolationĬdict = (1, 0.5, 0.5) # x=1 for top color in colormap ![]() Just make sure that for each color, the first and the last entry start with x=0 and x=1 respectively you must cover the whole spectrum of. This means that to change the color of the colormap, you have to examine how each of the three components of rgb are interpolated in the region of the colormap that you are interested in. What this means is that data with z (assuming we're doing a pcolor or imshow) between 0.0 and 0.5 will have the red component of the rgb color associated with that data will increase from 0.0 (no red) to 1.0 (maximum red). Let's take a look at two consecutive elements of cdict: ((0.0, 0.0, 0.0) cdict returns a list of values of the form (x, y0, y1). ![]() This dict has three keys, red, green, blue. However, it's pretty tricky figuring out exactly how to alter this dictionary. This returns a dictionary of all the colors that make up the colormap. If you really want to change the colormap, look at the documentation here and for LinearSegmentedColormap here.Ĭdict = cm.get_cmap('spectral_r')._segmentdata This may not solve your problem exactly, but it could be useful if you like a particular colormap and wish it had additional colors at both ends. This will force the entire colormap to be used for values between 0.01 and 0.99 and values above and below will be cyan and magenta respectively. For example, say you had data between 0 and 1 but didn't like the colors used at the extremes of the colormap for 0 and 1. Try using vmin, vmax keyword arguments in your plotting function. I was just recently struggling with this on my own. The plot on the right shows the same image using new_cmap. ![]() The plot on the left shows the image using the original colormap (in this example, jet). New_cmap = truncate_colormap(cmap, 0.2, 0.8)Īx.imshow(arr, interpolation='nearest', cmap=cmap)Īx.imshow(arr, interpolation='nearest', cmap=new_cmap) Below, I sample the original colormap at 100 points between 0.2 and 0.8: cmap(np.linspace(0.2, 0.8, 100))Īnd use these colors to generate a new colormap: import matplotlib.pyplot as pltĭef truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100): The staticmethod _list can be used to create new LinearSegmentedColormaps.
0 Comments
Leave a Reply. |