5

matplotlibのcontourf関数に問題があります。データをインポートする txt データ ファイルがあります。データの列 (pm1 と pm2) があり、2D ヒストグラムを実行しています。このデータを 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配置すると、xedgesとdzyexgesInput z must be a 2D array も使用しようとしましたが、これは解決しません.問題。

問題は関数 histogram2D の戻り値の形状に関係していると思います。しかし、私はそれを解決する方法がわかりません。

また、カラーコードが最小値から最大値に変化する 3D 棒グラフを実行したいと思います。とにかくこれを作ることはありますか?

ありがとうございました

4

1 に答える 1

1

データがどのように見えるかわからないので、おそらくあなたが何をしようとしているのか正確にはわかりませんが、プロットがcontourfプロットと同じ軸を共有するのは間違っているようbar3dです。contourf3D プロジェクションのない軸を新しい Figure に追加すると、を使用して問題なくプロットを作成できるはずですhist。ランダムな正規分布からのデータを使用した例:

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()

このような画像を返します。

2番目の質問については、色分けされた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()

私はこのイメージを取得します。

于 2016-04-07T07:12:03.660 に答える