2017-06-14 12 views
0

fill_betweenを使用すると、色付きのパッチは縦にわずかに傾いているので、y軸の上に空白があり、色は下のほうにうまくマージされますyaxis。誰でもこれを防ぐ方法を知っている/これを引き起こしていることを理解していますか? enter image description herePython Matplotlib - y軸の上の行間の空白を削除する

プロットは「天気予報ウィンドウ」を示しています。天気予報パラメータが特定のしきい値を下回っている場合、期間は「運用中」で、それ以外は「非運用」です。このプロットを生成するためのコードは次のとおりです。

figure = plt.figure(figsize=(8, 3 * 3)) 
gs = gridspec.GridSpec(3, 1) 
gs.update(hspace=0.3) 
ax0 = plt.subplot(gs[0]) 
df1.plot() # pandas DataSeries 
ax0.set_xlabel('') 
ax1 = plt.subplot(gs[1]) 
df2.plot() # pandas DataSeries 
ax1.set_xlabel('') 
ax2 = plt.subplot(gs[2]) 
trans = mtransforms.blended_transform_factory(ax2.transData, ax2.transAxes) 
ax2.plot(xtime, y, color = 'green', alpha = 0.5, lw = 0.01) 
ax2.set_xlim(xtime[0], xtime[-1]) 
ax2.fill_between(xtime2, 0, 1, where = yop > 0, facecolor = 'green', alpha = 0.5, interpolate = True, transform = trans) 
# yop is numpy array of 0's and 1's 
ax2.fill_between(xtime2, 0, 1, where = ynonop > 0, facecolor = 'red', alpha = 0.5, interpolate = True, transform = trans) 
# ynonop has 0's and 1's opposite to yop 

interpolate = Trueは、いくつかの役割がポイント間の空白を削除して果たしています。ここで

は、問題をテストするシンプルなコードです:白のストライプを引き起こしているかを理解するには

import matplotlib.pyplot as plt 
import numpy as np 
fig, ax = plt.subplots() 
x = np.arange(0.0, 365, 1) 
yop = np.random.randint(2, size=len(x)) 
ynonop = np.copy(yop) 
# make 0's and 1's opposite to yop 
ynonop[ynonop == 1] = 2 
ynonop[ynonop == 0] = 1 
ynonop[ynonop == 2] = 0 
import matplotlib.transforms as mtransforms 
trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes) 
ax.set_xlim(x[0], x[-1]) 
ax.fill_between(x, 0, 1, where=yop > 0, facecolor='green', alpha=0.5, interpolate = True, transform=trans) 
ax.fill_between(x, 0, 1, where=ynonop > theta, facecolor='red', alpha=0.5, interpolate = True, transform=trans) 
plt.show() 
# plt.savefig('test.png', bbox_inches = 0) 

enter image description here

答えて

2

を、あなたはプロットを拡大します。

enter image description here

fill_betweenは、特定の条件を満たす点の間を埋めているので、あなたは、鋸歯状の形状を取得します。

可能な解決策は、broken_barhプロットを使用することです。この目的のために、データを2列形式(位置、幅)に再配置する必要があります。

import matplotlib.pyplot as plt 
import numpy as np 

fig, (ax,ax2) = plt.subplots(nrows=2, sharex=True, sharey=True) 

x = np.arange(0.0, 365, 1) 
yop = np.random.randint(2, size=len(x)) 
ynonop = np.copy(yop) 
# make 0's and 1's opposite to yop 
ynonop[ynonop == 1] = 2 
ynonop[ynonop == 0] = 1 
ynonop[ynonop == 2] = 0 

trans = ax.get_xaxis_transform() 
ax.set_xlim(x[0], x[-1]) 
ax.fill_between(x, 0, 1, where=yop > 0, facecolor='green', 
       alpha=0.5, interpolate = True, transform=trans) 
ax.fill_between(x, 0, 1, where=ynonop > 0, facecolor='red', 
       alpha=0.5, interpolate = True, transform=trans) 

trans2 = ax2.get_xaxis_transform() 
xra = np.c_[x[:-1],np.diff(x)] 
ax2.broken_barh(xra[yop[:-1] > 0,:], (0,1), 
         facecolors='green', alpha=0.5, transform=trans2) 

ax2.broken_barh(xra[ynonop[:-1] > 0,:], (0,1), 
         facecolors='red', alpha=0.5, transform=trans2) 

ax.set_title("fill_between") 
ax2.set_title("broken_barh") 
plt.show() 

enter image description here

1

また、ピクセルデータスペースに落ちる場所を正確にあなたがコントロールすることができますextentでxの値を微調整することによりimshow

import matplotlib.pyplot as plt 
import numpy as np 
import matplotlib.colors as mcolors 
import matplotlib.transforms as mtransforms 

fig, ax = plt.subplots() 
x = np.arange(0.0, 365, 1) 
yop = np.random.randint(2, size=len(x)) 

trans = mtransforms.blended_transform_factory(ax.transData, ax.transAxes) 
ax.set_xlim(x[0], x[-1]) 
lc = mcolors.ListedColormap(['r', 'g'], name='RWG') 
ax.imshow(yop.reshape(1, -1), 
      extent=[0, len(yop), 0, 1], 
      transform=trans, 
      cmap=lc, 
      norm=mcolors.NoNorm(), alpha=.5) 

ax.set_aspect('auto') 
# debugging plotting 
ax.step(x, yop, '.', where='post', linestyle='none') 
ax.set_ylim([-.1, 1.1]) 
plt.show() 

enter image description here

を使用してこれを行うことができます。

関連する問題