如何在 pyplot 图的特定区域设置背景颜色? [英] How can I set the background color on specific areas of a pyplot figure?

查看:232
本文介绍了如何在 pyplot 图的特定区域设置背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法用以下代码绘制了一系列点:

I've managed to plot a series of points with the following code:

plt = pp.figure()
for i in range(spt.shape[1]):
    spktrain = spt[0,i]
    for trial in spktrain:
        non_z = np.nonzero(trial)
        non_z = non_z[0]
        pp.plot(t[non_z], trial[non_z], 'bo')

我想在图中放置交替的白色和灰色背景带,以便将数据与外部 for 循环的每次迭代分开.换句话说,我希望每个spktrain"的数据都有自己的背景颜色(数据不重叠).

I would like to place alternating bands of white and gray background on the figure in order to separate the data from each iteration of the outer for loop. In other words, I would like the data from each "spktrain" to have it's own background color (the data does not overlap).

如何更改特定区域中图形的背景颜色?

How can I go about changing the background color of a figure in a specific region?

推荐答案

您可以像这样使用 axhspan 和/或 axvspan:

You can use axhspan and/or axvspan like this:

import matplotlib.pyplot as plt

plt.figure()
plt.xlim(0, 5)
plt.ylim(0, 5)

for i in range(0, 5):
    plt.axhspan(i, i+.2, facecolor='0.2', alpha=0.5)
    plt.axvspan(i, i+.5, facecolor='b', alpha=0.5)

plt.show()

这篇关于如何在 pyplot 图的特定区域设置背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆