基于 1. y 轴和 2. x 轴的网格显示在 matplotlib 中

问题描述 投票:0回答:1

对于我的学生,我需要一个带有 2 个 x 轴和 2 个 y 轴的空白图。为了获得更好的工作流程,有一个基于第一个 x 轴和第一个 y 轴的网格,因此他们可以轻松地手动从机器复制数据。他们可以在第二个 x 轴和 y 轴上检查计算结果。

在这里你可以看到我想要的情节(在paint.exe中操作:p)

您可以看到,网格完全位于蓝色(第一个)轴上,并且红色(第二个)x 轴上有一条线。

在下面的代码中,第二个 a 轴没有线,因为第 25 行中有 auf

frame_on=False

import matplotlib.pyplot as plt
import numpy as np

# put in some data outside the plot
x_values1=[-10,-10]
y_values1=[-10,-10]

x_values2=[-10,-10]
y_values2=[-10,-10]

fig=plt.figure()

# define 1st x- and y-axis
ax=fig.add_subplot(111, label="1")
ax.set_xlim(xmin=0, xmax=12)
ax.set_ylim(ymin=0, ymax=120)

# grid on after 1st x- and y-axis
ax.grid(which='major', color='#DDDDDD', linewidth=0.5)
ax.grid(which='minor', color='#EEEEEE', linestyle=':', linewidth=0.5)
ax.minorticks_on()
plt.grid(True, linestyle='--', color='gray', alpha=0.2, which='both')

# define 2nd x- and y-axis
ax2=fig.add_subplot(111, label="2", frame_on=False) # frame_on=True
ax2.set_xlim(xmin=0, xmax=24)
ax2.set_ylim(ymin=0, ymax=120000/78)

# label of 1st axis
ax.plot(x_values1, y_values1)
ax.set_xlabel("1st x-axis", color='blue')
ax.set_ylabel("1st y-axis", color='blue')

ax.tick_params(axis='x')
ax.tick_params(axis='y')

# label of 2nd axis and repositioning
ax2.scatter(x_values2, y_values2, color="red")
ax2.xaxis.tick_bottom()
ax2.yaxis.tick_right()
ax2.set_xlabel('2nd x-axis', color='red') 
ax2.set_ylabel('2nd y-axis' , color='black')       
ax2.xaxis.set_label_position('bottom')

# repositioning 2nd x-axis
ax2.spines['bottom'].set_position(('outward', 35))
ax2.yaxis.set_label_position('right') 
ax2.tick_params(axis='x')
ax2.tick_params(axis='y')
ax2.set_xticks(np.arange(0, 25, 2))

plt.subplots_adjust(bottom=0.2, right = 0.88)
plt.show()

如果我在第25行转动

frame_on=True
,就没有网格。

如果我将

plt.grid(True, linestyle='--', color='gray', alpha=0.2, which='both')
的位置切换到脚本末尾,则网格基于
frame_on=True
之后的第二个轴。

import matplotlib.pyplot as plt
import numpy as np

# put in some data outside the plot
x_values1=[-10,-10]
y_values1=[-10,-10]

x_values2=[-10,-10]
y_values2=[-10,-10]

fig=plt.figure()

# define 1st x- and y-axis
ax=fig.add_subplot(111, label="1")
ax.set_xlim(xmin=0, xmax=12)
ax.set_ylim(ymin=0, ymax=120)

# grid on after 1st x- and y-axis
ax.grid(which='major', color='#DDDDDD', linewidth=0.5)
ax.grid(which='minor', color='#EEEEEE', linestyle=':', linewidth=0.5)
ax.minorticks_on()
# plt.grid(True, linestyle='--', color='gray', alpha=0.2, which='both') # put it to the end

# define 2nd x- and y-axis
ax2=fig.add_subplot(111, label="2", frame_on=True) # frame_on=True
ax2.set_xlim(xmin=0, xmax=24)
ax2.set_ylim(ymin=0, ymax=120000/78)

# label of 1st axis
ax.plot(x_values1, y_values1)
ax.set_xlabel("1st x-axis", color='blue')
ax.set_ylabel("1st y-axis", color='blue')

ax.tick_params(axis='x')
ax.tick_params(axis='y')

# label of 2nd axis and repositioning
ax2.scatter(x_values2, y_values2, color="red")
ax2.xaxis.tick_bottom()
ax2.yaxis.tick_right()
ax2.set_xlabel('2nd x-axis', color='red') 
ax2.set_ylabel('2nd y-axis' , color='black')       
ax2.xaxis.set_label_position('bottom')

# repositioning 2nd x-axis
ax2.spines['bottom'].set_position(('outward', 35))
ax2.yaxis.set_label_position('right') 
ax2.tick_params(axis='x')
ax2.tick_params(axis='y')
ax2.set_xticks(np.arange(0, 25, 2))

plt.subplots_adjust(bottom=0.2, right = 0.88)

plt.grid(True, linestyle='--', color='gray', alpha=0.2, which='both')

plt.show()

有没有一种方法可以让我在不使用油漆的情况下得到像第一张图这样的图?

python matplotlib grid axis
1个回答
0
投票

“是的,先创建 ax2,然后在其之上创建 ax。 – Jody Klymak”就是这样,谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.