5
有沒有辦法根據contour
函數使用的顏色表着色點? 我意識到我可以指定一個顏色映射表,但大概contour
函數會執行一些 縮放和/或數據規範化?根據輪廓顏色的顏色點
下面是一個例子:......根據該值
import numpy as np
import scipy.stats as ss
def plot_2d_probsurface(data, resolution=20, ax = None, xlim=None, ylim=None):
# create a function to calcualte the density at a particular location
kde = ss.gaussian_kde(data.T)
# calculate the limits if there are no values passed in
# passed in values are useful if calling this function
# systematically with different sets of data whose limits
# aren't consistent
if xlim is None:
xlim = (min(data[:,0]), max(data[:,0]))
if ylim is None:
ylim = (min(data[:,1]), max(data[:,1]))
# create some tick marks that will be used to create a grid
xs = np.linspace(xlim[0], xlim[1], resolution)
ys = np.linspace(ylim[0], ylim[1], resolution)
# wrap the KDE function and vectorize it so that we can call it on
# the entire grid at once
def calc_prob(x,y):
return kde([x,y])[0]
calc_prob = vectorize(calc_prob)
# check if we've received a plotting surface
if ax is None:
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(1,1,1)
# create the grid and calculate the density at each point
X,Y = np.meshgrid(xs, ys)
Z = calc_prob(X,Y)
# the values according to which the points should be colored
point_values = kde(data.T)
# plot the contour
cont = ax.contour(X,Y,Z)
#print cont
ax.plot(data[:,0], data[:,1], 'o')
return (None, None)
data_x = np.random.random((50,2))
cont = plot_2d_probsurface(data_x)
所以在下面的情節,最高密度的點會變成棕色,未來橙,未來黃等點數應該是有色的已經在point_values
。這隻需要轉換爲顏色並傳遞給plot
函數。但是,如何在contour
情節中對它們進行縮放?
獎勵回答你自己的問題!僅供參考:顏色不完全匹配。輪廓的顏色被縮放爲最小和最大輪廓,而散射顏色被縮放爲數據的最小值和最大值。快速解決這個問題的方法是做類似'cont = ax.contour(...)'和'ax.scatter(x,y,c = z,cmap = cont.cmap,norm = cont.norm) '。這將給出連續的,而不是離散的(如'contourf'將使用)色彩映射,但縮放比例將是相同的。 (如果你真的需要一個離散的顏色映射表,使用'plt.get_cmap(「name」,N)'。) – 2014-11-02 21:14:22
謝謝!這正是我所問的。如果你想將其作爲一個答案來形成,我會接受它,因爲這是對我提出的問題更準確,更準確的回答:) – 2014-11-03 11:47:36