我试图在两个y轴上绘制两条曲线,如图所示。但是绿色图(针举)被绘制在主轴图上方,即使我将zorder设置为1和2作为红色图(压力)。
我使用的代码:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker as tick
data = np.genfromtxt("secondary_axis.dat", skiprows = 2, delimiter = ',')
time = data[:, 0]
pressure = data[:, 1] * 0.006894759086775369
pressure_charge = data[0, 0]
needle_lift = data[:, 2]
figure = plt.figure(figsize=(5.15, 5.15))
figure.clf()
plot = plt.subplot(111)
plot.plot(time, pressure, label = r'\textit{Raw}', zorder = 2)
plot.set_xlabel(r'\textit{X}', labelpad=6)
plot.set_ylabel(r'\textit{Y}', labelpad=6)
primary_ticks = len(plot.yaxis.get_major_ticks())
ax2 = plot.twinx()
ax2.plot(time, needle_lift, color='#4DAF4A', zorder = 1)
ax2.grid(False)
ax2.set_ylabel(r'\textit{Z}', labelpad=6)
ax2.yaxis.set_major_locator(tick.LinearLocator(primary_ticks))
plt.show()
数据可用here
如何使用matplotlib将绿色(针头提升)图放在红色图(压力)下面?
使用rc设置更新了代码
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc_context
from matplotlib import ticker as tick
def init_plot_dark():
params = {'backend' : 'agg', 'legend.numpoints' : 1,
'lines.linewidth' : 1.0, 'lines.linestyle' : '-', 'axes.facecolor' : '#EEEEEE', 'axes.edgecolor' : '#FFFFFF',
'axes.linewidth' : 0.0, 'axes.grid' : True, 'axes.titlesize' : 'large', 'axes.labelsize' : 12, 'axes.labelweight' : 'normal',
'axes.labelcolor' : '000000', 'axes.axisbelow' : True, 'polaraxes.grid' : True , 'axes3d.grid' : True,
'axes.color_cycle' : ('#E41A1C', '#377EB8', '#4DAF4A', '#984EA3', '#FF7F00', '#FFFF33', '#A65628', '#F781BF', '#999999'),
'xtick.major.size' : 4 ,'xtick.minor.size' : 2 ,'xtick.major.width' : 0 ,'xtick.minor.width' : 0 ,'xtick.major.pad' : 6,
'xtick.minor.pad' : 6, 'xtick.color' : '#000000' , 'xtick.labelsize' : 10, 'xtick.direction' : 'in',
'ytick.major.size' : 4, 'ytick.minor.size' : 2 ,'ytick.major.width' : 0 ,'ytick.minor.width' : 0 ,'ytick.major.pad' : 6,
'ytick.minor.pad' : 6 ,'ytick.color' : '#000000', 'ytick.labelsize' : 10, 'ytick.direction' : 'in',
'grid.color' : '#FFFFFF', 'grid.linestyle' : '-', 'grid.linewidth' : 0.5, 'grid.alpha' : 1.0,
'legend.fontsize' : 10, 'legend.borderaxespad' : 0.5, 'legend.shadow' : False, 'legend.frameon' : True}
plt.rcParams.update(params)
init_plot_dark()
data = np.genfromtxt("secondary_axis.dat", skiprows = 2, delimiter = ',')
time = data[:, 0]
pressure = data[:, 1] * 0.006894759086775369
pressure_charge = data[0, 0]
needle_lift = data[:, 2]
figure = plt.figure(figsize=(5.15, 5.15))
figure.clf()
plot = plt.subplot(111)
plot.plot(time, pressure, label = r'\textit{Raw}', zorder = 2)
plot.set_xlabel(r'\textit{X}', labelpad=6)
plot.set_ylabel(r'\textit{Y}', labelpad=6)
primary_ticks = len(plot.yaxis.get_major_ticks())
ax2 = plot.twinx()
ax2.plot(time, needle_lift, color='#4DAF4A', zorder = 1)
ax2.grid(False)
ax2.set_ylabel(r'\textit{Z}', labelpad=6)
ax2.yaxis.set_major_locator(tick.LinearLocator(primary_ticks))
plt.show()
答案 0 :(得分:4)
您需要将第一个轴的zorder设置为高于第二个轴的zorder,但是在绘制它们之后需要这样做。在plt.show()
plot.set_zorder(ax2.get_zorder()+1)
plot.patch.set_visible(False)
第一行得到zorder正确,但是会很好地隐藏第二个轴。因此,我们还需要使用第二行删除画布。
修改强>
要使用rcParams
获取网格线,您可以执行以下操作:切换在plot
轴和ax2
轴上绘制的线(即压力{ {1}}并抬起ax2
),然后改变它们的轴和刻度标签的哪一侧:
plot
答案 1 :(得分:0)
上面的答案是正确的,但是我想写一个更短,更简单的答案。
给出:
fig, ax1 = plt.subplots()
ax2 = ax1.twiny()
只需添加(在绘图之前或之后):
ax1.set_zorder(1) # default zorder is 0 for ax1 and ax2
ax1.patch.set_visible(False) # prevents ax1 from hiding ax2