Matplotlib:将网格线保留在图形后面,但将y和x轴保留在上方 [英] Matplotlib: keep grid lines behind the graph but the y and x axis above

查看:379
本文介绍了Matplotlib:将网格线保留在图形后面,但将y和x轴保留在上方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在我的图形下绘制网格线而又不弄乱x和y主轴的zorder:

I am having a hard time plotting grid lines under my graphs without messing with the main x and y axis zorder:

import matplotlib.pyplot as plt
import numpy as np


N = 5
menMeans = (20, 35, 30, 35, 27)
menStd =   (2, 3, 4, 1, 2)

ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd, alpha=0.9, linewidth = 0,zorder=3)

womenMeans = (25, 32, 34, 20, 25)
womenStd =   (3, 5, 2, 3, 3)
rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd, alpha=0.9, linewidth = 0,zorder=3)

# add some
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(ind+width)
ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') )

ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') )

fig.gca().yaxis.grid(True, which='major', linestyle='-', color='#D9D9D9',zorder=2, alpha = .9)
[line.set_zorder(4) for line in ax.lines]

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)

plt.show()

该示例取自matplotlib自己的示例,并且我进行了一些微调以显示如何使问题出现.我无法发布图像,但是如果运行代码,您将看到条形图绘制在水平网格线上方以及x和y轴上方.我不希望图形隐藏x和y轴,尤其是当刻度线也被阻塞时.

The example is taken from matplotlib's own, and I have tweaked a little to show how to make the problem appear. I cannot post images but if you run the code you will see that the bars are plotted above the horizontal grid lines as well as above the x and y axis. I do not want the x and y axis being hidden by the graph, especially when the ticks are also getting blocked.

推荐答案

我已经尝试过matplotlib 1.2.1、1.3.1rc2和master(提交06d014469fc5c79504a1b40e7d45bc33acc00773)

I have tried matplotlib 1.2.1, 1.3.1rc2 and master (commit 06d014469fc5c79504a1b40e7d45bc33acc00773)

要使轴尖位于条形图的顶部,您可以执行以下操作:

To get the axis spines on top of the the bars you can do the following:

for k, spine in ax.spines.items():  #ax.spines is a dictionary
    spine.set_zorder(10)

编辑

看来我无法使刻度线位于条形图的顶部.我已经尝试过

It seems that I can't make the tick lines to go on top of the bars. I've tried

1. ax.tick_params(direction='in', length=10, color='k', zorder=10)
   #This increases the size of the lines to 10 points, 
   #but the lines stays hidden behind  the bars
2. for l in ax.yaxis.get_ticklines():
       l.set_zorder(10)

和其他没有结果的方式.似乎在绘制条形图时,它们被放在顶部,而zorder被忽略了

and some other way with no results. It seems that when drawing the bars they are put on top and the zorder is ignored

一种解决方法是向外绘制刻度线

A workaround could be to draw the tick lines outwards

ax.tick_params(direction='out', length=4, color='k', zorder=10)

或同时使用direction='inout'

EDIT2

在@tcaswell评论之后,我已经做了一些测试.

I've done some test after @tcaswell comments.

如果将ax.bar函数中的zorder设置为< = 2,则在条上方绘制轴,刻度线网格线.如果值大于2.01(轴的默认值),则在轴,刻度线和网格的顶部绘制条形图.然后可以为棘刺设置更大的值(如上所述),但是更改刻度线zorder的任何尝试都将被忽略(尽管这些值会在相应的艺术家上更新).

If zorder in the ax.bar function is set to be <=2, the axis, ticklines and grid lines are drawn above the bars. If the valus is >2.01 (the default value for axis) the bars are drawn on top of the axis, ticklines and grid. Then it possible to set larger values to the spines (as above) but any attempt to change the zorder of the ticklines is simply ignored (although the values are updated on the corresponding artists).

我尝试将zorder=1用于bar,将zorder=0用于网格,并且将网格绘制在条的顶部 .因此,zorder被忽略.

I've tried the to use zorder=1 for the bar and zorder=0 for the grid and the grid is drawn on top of the bars. So zorder is ignored.

回顾

在我看来,刻度线和网格zorder只是被忽略并保留为默认值.对我来说,这是一个与bar或某些patches相关的错误.

It seems to me that ticklines and grid zorder are just ignored and kept to the default values. For me this is a bug somehow related with bar or some patches.

顺便说一句,我确实记得使用imshow

BTW, I do remember changing successfully the zorder in ticklines when using imshow

这篇关于Matplotlib:将网格线保留在图形后面,但将y和x轴保留在上方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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