2017-03-04 8 views
3

私はプロットしたいと思っているデータをいくつか持っています。問題は、(x,y)が「いい」矩形の一部ではなく、付属の画像に示されているような任意の平行四辺形であることです(この特定の矩形も長方形ですが、より一般的なケースを考えることができます)。だから私はこの場合、plot_surfaceをどのように使うことができるのか分かりません。なぜなら、これは通常xとyを2次元配列として取るので、ここではxとyの値は1dです。ありがとう。 x,y-DataPython 3Dプロットは非長方形のドメイン上にある

+0

パララルグラムのスペックは事前に知っていますか? –

+0

実際には、私はデータを取得し、私はプロットする必要があります - もちろん、私は与えられたデータから仕様を計算することができたと思います... – Faser

+0

データの順序を知っていますか? – Eric

答えて

5

異なる点は、1Dアレイとしてmatplotlib.Axes3D.plot_trisurfに供給することができます。彼らが特定の構造に従うかどうかは重要ではありません。データの構造に依存するであろう

他の方法は

  • が通常長方形のグリッド上の点を補間することであろう。これはgriddataを使用して達成できます。例を参照here
  • 入力配列が規則正しく存在するように整形し、plot_surface()を使用します。点の順番によっては、平行四辺形のグリッドに対して非常に簡単な解決策となります。
    sphere exampleから見ることができるように、plot_surface()は、規則的な方法で構造化されている限り、非常に不等なグリッド形状のケースでも機能します。ここで

いくつかの例は次のとおりです:完全性については

enter image description here

、ここでは上記の画像を生成したコードを見つける:あなたのデータが順番にあり、あなたが知っている場合

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
import numpy as np 

f = lambda x,y: np.sin(x+0.4*y)*0.23+1 

fig = plt.figure(figsize=(5,6)) 
plt.subplots_adjust(left=0.1, top=0.95,wspace=0.01) 


ax0 = fig.add_subplot(322, projection="3d") 

ma = 6*(np.random.rand(100)-0.5) 
mb = 6*(np.random.rand(100)-0.5) 
phi = np.pi/4 
x = 1.7*ma*np.cos(phi) + 1.7*mb*np.sin(phi) 
y = -1.2*ma*np.sin(phi) +1.2* mb*np.cos(phi) 
z = f(x,y) 
ax0.plot_trisurf(x,y,z) 

ax1 = fig.add_subplot(321) 
ax0.set_title("random plot_trisurf()") 
ax1.set_aspect("equal") 
ax1.scatter(x,y, marker="+", alpha=0.4) 
for i in range(len(x)): 
    ax1.text(x[i],y[i], i , ha="center", va="center", fontsize=6) 


n = 10 
a = np.linspace(-3, 3, n) 
ma, mb = np.meshgrid(a,a) 
phi = np.pi/4 
xm = 1.7*ma*np.cos(phi) + 1.7*mb*np.sin(phi) 
ym = -1.2*ma*np.sin(phi) +1.2* mb*np.cos(phi) 
shuf = np.c_[xm.flatten(), ym.flatten()] 
np.random.shuffle(shuf) 
x = shuf[:,0] 
y = shuf[:,1] 
z = f(x,y) 

ax2 = fig.add_subplot(324, projection="3d") 
ax2.plot_trisurf(x,y,z) 

ax3 = fig.add_subplot(323) 
ax2.set_title("unstructured plot_trisurf()") 
ax3.set_aspect("equal") 
ax3.scatter(x,y, marker="+", alpha=0.4) 
for i in range(len(x)): 
    ax3.text(x[i],y[i], i , ha="center", va="center", fontsize=6) 


x = xm.flatten() 
y = ym.flatten() 
z = f(x,y) 

X = x.reshape(10,10) 
Y = y.reshape(10,10) 
Z = z.reshape(10,10) 

ax4 = fig.add_subplot(326, projection="3d") 
ax4.plot_surface(X,Y,Z) 

ax5 = fig.add_subplot(325) 
ax4.set_title("regular plot_surf()") 
ax5.set_aspect("equal") 
ax5.scatter(x,y, marker="+", alpha=0.4) 
for i in range(len(x)): 
    ax5.text(x[i],y[i], i , ha="center", va="center", fontsize=6) 


for axes in [ax0, ax2,ax4]: 
    axes.set_xlim([-3.5,3.5]) 
    axes.set_ylim([-3.5,3.5]) 
    axes.set_zlim([0.9,2.0]) 
    axes.axis("off") 
plt.savefig(__file__+".png") 
plt.show() 
+1

'surface'は長方形のグリッドを必要とせず、必要なのは四角形のグリッドだけです。したがって、正しく並べ替えれば、OPデータは数値的な変更なしにそのまま「表面」に渡すことができます – Eric

2

をパラログラムのサイズは、おそらくリシェイプで十分です:

ax.surface(x.reshape(10, 10), y.reshape(10, 10), z.reshape(10, 10)) 

平行四辺形の各辺が10ポイントであり、ポイントがジグザグのパターンで並べられている場合に動作します

+0

ありがとうエリック、あなたのやり方も同様でした。後でもっと奇妙な形のグリッドをサポートすることを検討しているので、今はトリスフルを使用します。 – Faser

+0

@Faser:これらのグリッドを生成していますか?また、「グリッド」とは「メッシュ」ではないということは、「サーフェス」が常に十分であることを意味します – Eric

関連する問題