3D 直方图和等高线图 Python
3D histograms and Contour plots Python
我对 matplotlib 的 contourf 函数有疑问。我有一个 txt 数据文件,我正在从中导入数据。我有数据列(pm1 和 pm2)并且正在执行二维直方图。我想将此数据绘制为 3D 直方图和等高线图,以查看最大值所在的位置。
这是我的代码:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
rows = np.arange(200,1300,10)
hist, xedges, yedges = np.histogram2d (pm1_n, pm2_n, bins = (rows, rows) )
elements = (len(xedges) - 1) * (len(yedges) - 1)
xpos, ypos = np.meshgrid(xedges[:-1], yedges[:-1])
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(elements)
dx = 0.1 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()
#####The problem is here#####
#ax.contourf(xpos,ypos,hist)
#ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average')
plt.show()
我可以绘制 3d 条形图,但无法绘制等高线图,如果我将 hist
放在 contourf 函数中,我会收到错误消息:Length of x must be number of columns in z
如果我将 dz
我得到 Input z must be a 2D array
我也尝试过使用 xedges 和 yexges 但这并不能解决问题。
我认为问题与函数 histogram2D 的 return 的形状有关。但是不知道怎么解决
我还想绘制 3D 条形图,颜色代码从最小值变为最大值。反正有办法做这个吗?
谢谢
也许我不明白你究竟想做什么,因为我不知道你的数据是什么样的,但让你的 contourf
绘图与你的 [ 共享同一个轴似乎是错误的=13=]情节。如果您将没有 3D 投影的轴添加到新图形,您应该能够使用 hist
制作 contourf
绘图。使用随机正态分布数据的示例:
import numpy as np
import matplotlib.pyplot as plt
n_points = 1000
x = np.random.normal(0, 2, n_points)
y = np.random.normal(0, 2, n_points)
hist, xedges, yedges = np.histogram2d(x, y, bins=np.sqrt(n_points))
fig2D = plt.figure()
ax2D = fig2D.add_subplot(111)
ax2D.contourf(hist, interpolation='nearest',
extent=(xedges[0], xedges[-1], yedges[0], yedges[-1]))
plt.show()
returns 像 this.
这样的图像
关于你的第二个问题,关于颜色编码的 3D 条形图,这个怎么样(使用与上面相同的数据但大小的 1/10):
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.colors as colors
n_points = 100
x = np.random.normal(0, 2, n_points)
y = np.random.normal(0, 2, n_points)
hist, xedges, yedges = np.histogram2d(x, y, bins=np.sqrt(n_points))
# Following your data reduction process
xpos, ypos = np.meshgrid(xedges[:-1], yedges[:-1])
length, width = 0.4, 0.4
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(n_points)
dx = np.ones(n_points) * length
dy = np.ones(n_points) * width
dz = hist.flatten()
# This is where the colorbar customization comes in
dz_normed = dz / dz.max()
normed_cbar = colors.Normalize(dz_normed.min(), dz_normed.max())
# Using jet, but should work with any colorbar
color = cm.jet(normed_cbar(dz_normed))
fig3D = plt.figure()
ax3D = fig3D.add_subplot(111, projection='3d')
ax3D.bar3d(xpos, ypos, zpos, dx, dy, dz, color=color)
plt.show()
我得到 this image。
我对 matplotlib 的 contourf 函数有疑问。我有一个 txt 数据文件,我正在从中导入数据。我有数据列(pm1 和 pm2)并且正在执行二维直方图。我想将此数据绘制为 3D 直方图和等高线图,以查看最大值所在的位置。
这是我的代码:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
rows = np.arange(200,1300,10)
hist, xedges, yedges = np.histogram2d (pm1_n, pm2_n, bins = (rows, rows) )
elements = (len(xedges) - 1) * (len(yedges) - 1)
xpos, ypos = np.meshgrid(xedges[:-1], yedges[:-1])
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(elements)
dx = 0.1 * np.ones_like(zpos)
dy = dx.copy()
dz = hist.flatten()
#####The problem is here#####
#ax.contourf(xpos,ypos,hist)
#ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average')
plt.show()
我可以绘制 3d 条形图,但无法绘制等高线图,如果我将 hist
放在 contourf 函数中,我会收到错误消息:Length of x must be number of columns in z
如果我将 dz
我得到 Input z must be a 2D array
我也尝试过使用 xedges 和 yexges 但这并不能解决问题。
我认为问题与函数 histogram2D 的 return 的形状有关。但是不知道怎么解决
我还想绘制 3D 条形图,颜色代码从最小值变为最大值。反正有办法做这个吗?
谢谢
也许我不明白你究竟想做什么,因为我不知道你的数据是什么样的,但让你的 contourf
绘图与你的 [ 共享同一个轴似乎是错误的=13=]情节。如果您将没有 3D 投影的轴添加到新图形,您应该能够使用 hist
制作 contourf
绘图。使用随机正态分布数据的示例:
import numpy as np
import matplotlib.pyplot as plt
n_points = 1000
x = np.random.normal(0, 2, n_points)
y = np.random.normal(0, 2, n_points)
hist, xedges, yedges = np.histogram2d(x, y, bins=np.sqrt(n_points))
fig2D = plt.figure()
ax2D = fig2D.add_subplot(111)
ax2D.contourf(hist, interpolation='nearest',
extent=(xedges[0], xedges[-1], yedges[0], yedges[-1]))
plt.show()
returns 像 this.
这样的图像关于你的第二个问题,关于颜色编码的 3D 条形图,这个怎么样(使用与上面相同的数据但大小的 1/10):
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.colors as colors
n_points = 100
x = np.random.normal(0, 2, n_points)
y = np.random.normal(0, 2, n_points)
hist, xedges, yedges = np.histogram2d(x, y, bins=np.sqrt(n_points))
# Following your data reduction process
xpos, ypos = np.meshgrid(xedges[:-1], yedges[:-1])
length, width = 0.4, 0.4
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros(n_points)
dx = np.ones(n_points) * length
dy = np.ones(n_points) * width
dz = hist.flatten()
# This is where the colorbar customization comes in
dz_normed = dz / dz.max()
normed_cbar = colors.Normalize(dz_normed.min(), dz_normed.max())
# Using jet, but should work with any colorbar
color = cm.jet(normed_cbar(dz_normed))
fig3D = plt.figure()
ax3D = fig3D.add_subplot(111, projection='3d')
ax3D.bar3d(xpos, ypos, zpos, dx, dy, dz, color=color)
plt.show()
我得到 this image。