【发布时间】:2015-09-21 11:39:36
【问题描述】:
我有一个库函数,可以将数据绘制到包含多个子图的 pyplot 图中。 我只是在所有子图中添加了网格线,但它们覆盖了实际数据,但我希望它们在后台。 我尝试更改绘图和 ax.plot() ax.grid() 命令的执行顺序,但这没有影响。 有没有办法强制网格进入背景?
相关的额外问题:我也使用 axhline 来指定 x=0 线,但它始终假定网格颜色,即使它被指定为不同的颜色...
代码目前的工作方式:
def plot_the_things(fig=None):
# in case no figure is provided, make a new one,
# otherwise add to the existing one
plot_fig=fig if fig else plt.figure()
#[...some calculations of data ...]
plot_ax1 = plot_fig.add_subplot(3,3,1)
plot_ax1.axhline(y=0, ls='-', color='0.5')
plot_ax1.plot(self.diff_3[:,0],self.diff_3[:,1])
# [...setting labels, adapt axes limits in case the new data needs wider ones..]
plot_ax1.grid(b=True, which='major', axis='both', c='0.75', ls='-', linewidth=1)
# this is repeated in similar fashion for the other axes -- there are 9 of
# them, each plotting something different in a different axes
这个函数被多次调用。更准确地说:它实际上是一个类的一部分。我有这个类的多个实例并调用它们,传入同一个图形对象。然后每个实例绘制自己的数据,效果很好,甚至axhline() 也正确显示(在数据下方!)但是在我输入添加网格的命令后,它总是显示在数据顶部并覆盖axhline,很烦人。
...有什么办法可以解决这个问题?
(我认为我可以而且也许应该将所有只需要运行一次的东西移到一个不会重复执行但现在时间和精神资源很少的地方,所以我用最快的方式去了工作......但我不希望这会改变任何东西)
【问题讨论】:
标签: python matplotlib