Ich muss 2-dimensionale Verteilungen von 2 Gruppen vergleichen.Overlay Contour Plots in Matplotlib
Wenn ich matplotlib.pyplot.contourf
verwende und die Diagramme überlagere, füllt die Hintergrundfarbe jedes Konturdiagramms den gesamten Zeichenbereich aus. Gibt es eine Möglichkeit, die unterste Kontur für jedes Konturdiagramm transparent zu machen, so dass das Zentrum jeder Kontur leichter zu sehen ist?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import scipy.stats as st
def make_cloud(x, y, std, n=100):
x = np.random.normal(x, std, n)
y = np.random.normal(y, std, n)
return np.array(zip(x, y))
def contour_cloud(x, y, cmap):
xmin, xmax = -4, 4
ymin, ymax = -4, 4
xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([xx.ravel(), yy.ravel()])
values = np.vstack([x, y])
kernel = st.gaussian_kde(values)
f = np.reshape(kernel(positions).T, xx.shape)
plt.contourf(xx, yy, f, cmap=cmap, alpha=0.5)
cloud1 = make_cloud(-1, 1, 1)
cloud2 = make_cloud(1, -1, 1)
plt.scatter(x=cloud1[:,0], y=cloud1[:,1])
plt.scatter(x=cloud2[:,0], y=cloud2[:,1], color='red')
fig = plt.gcf()
ax = plt.gca()
contour_cloud(x=cloud1[:, 0], y=cloud1[:, 1], cmap=cm.Blues)
contour_cloud(x=cloud2[:, 0], y=cloud2[:, 1], cmap=cm.Reds)
Schön, danke! – Chris