2017-10-21 8 views
0

私の "現実世界"の問題で私はそのように拡大した後に私の図の目盛ラベルに書き込まれたxy値を再計算したい原点は常に(0,0)であり、明らかにx軸とy軸の値の相対距離は同じです。 私の問題は、このスレッドで解決した。 Initial solutionカラーバーを作成すると、以前に整列された2つの軸オブジェクトが相対的に移動します - Matplotlib

ソリューションをプロットし、ズーム後の異なるダニのlablesを取得つの可視軸を保持している1つの不可視の軸の作成が含まれます。

私の場合、私は複数のカウンターとcoutourfプロットをスーパーインポーズしたいと思います。これらのプロットの1つのみに対して、私は図にカラーバーを追加したい! しかし、スクリプトでカラーバーを作成すると、私が作成した2つの軸オブジェクトが互いに対して相対的に移動します。カラーバーがなければ完全に整列しています!ここ

はおおよそ挙動を再現MWEある:

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib import mlab, cm 

# Default delta is large because that makes it fast, and it illustrates 
# the correct registration between image and contours. 
delta = 0.5 

extent = (-3, 4, -4, 3) 

x = np.arange(-3.0, 4.001, delta) 
y = np.arange(-4.0, 3.001, delta) 
X, Y = np.meshgrid(x, y) 
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
Z = (Z1 - Z2) * 10 

levels = np.arange(-2.0, 1.601, 0.4) # Boost the upper limit to avoid truncation errors. 

norm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max()) 
cmap = cm.PRGn 




# ax is empty 
fig, ax = plt.subplots() 
ax.set_navigate(False) 
# ax2 will hold the plot, but has invisible labels 
ax2 = fig.add_subplot(111,zorder=2) 

ax2.contourf(X, Y, Z, levels, 
       cmap=cm.get_cmap(cmap, len(levels) - 1), 
       norm=norm, 
       ) 
ax2.axis("off") 

ax.set_xlim(ax2.get_xlim()) 
ax.set_ylim(ax2.get_ylim()) 

# 
# Declare and register callbacks 
def on_lims_change(axes): 
    # change limits of ax, when ax2 limits are changed. 
    a=ax2.get_xlim() 
    ax.set_xlim(0, a[1]-a[0]) 
    a=ax2.get_ylim() 
    ax.set_ylim(0, a[1]-a[0]) 



sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) #Do not show unnecessary parts of the colormap 
sm._A = [] 
cb = plt.colorbar(sm,extend="both", label="units") 
cb.ax.tick_params(labelsize=10) 


ax2.callbacks.connect('xlim_changed', on_lims_change) 
ax2.callbacks.connect('ylim_changed', on_lims_change) 
ax.axis('scaled') 
plt.axis('scaled') 
# Show 
plt.show() 

今ContourPlotは、可視軸realtiveずれているように見えます。私は

Link1 Link2 「カラーバーのボックスが自動的にそれが接続されている軸からスペースを食べる」ことを、示唆いくつかのヒントをオンラインで見つかりましたが、私は本当にこの動作を変更するために何をする必要があるか分かりません私の問題が関連しているかどうかも分かりません。私はそれがデータセット内にあるまったく同じ縦横比を維持する必要があるとして、

ax.axis('scaled') 
plt.axis('scaled') 

必要です:

部分があることに注意してください!

ありがとうございます!

答えて

1

あなたが "でカラーバーを作成し、また

ax.set_position(ax2.get_position()) 

を経由してカラーバーを追加した後ax2(データを示す軸)の位置にaxの位置(ラベルと空の軸)を変更することができます両方の軸からスペース、

cb = fig.colorbar(sm,ax=[ax,ax2], extend="both", label="units") 

はどちらのソリューションがthis linked questionへの回答で発見された「steeling。 contourfプロットは、Axesの棘を重ならないよう、ax2場合は上にaxを入れ、さらに

ax.axis('scaled') 
ax2.axis('scaled') 


次の質問の実際の範囲外のいくつかの追加の改善です。

# put `ax` on top, to let the contours not overlap the shown axes 
ax.set_zorder(2)  
ax.patch.set_visible(False) 
# ax2 will hold the plot, but has invisible labels 
ax2 = fig.add_subplot(111,zorder=1) 

完全なコード:

import matplotlib.pyplot as plt 
import numpy as np 
from matplotlib import mlab, cm 

delta = 0.5 
extent = (-3, 4, -4, 3) 
x = np.arange(-3.0, 4.001, delta) 
y = np.arange(-4.0, 3.001, delta) 
X, Y = np.meshgrid(x, y) 
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
Z = (Z1 - Z2) * 10 

levels = np.arange(-2.0, 1.601, 0.4) 

norm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max()) 
cmap = cm.PRGn 

# ax is empty 
fig, ax = plt.subplots() 
ax.set_navigate(False) 
# put `ax` on top, to let the contours not overlap the shown axes 
ax.set_zorder(2)  
ax.patch.set_visible(False) 
# ax2 will hold the plot, but has invisible labels 
ax2 = fig.add_subplot(111,zorder=1) 

ax2.contourf(X, Y, Z, levels, 
       cmap=cm.get_cmap(cmap, len(levels) - 1), 
       norm=norm, 
       ) 
ax2.axis("off") 

ax.set_xlim(ax2.get_xlim()) 
ax.set_ylim(ax2.get_ylim()) 

# 
# Declare and register callbacks 
def on_lims_change(axes): 
    # change limits of ax, when ax2 limits are changed. 
    a=ax2.get_xlim() 
    ax.set_xlim(0, a[1]-a[0]) 
    a=ax2.get_ylim() 
    ax.set_ylim(0, a[1]-a[0]) 


sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm) 
sm._A = [] 
cb = fig.colorbar(sm,ax=[ax,ax2], extend="both", label="units") 
cb.ax.tick_params(labelsize=10) 

ax2.callbacks.connect('xlim_changed', on_lims_change) 
ax2.callbacks.connect('ylim_changed', on_lims_change) 
ax.axis('scaled') 
ax2.axis('scaled') 
#ax.set_position(ax2.get_position()) 
# Show 
plt.show() 

enter image description here

+0

私は理解の私の欠如を浮き彫りに成功せず、スレッド内でソリューションを実装しようとしました。私が直面する問題は、 'circle = Circle((-3,2)、0.1、color = 'r')ax2.add_patch(circle)'のように 'matplotlib.patches 'を追加したい場合です。パッチがプロットの端に追加されると、表示されているX-Y範囲が変化し、丸が表示され、座標がシフトします。これは 'ax.axis( 'scaled')の呼び出しに関連していますか? ax2.axis( 'scaled')' ....私はそれを検索するときに何かを見つけることができないので、ヒントを見ていただきありがとうございます。あなたのソリューションに感謝します!本当に感謝しています! – NorrinRadd

+0

'ax.set_xlim(ax2.get_xlim())'を呼び出す前に円を追加する必要があります。 – ImportanceOfBeingErnest

+0

私はこのコミュニティがおかげさまで誇張された表情で少し制限されていることを知っています...しかし、私は正直なところあなたにビールや何かを買いたいと思っています...これは私にとって非常に直面する問題を解決しました!ありがとうございました! – NorrinRadd

関連する問題