python绘制网格线在原图上面_python – Matplotlib:在图形后面保留网格线,但上面的y和x轴...

我很难在我的图形下绘制网格线而不会弄乱主x轴和y轴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轴,特别是当刻度线也被阻挡时.

解决方法:

我试过matplotlib 1.2.1,1.3.1rc2和master(commit 06d014469fc5c79504a1b40e7d45bc33acc00773)

要将轴刺刺在条形图的顶部,您可以执行以下操作:

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

spine.set_zorder(10)

编辑

似乎我不能让刻度线在栏杆上方.我试过了

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被忽略

解决方法可能是向外绘制刻度线

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

或者向内和向外,使用direction =’inout’

EDIT2

我在@tcaswell评论后做了一些测试.

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

我已经尝试使用zorder = 1作为条形,zorder = 0作为网格,网格绘制在条形图的顶部.所以zorder被忽略了.

概括

在我看来,滴答线和网格zorder只是被忽略并保持默认值.对我来说,这是一个与bar或某些补丁有关的错误.

顺便说一句,我确实记得在使用imshow时成功更改了刻度线中的zorder

标签:python,matplotlib,plot

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值