matplotlib - 斑马条纹图形的背景颜色?

发布于 2024-08-31 20:10:58 字数 346 浏览 1 评论 0原文

我正在使用 matplotlib 构建一个简单的折线图,并且我想在图表的背景上添加斑马条纹,以便每个交替行的颜色都不同。有办法做到这一点吗?

我的图表已经有网格,并且只有主要刻度。

编辑:来自我下面评论的代码,但更清晰:

yTicks = ax.get_yticks()[:-1]
xTicks = ax.get_xticks()
ax.barh(yTicks, [max(xTicks)-min(xTicks)] * len(yTicks),
    height=(yTicks[1]-yTicks[0]), left=min(xTicks), color=['w','#F0FFFF'])

I'm building a simple line chart with matplotlib, and I'd like to zebra-stripe the background of the chart, so that each alternating row is colored differently. Is there a way to do this?

My chart already has gridding, and has major ticks only.

Edit: The code from my comment below, but more legible:

yTicks = ax.get_yticks()[:-1]
xTicks = ax.get_xticks()
ax.barh(yTicks, [max(xTicks)-min(xTicks)] * len(yTicks),
    height=(yTicks[1]-yTicks[0]), left=min(xTicks), color=['w','#F0FFFF'])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2024-09-07 20:10:58

这是一个使用条形图(axes.barh)来模拟条带化的快速技巧。

import matplotlib.pyplot as plt

# initial plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3,4,5])

yTickPos,_ = plt.yticks()
yTickPos = yTickPos[:-1] #slice off the last as it is the top of the plot
# create bars at yTickPos that are the length of our greatest xtick and have a height equal to our tick spacing
ax.barh(yTickPos, [max(plt.xticks()[0])] * len(yTickPos), height=(yTickPos[1]-yTickPos[0]), color=['g','w'])

plt.show()

产生:

在此输入图像描述

Here's a quick hack that uses a barchart (axes.barh) to simulate striping.

import matplotlib.pyplot as plt

# initial plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3,4,5])

yTickPos,_ = plt.yticks()
yTickPos = yTickPos[:-1] #slice off the last as it is the top of the plot
# create bars at yTickPos that are the length of our greatest xtick and have a height equal to our tick spacing
ax.barh(yTickPos, [max(plt.xticks()[0])] * len(yTickPos), height=(yTickPos[1]-yTickPos[0]), color=['g','w'])

plt.show()

Produces:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文