2017-03-17 12 views
2

これは非常に小さな問題ですが、それでもわかりません。 私はカラーマップをプロットしmatplotlibのでimshowを使用しています - しかし、結果は数字やタイトルが一緒に整列されていないということです。imshow colormap figureとsuptitleが中央に揃っていない

enter image description here

私はプロットのために使用しているコードは次のとおりです。

fig, ax = plt.subplots(figsize=(27, 10)) 

cax1 = ax.imshow(reversed_df, origin='lower', cmap='viridis', interpolation = 'nearest', aspect=0.55) 

ylabels = ['0:00', '03:00', '06:00', '09:00', '12:00', '15:00', '18:00', '21:00'] 
major_ticks = np.arange(0, 24, 3) 
ax.set_yticks(major_ticks) 
ax.set_yticklabels(ylabels, fontsize = 15) 

xlabels = ['Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan17'] 
xmajor_ticks = np.arange(0,12,1) 
ax.set_xticks(xmajor_ticks) 
ax.set_xticklabels(xlabels, fontsize = 15) 
fig.autofmt_xdate() 

fmt = '%1.2f' 
cb = plt.colorbar(cax1,fraction=0.046, pad=0.04, format=fmt) 
cb.update_ticks 

fig.suptitle('2016 Monthly Pressure Data (no Normalization) $[mbar]$',fontsize=20, horizontalalignment='center') 

fig.savefig('Pressure 2016 Full.jpeg', dpi=300) 
plt.show() 

ありがとうございました!

答えて

2

この問題は軸のアスペクトが"equal"imshowのプロットの場合)に設定され、カラーバーが追加された場合に発生します。

import matplotlib.pyplot as plt 
import numpy as np 

x = np.random.rand(5,5) 
fig, ax = plt.subplots(figsize=(12, 3)) 
fig.patch.set_facecolor('#dbf4d9') 

im = ax.imshow(x) 

fig.colorbar(im) 
plt.show() 

enter image description here

回避策は、サブプロットはleftと画像が中心になるようにrightパラメータを設定することであろう。これは、いくつかの試行錯誤が必要ですが、以下のように働くことがあります。

import matplotlib.pyplot as plt 
import numpy as np 

x = np.random.rand(5,5) 
fig, ax = plt.subplots(figsize=(12, 3)) 
plt.subplots_adjust(left=0.2, right=0.68) 
fig.patch.set_facecolor('#dbf4d9') 

im = ax.imshow(x) 

fig.colorbar(im) 
plt.show() 

enter image description here

作品の魅力のよう
+0

!私はplt.subplots_adjustを使用しました(左= 0.1、右= 0.64) – ValientProcess

関連する問題