防止 'darkgrid' twinx() 图中的 ax2 网格线分割 ax1 曲线

Prevent 'darkgrid' ax2 gridlines in twinx() plot from disecting ax1 curve

在绘制 twinx() 图时,我无法阻止 'darkgrid' 中的网格线分割与 X1 轴关联的线。我可以通过不使用 'darkgrid' 或将空列表传递给 X2(并将轴标签丢失到 -se 最后一行)来“解决”问题,但我确实想要 'darkgrid' 和 x2 轴标签。

#Some imports
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set_style("darkgrid")
%matplotlib inline

#Data
d = np.arange(0, 1000, 100)
x1 = d/30         #redmodel
x2 = np.sqrt(d)   #bluemodel

#Figure
fig = plt.figure(figsize = (8, 12))

sy1 = 'r-'        #redmodel
sy2 = 'b-'        #bluemodel

ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()

_ = ax1.plot(x1, d, sy1)
_ = ax1.set_ylabel('D')
_ = ax1.set_ylim(0, 1000)
_ = ax1.set_xlabel('X1')
_ = ax1.set_xlim(0, 31)

_ = ax2.plot(x2, d, sy2)
_ = ax2.set_xlabel('X2')
_ = ax2.set_xlim(0, 31)
#_ = ax2.set_xticks([]) #Empty list passed to omit_xticks, otherwise ax2 gridines disect red line

当我在寻找这个问题的解决方案时,我偶然发现了 axes_grid1 工具包 帮助程序 类 的集合,它具有 twin() 选项(除了 twinx 和 twiny)这可能是我的问题的解决方案。但是如果你知道一个更简单的请帮助我。

你的问题的目的是回答,因为挑战是在红线上绘制 x2 轴的网格线。我觉得你可以简单的给x2轴的网格线设置一个标准。

_ = ax2.grid(which='major', axis='x', zorder=1.0)