如何在Matplotlib条形图后面绘制网格线

编程入门 行业动态 更新时间:2024-10-28 01:14:58
本文介绍了如何在Matplotlib条形图后面绘制网格线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 x = ['01-02', '02-02', '03-02', '04-02', '05-02'] y = [2, 2, 3, 7, 2] fig, ax = plt.subplots(1, 1) ax.bar(range(len(y)), y, width=0.3,align='center',color='skyblue') plt.xticks(range(len(y)), x, size='small') plt.savefig('/home/user/graphimages/foo2.png') plt.close()

我想在条形图后面绘制(x和y的)网格线.

I want to draw grid lines (of x & y) behind the bar graph.

推荐答案

要添加网格,只需添加

ax.grid()

如果您希望网格位于条形图的后面,请添加

If you want the grid to be behind the bars then add

ax.grid(zorder=0) ax.bar(range(len(y)), y, width=0.3, align='center', color='skyblue', zorder=3)

重要的是条形图的zorder大于网格.实验似乎zorder=3是实际上能提供所需效果的最低值.我不知道为什么zorder=1还不够.

The important part is that the zorder of the bars is greater than grid. Experimenting it seems zorder=3 is the lowest value that actually gives the desired effect. I have no idea why zorder=1 isn't sufficient.

编辑: 我注意到这个问题已经在此处使用了答案不同的方法,尽管它遭受一些链接腐烂.据我所知,这两种方法都产生相同的结果,但是安德鲁·库克的答案更为优雅.

EDIT: I have noticed this question has already been answered here using a different method although it suffers some link rot. Both methods yield the same result as far as I can see but andrew cooke's answer is more elegant.

更多推荐

如何在Matplotlib条形图后面绘制网格线

本文发布于:2023-07-18 00:50:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139275.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   如何在   条形图   Matplotlib

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!