2017-06-14 79 views
0

当我使用fill_between彩色补丁是略微倾斜的竖直所以有在y轴的顶部的空白,而颜色在底部很好地合并在Y轴。任何人都知道如何防止/明白是什么原因造成的? enter image description here的Python Matplotlib - fill_between在顶部除去线之间的白色空间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情节。为此人会需要将数据rearange到(位置,宽度)的2columns格式。

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

你也可以做到这一点使用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

通过extent调整x值可以控制在何处像素落在数据空间。

相关问题