2017-02-05 10 views
1

親愛なる仲間のPythonユーザー、望ましくないカラーバーと軸ラベル/プロットタイトルの相互作用

私はmatplotlibのライブラリを使用して、関数imshowとの組み合わせでLinearSegmentedColormapを組み合わせることにより、カラーマップを作成しよう、と私は、実際のカラーバーで苦労しています。

カラーバーは、デフォルトでは動作しません。つまり、グラフには大きすぎます。デフォルトでは、私はこれを取得:だから

default colorbar

を、私はthis postを参照して、カラーバーの高さを修正するために、次のコード行を使用:

ax = plt.gca() 
divider = make_axes_locatable(ax) 
cax = divider.append_axes("right", size="5%", pad=0.05) 
plt.colorbar(img2, cax=cax) 

Anが、私はこの非常に奇妙な結果を得ました:私が追加されたコードの行は、私の斧と対話する理由を把握することはできませんよ

fixed graph

とタイトルをプロット...彼らはなぜカラーバーに行くのですか?

import matplotlib.pyplot as plt 
import matplotlib.pylab as pylab 
import matplotlib.colors as colors 
import numpy as np 
from mpl_toolkits.axes_grid1 import make_axes_locatable  

#random data for example 
clmap = np.random.rand(30,500) 

#plot 2D color map 
fig = plt.figure() 
cmap2 = colors.LinearSegmentedColormap.from_list('my_colormap', ['blue','green','red'], 256) 
img2 = plt.imshow(clmap,interpolation='nearest',cmap = cmap2,origin='lower') 
ax = plt.gca() 
divider = make_axes_locatable(ax) 
cax = divider.append_axes("right", size="5%", pad=0.05) 
fig.colorbar(img2, cmap = cmap2, cax=cax) 
plt.title('color map of atom probab at iteration 1') 
plt.xlabel('atom id') 
plt.ylabel('layer') 
fig.savefig("map_p_1.png") 
plt.gcf().clear() 
+0

うち最も簡単な方法は、新しい軸を作成する前にpyplotコマンドを適用することです追加されたコード行は私の軸とプロットのタイトルと相互作用します。 – ImportanceOfBeingErnest

答えて

0

あなたがオブジェクト指向APIでpyplotステートマシン(plt)を混合している:

はここで完全なコードです。
2番目の軸オブジェクト(cax)を作成すると、現在の軸になります。その後、すべてのpyplotコマンドがこの軸に適用されます。 、我々はまた、なぜ把握する」ことができない[MCVE]の形で私たちにactualyコードを表示せずに

import matplotlib.pyplot as plt 
import matplotlib.colors as colors 
import numpy as np 
from mpl_toolkits.axes_grid1 import make_axes_locatable  

#random data for example 
clmap = np.random.rand(30,500) 

#plot 2D color map 
fig = plt.figure() 
plt.title('color map of atom probab at iteration 1') 
plt.xlabel('atom id') 
plt.ylabel('layer') 
cmap2 = colors.LinearSegmentedColormap.from_list('my_colormap', ['blue','green','red'], 256) 
img2 = plt.imshow(clmap,interpolation='nearest',cmap = cmap2,origin='lower') 
ax = plt.gca() 
divider = make_axes_locatable(ax) 
cax = divider.append_axes("right", size="5%", pad=0.05) 
fig.colorbar(img2, cmap = cmap2, cax=cax) 

fig.savefig("map_p_1.png") 

plt.show() 
+0

実際、これは機能します!ありがとう! – Glxblt76

関連する問題