2016-04-06 31 views
5

matplotlibのcontourf関数に問題があります。私は自分のデータをインポートしているtxtデータファイルを持っています。データの列(pm1とpm2)があり、2Dヒストグラムを実行しています。私は、このデータを3Dヒストグラムとしてプロットし、等高線プロットとして、最大値がどこにあるのかを見たいと思います。3Dヒストグラムと等高線プロットPython

これは私のコードです:

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棒グラフをプロットすることができますが、私は、輪郭1をプロットすることはできませんよ、私はcontourf機能でhistを配置する場合、私はエラーを取得:Length of x must be number of columns in zとあれば私は置くdz私は得るInput z must be a 2D array 私はまたxedgesとyexgesを使ってみましたが、これは問題を解決しません。

この問題は、関数histogram2Dの返り値の形に関係していると思います。しかし、私はそれを解決する方法を知らない。

最小値から最大値に変化するカラーコードで3Dバープロットを実行したいと思います。これを作るためにとにかくありますか?

は、私はあなたのデータがどのようなものかわかりませんが、あなたと同じ軸を共有し、あなたのcontourfプロットを持っている間違ったようですので、おそらく、私はあなたがやろうとしている正確に理解していないあなたに

答えて

1

ありがとうございましたbar3dプロット。 3D投影をしない軸を新しい図形に追加すると、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() 

thisような画像を返します。

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を取得:この(サイズを超えるが1/10と同じデータを使用して)について、どのように色分けされた3Dのバープロットについてのあなたの2番目の質問については

、、。

+0

カラーバーのカスタマイズラインへの参照:[pylab example](http://matplotlib.org/examples/pylab_examples/hist_colormapped.html) と[this post](http://stackoverflow.com/questions/11950375/apply) -color-map-to-mpl-toolkits-mplot3d-axes3d-bar3d) – lanery

関連する問題