gpt4 book ai didi

python - 如何使用 matplotlib 创建相等数量的主要和次要 y 轴刻度?

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:44 28 4
gpt4 key购买 nike

我一直在努力创建一个带有次轴的图,以便主轴和次轴具有相同数量的主要刻度,从而使网格线重合。在下图中,我在次轴上显示了网格线来说明问题。

enter image description here

通过手动设置次轴限制,我得到了这个图,这是我想要的输出:

enter image description here

我已经包含了可重现的代码:

import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('data.dat', skiprows=2, delimiter=',', unpack=True).transpose()
time = data[:,0]
pressure = data[:,1]
lift = data[:,2]
figure_pressure_trace = plt.figure(figsize=(5.15, 5.15))
figure_pressure_trace.clf()
P_vs_t = plt.subplot(111)
P_vs_t.plot(time, pressure, linewidth=1.0)
P_vs_t.set_ylabel(r'\textit{Pressure (bar)}', labelpad=6)
P_vs_t.set_xlabel(r'\textit{Time (ms)}', labelpad=6)
lift_vs_t = P_vs_t.twinx()
lift_vs_t.plot(time, lift, color='#4DAF4A')
lift_vs_t.set_ylabel(r'\textit{Lift(mm)}', labelpad=6)
plt.show()
plt.close()

数据可用here .

更新:

我创建了一个函数来创建相等数量的报价,整个代码是:

import numpy as np
import matplotlib.pyplot as plt

def equal_y_ticks(primary, secondary):
y_min_primary, y_max_primary = primary.get_ybound()
y_min_secondary, y_max_secondary = secondary.get_ybound()
primary_ticks = len(primary.yaxis.get_major_ticks())
secondary_ticks = len(secondary.yaxis.get_major_ticks())
primary_spacing = (y_max_primary - y_min_primary) / (primary_ticks - 1)
secondary_spacing = (y_max_secondary - y_min_secondary) / (secondary_ticks - 1)
ticks = max(primary_ticks, secondary_ticks)
if secondary_ticks < primary_ticks:
y_max_secondary = y_min_secondary + (primary_ticks * secondary_spacing)
secondary.yaxis.set_ticks(np.arange(y_min_secondary, y_max_secondary, secondary_spacing))
else:
y_max_primary = y_min_primary + (secondary_ticks * primary_spacing)
primary.yaxis.set_ticks(np.arange(y_min_primary, y_max_primary, primary_spacing))

data = np.loadtxt('data.dat', skiprows=2, delimiter=',', unpack=True).transpose()
time = data[:,0]
pressure = data[:,1]
lift = data[:,2]
figure_pressure_trace = plt.figure(figsize=(5.15, 5.15))
figure_pressure_trace.clf()
P_vs_t = plt.subplot(111)
P_vs_t.plot(time, pressure, linewidth=1.0)
P_vs_t.set_ylabel(r'\textit{Pressure (bar)}', labelpad=6)
P_vs_t.set_xlabel(r'\textit{Time (ms)}', labelpad=6)
lift_vs_t = P_vs_t.twinx()
lift_vs_t.plot(time, lift, color='#4DAF4A')
equal_y_ticks(P_vs_t, lift_vs_t)
lift_vs_t.set_ylabel(r'\textit{Lift(mm)}', labelpad=6)
plt.show()
plt.close()

但是这个函数给我这样的图(对于一些数据):

enter image description here

最佳答案

我认为您正在寻找 LinearLocator (docs)

import matplotlib.pyplot as plt
from matplotlib import ticker as mtick
fig, ax = plt.subplots()
ax2 = ax.twinx()

ax.yaxis.set_major_locator(mtick.LinearLocator(5))
ax2.yaxis.set_major_locator(mtick.LinearLocator(5))

ax.set_ylim(0, 15)
ax2.set_ylim(0, 1500)
ax.yaxis.grid(True, lw=7, color='g', ls='--')
ax2.yaxis.grid(True, color='k', ls='-', lw=3)

这将在最小值和最大值之间放置 N 个均匀间隔的刻度。

enter image description here

关于python - 如何使用 matplotlib 创建相等数量的主要和次要 y 轴刻度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28566328/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com