Python Matplotlib-在yaxis顶部的行之间填充\删除空格

Python Matplotlib-在yaxis顶部的行之间填充\删除空格,python,matplotlib,Python,Matplotlib,当我使用彩色面片时,它们会稍微垂直倾斜,因此在y轴的顶部有空白,而颜色会很好地合并在y轴的底部。有人知道如何预防/了解这是什么原因吗 该图显示了一个“天气窗口”:当天气参数低于某个阈值时,时间段为“可运行”,而在其他时间段为“不可运行”。生成此绘图的代码为: figure = plt.figure(figsize=(8, 3 * 3)) gs = gridspec.GridSpec(3, 1) gs.update(hspace=0.3) ax0 = plt.subplot(gs[0]) df1.

当我使用彩色面片时,它们会稍微垂直倾斜,因此在y轴的顶部有空白,而颜色会很好地合并在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)

要了解白色条纹的原因,可以放大绘图

因为满足特定条件的点之间的填充,所以得到了锯齿状的形状

一个可能的解决方案可能是使用绘图。为此,需要将数据重新设置为(位置、宽度)的2列格式


要了解白色条纹的原因,可以放大绘图

因为满足特定条件的点之间的填充,所以得到了锯齿状的形状

一个可能的解决方案可能是使用绘图。为此,需要将数据重新设置为(位置、宽度)的2列格式


您也可以使用
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()


通过调整
范围中的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()

通过调整
extent
中的x值,可以精确控制像素在数据空间中的位置