我有X,Y,Z格式的数据,其中所有都是一维数组,Z是坐标(X,Y)处的测量幅度。我希望将这些数据显示为轮廓或“显示”。绘制轮廓/颜色代表值Z(振幅)的图。
测量网格和X和Y外观不规则。
非常感谢,
LEN(X)= 100
LEN(Y)= 100
LEN(Z)= 100
答案 0 :(得分:40)
plt.tricontourf(x,y,z)
是否满足您的要求?
它将绘制不规则间隔数据(非直线网格)的填充轮廓。
您可能还想查看plt.tripcolor()
。
import numpy as np
import matplotlib.pyplot as plt
x = np.random.rand(100)
y = np.random.rand(100)
z = np.sin(x)+np.cos(y)
f, ax = plt.subplots(1,2, sharex=True, sharey=True)
ax[0].tripcolor(x,y,z)
ax[1].tricontourf(x,y,z, 20) # choose 20 contour levels, just to show how good its interpolation is
ax[1].plot(x,y, 'ko ')
ax[0].plot(x,y, 'ko ')
plt.savefig('test.png')
答案 1 :(得分:2)
(源代码@结束......)
这是我用它制作的一点点眼睛糖果。它探讨了一个事实,即网格网格的线性变换仍然是网格网格。即在我所有情节的左侧,我正在使用X和Y坐标进行2-d(输入)功能。在右边,我想使用相同功能的(AVG(X,Y),Y-X)坐标。
我在本机坐标中制作网格物体并将其转换为其他坐标的网格物体。如果变换是线性的,则可以正常工作。
对于底部两张图,我使用随机抽样直接解决您的问题。
以下是setlims=False
的图片:
与setlims=True
相同:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
def f(x, y):
return y**2 - x**2
lim = 2
xlims = [-lim , lim]
ylims = [-lim, lim]
setlims = False
pde = 1
numpts = 50
numconts = 20
xs_even = np.linspace(*xlims, num=numpts)
ys_even = np.linspace(*ylims, num=numpts)
xs_rand = np.random.uniform(*xlims, size=numpts**2)
ys_rand = np.random.uniform(*ylims, size=numpts**2)
XS_even, YS_even = np.meshgrid(xs_even, ys_even)
levels = np.linspace(np.min(f(XS_even, YS_even)), np.max(f(XS_even, YS_even)), num=numconts)
cmap = sns.blend_palette([sns.xkcd_rgb['cerulean'], sns.xkcd_rgb['purple']], as_cmap=True)
fig, axes = plt.subplots(3, 2, figsize=(10, 15))
ax = axes[0, 0]
H = XS_even
V = YS_even
Z = f(XS_even, YS_even)
ax.contour(H, V, Z, levels, cmap=cmap)
ax.plot(H.flatten()[::pde], V.flatten()[::pde], linestyle='None', marker='.', color='.75', alpha=0.5, zorder=1, markersize=4)
if setlims:
ax.set_xlim([-lim/2., lim/2.])
ax.set_ylim([-lim/2., lim/2.])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Points on grid, contour')
ax = axes[1, 0]
H = H.flatten()
V = V.flatten()
Z = Z.flatten()
ax.tricontour(H, V, Z, levels, cmap=cmap)
ax.plot(H.flatten()[::pde], V.flatten()[::pde], linestyle='None', marker='.', color='.75', alpha=0.5, zorder=1, markersize=4)
if setlims:
ax.set_xlim([-lim/2., lim/2.])
ax.set_ylim([-lim/2., lim/2.])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Points on grid, tricontour')
ax = axes[0, 1]
H = (XS_even + YS_even) / 2.
V = YS_even - XS_even
Z = f(XS_even, YS_even)
ax.contour(H, V, Z, levels, cmap=cmap)
ax.plot(H.flatten()[::pde], V.flatten()[::pde], linestyle='None', marker='.', color='.75', alpha=0.5, zorder=1, markersize=4)
if setlims:
ax.set_xlim([-lim/2., lim/2.])
ax.set_ylim([-lim, lim])
ax.set_xlabel('AVG')
ax.set_ylabel('DIFF')
ax.set_title('Points on transformed grid, contour')
ax = axes[1, 1]
H = H.flatten()
V = V.flatten()
Z = Z.flatten()
ax.tricontour(H, V, Z, levels, cmap=cmap)
ax.plot(H.flatten()[::pde], V.flatten()[::pde], linestyle='None', marker='.', color='.75', alpha=0.5, zorder=1, markersize=4)
if setlims:
ax.set_xlim([-lim/2., lim/2.])
ax.set_ylim([-lim, lim])
ax.set_xlabel('AVG')
ax.set_ylabel('DIFF')
ax.set_title('Points on transformed grid, tricontour')
ax=axes[2, 0]
H = xs_rand
V = ys_rand
Z = f(xs_rand, ys_rand)
ax.tricontour(H, V, Z, levels, cmap=cmap)
ax.plot(H[::pde], V[::pde], linestyle='None', marker='.', color='.75', alpha=0.5, zorder=1, markersize=4)
if setlims:
ax.set_xlim([-lim/2., lim/2.])
ax.set_ylim([-lim/2., lim/2.])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Points random, tricontour')
ax=axes[2, 1]
H = (xs_rand + ys_rand) / 2.
V = ys_rand - xs_rand
Z = f(xs_rand, ys_rand)
ax.tricontour(H, V, Z, levels, cmap=cmap)
ax.plot(H[::pde], V[::pde], linestyle='None', marker='.', color='.75', alpha=0.5, zorder=1, markersize=4)
if setlims:
ax.set_xlim([-lim/2., lim/2.])
ax.set_ylim([-lim, lim])
ax.set_xlabel('AVG')
ax.set_ylabel('DIFF')
ax.set_title('Points random transformed, tricontour')
fig.tight_layout()
答案 2 :(得分:0)
xx, yy = np.meshgrid(x, y)
plt.contour(xx, yy, z)
如果它们是不规则的间距并不重要,轮廓和3d图需要网格栅。
答案 3 :(得分:0)
如果您准备将Python与其竞争对手R分开,我刚刚向CRAN提交了一个软件包(明天或第二天应该可用),它可以在非常规网格上进行轮廓加工 - 以下内容通过几行代码实现:
library(contoureR)
set.seed(1)
x = runif(100)
y = runif(100)
z = sin(x) + cos(y)
df = getContourLines(x,y,z,binwidth=0.0005)
ggplot(data=df,aes(x,y,group=Group)) +
geom_polygon(aes(fill=z)) +
scale_fill_gradient(low="blue",high="red") +
theme_bw()
产生以下内容:
如果你想要一个更规则的网格,并且可以承受一些额外的计算时间:
x = seq(0,1,by=0.005)
y = seq(0,1,by=0.005)
d = expand.grid(x=x,y=y)
d$z = with(d,sin(x) + cos(y))
df = getContourLines(d,binwidth=0.0005)
ggplot(data=df,aes(x,y,group=Group)) +
geom_polygon(aes(fill=z)) +
scale_fill_gradient(low="blue",high="red") +
theme_bw()
上面的模糊边缘,我知道如何解决,应该修复下一版本的软件....
答案 4 :(得分:0)
散点图可能适用于您的情况:
import numpy as np
import matplotlib.pyplot as plt
# Generate random data, x,y for coordinates, z for values(amplitude)
x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)
# Scatter plot
plt.scatter(x=x,y=y,c=z)
使用选项 c
可视化您的振幅。