matplotlib: preventing a few very large (or small) values to affect my contour

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-02 11:17:51

问题


in plotting the data some times there are a few very large (or very small) numbers which, if not taken care of, will affect the contour in a bad way. a solution is to take out the 10% highest and lowest data out of the contour color grading and considering them as less than and more than. the following figure shows the idea:

the two arrow shapes on the top and the bottom of the bar support this idea. any value above 14 will be shown in white and any value below -2 will be shown in black color. how is it possible in matplotlib? How can I define: - to put the 5% of highest values and 5% of lowest values in two categories shown in the triangular parts in both ends of the bar? (Should I define it the contour operation or are there other ways?) - what if I want to give certain values instead of the percentage? for instance, ask to put any value above 14 on the white triangule and any value below -2 as black areas?

Thank you so much for your help.


回答1:


Taken from http://matplotlib.org/examples/api/colorbar_only.html. You can play with it and you will see if it could solve your problem.

import matplotlib.pyplot as plt
from matplotlib import mpl
import numpy as np

x = np.linspace(-1,1,100)
X,Y = np.meshgrid(x,x)
Z = np.exp(-X**2-Y**2)

vmin = 0.3 #Lower value
vmax = 0.9 #Upper value

bounds = np.linspace(vmin,vmax,4)

cmap = mpl.colors.ListedColormap([(0,0,0),(0.5,0.5,0.5),(0,1,0),(1,1,1)])
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

plt.imshow(Z,cmap=cmap,interpolation='nearest',vmin=vmin,vmax=vmax)

ax = plt.colorbar().ax
cb = mpl.colorbar.ColorbarBase(ax, norm=norm,
                               extend='both',
                               cmap=cmap)
cmap.set_over([0,0,1])
cmap.set_under([1,0,0])

plt.show()



来源:https://stackoverflow.com/questions/17494185/matplotlib-preventing-a-few-very-large-or-small-values-to-affect-my-contour

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!