在主要网格线下方绘制次要网格线 [英] Draw minor grid lines below major gridlines

查看:67
本文介绍了在主要网格线下方绘制次要网格线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib绘制网格.网格的zorder应该在图中所有其他线的后面.到目前为止,我的问题是次要网格线总是绘制在主要网格线的前面,即

I am trying to draw a grid using matplotlib. The zorder of the grid should be behind all other lines in the plot. My problem so far is that the minor grid lines are always drawn in front of the major grid lines i.e.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator, FormatStrFormatter

plt.rc('text', usetex=True)
plt.rc('font', family='serif')

f = plt.figure(figsize=(4,4))
ax = f.add_subplot(111)

ax.xaxis.set_minor_locator(MultipleLocator(1))
ax.xaxis.set_major_locator(MultipleLocator(10))
ax.yaxis.set_minor_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(10))

majc ="#3182bd"
minc ="#deebf7"

ax.xaxis.grid(True,'minor',color=minc, ls='-', lw=0.2)
ax.yaxis.grid(True,'minor',color=minc, ls='-', lw=0.2)
ax.xaxis.grid(True,'major',color=majc, ls='-')
ax.yaxis.grid(True,'major',color=majc,ls ='-')
ax.set_axisbelow(True)

x = np.linspace(0, 30, 100)
ax.plot(x, x, 'r-', lw=0.7)

[line.set_zorder(3) for line in ax.lines]
plt.savefig('test.pdf')

有什么建议吗?谢谢.

特写示例

推荐答案

更具体地说,它看起来像是按顺序绘制垂直大调、垂直小调、水平大调、水平小调和绘制的线条.在matplotlib基础知识中可能相当深.

Even more specifically, it looks like it draws vertical majors, vertical minors, horizontal majors, horizontal minors, and plotted lines, in that order. Probably pretty deep in the matplotlib basics.

对于您使用的颜色,您可以通过 alpha 来区分主要和次要颜色,而不是 RGB.更改示例的两行:

For the colors you're using, you could work around by distinguishing major and minor by alpha, not RGB. Changing two lines of your example:

ax.xaxis.grid(True,'minor',color=majc, alpha=0.2, ls='-', lw=0.2)
ax.yaxis.grid(True,'minor',color=majc, alpha=0.2, ls='-', lw=0.2)

结果:

这篇关于在主要网格线下方绘制次要网格线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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