如何从数据(table)绘制python等高线图?
How to draw a contour plot in python from data (table)?
我想使用 table 数据绘制等高线图。
我有 2 个变量和响应(3 列)。
我不明白如何使用它来构建这个情节。我尝试了下面的代码。但是我犯了下一个错误:输入 z 必须是二维的,而不是一维的。
feature_x = data.factor1
feature_y = data.factor2
# Creating 2-D grid of features
[X, Y] = np.meshgrid(feature_x, feature_y)
fig, ax = plt.subplots(1, 1)
Z = data.response
# plots filled contour plot
ax.contourf(X, Y, Z)
ax.set_title('Filled Contour Plot')
ax.set_xlabel('feature_x')
ax.set_ylabel('feature_y')
plt.show()
数据
要绘制等高线图,z
需要是包含点 (x,y)
所有值的二维矩阵。您可以将等高线图所需的数据视为索引为 x
、列为 y
且值为 z
的 DataFrame。所以 z
需要是形状为 (x.size, y.size)
.
的二维数组
因为你的 z
不是二维矩阵而是一维数组,你不能有等高线图。
例如,您可以做的是 relplot
和 hue
and/or size
import numpy as np
import pandas as pd
import seaborn as sns
x = np.array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])
y = np.array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
z = np.array([249, 523, 603, 775, 577, 763, 808, 695, 642, 525, 795, 758])
df = pd.DataFrame({'x':x, 'y':y, 'z':z})
sns.relplot(
data=df,
x='x', y='y',
size='z', sizes=(10, 100),
hue='z',
palette='coolwarm',
);
编辑
但是......如果你正在寻找一个连续的估计,你可以使用gaussian_kde
,例如
import scipy.stats as sps
import matplotlib.pyplot as plt
offset = .25
xmin = x.min()-offset
xmax = x.max()+offset
ymin = y.min()-offset
ymax = y.max()+offset
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([x, y])
kernel = sps.gaussian_kde(values, weights=z)
Z = np.reshape(kernel(positions).T, X.shape)
fig, ax = plt.subplots(figsize=(7, 7))
ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,
extent=[xmin, xmax, ymin, ymax],
aspect='auto'
)
sns.scatterplot(
data=df,
x='x', y='y',
size='z', sizes=(10, 200),
color='k'
)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
ax.legend(loc='upper left', bbox_to_anchor=(1,1))
plt.show()
我想使用 table 数据绘制等高线图。 我有 2 个变量和响应(3 列)。 我不明白如何使用它来构建这个情节。我尝试了下面的代码。但是我犯了下一个错误:输入 z 必须是二维的,而不是一维的。
feature_x = data.factor1
feature_y = data.factor2
# Creating 2-D grid of features
[X, Y] = np.meshgrid(feature_x, feature_y)
fig, ax = plt.subplots(1, 1)
Z = data.response
# plots filled contour plot
ax.contourf(X, Y, Z)
ax.set_title('Filled Contour Plot')
ax.set_xlabel('feature_x')
ax.set_ylabel('feature_y')
plt.show()
数据
要绘制等高线图,z
需要是包含点 (x,y)
所有值的二维矩阵。您可以将等高线图所需的数据视为索引为 x
、列为 y
且值为 z
的 DataFrame。所以 z
需要是形状为 (x.size, y.size)
.
因为你的 z
不是二维矩阵而是一维数组,你不能有等高线图。
例如,您可以做的是 relplot
和 hue
and/or size
import numpy as np
import pandas as pd
import seaborn as sns
x = np.array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3])
y = np.array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])
z = np.array([249, 523, 603, 775, 577, 763, 808, 695, 642, 525, 795, 758])
df = pd.DataFrame({'x':x, 'y':y, 'z':z})
sns.relplot(
data=df,
x='x', y='y',
size='z', sizes=(10, 100),
hue='z',
palette='coolwarm',
);
编辑
但是......如果你正在寻找一个连续的估计,你可以使用gaussian_kde
,例如
import scipy.stats as sps
import matplotlib.pyplot as plt
offset = .25
xmin = x.min()-offset
xmax = x.max()+offset
ymin = y.min()-offset
ymax = y.max()+offset
X, Y = np.mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = np.vstack([X.ravel(), Y.ravel()])
values = np.vstack([x, y])
kernel = sps.gaussian_kde(values, weights=z)
Z = np.reshape(kernel(positions).T, X.shape)
fig, ax = plt.subplots(figsize=(7, 7))
ax.imshow(np.rot90(Z), cmap=plt.cm.gist_earth_r,
extent=[xmin, xmax, ymin, ymax],
aspect='auto'
)
sns.scatterplot(
data=df,
x='x', y='y',
size='z', sizes=(10, 200),
color='k'
)
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
ax.legend(loc='upper left', bbox_to_anchor=(1,1))
plt.show()