Python,Matplotlib:标准化多个图以适应相同的任意轴限制

Mil*_*ilo 5 python axis limits matplotlib scale

我正在尝试创建一个包含多个图的图形,并对它们进行标准化,以便可以轻松地区分它们的特征。我在表达我想要做什么时遇到一些困难,但下面的示例代码应该有助于澄清。

由以下代码生成的示例图。

该代码创建一个包含三行的图形。黑线的数据在-1000和1000之间变化,因此比例会相应调整。这意味着绿色数据的变化,甚至红色数据的变化都很难看到。理想情况下,我希望放大绿色和红色数据,以便它们的变化更清晰 - 但希望不只是乘以一个常数值。

我希望得到的结果示例 - 不同的线都有不同的数量级,但已适合任意 y 轴比例,因此可以演示它们的形状。

import numpy as np
import matplotlib.pyplot as plt

time_arr = np.arange(0, 25, 1)

dist_arr = np.zeros(len(time_arr))
speed_arr = np.zeros(len(time_arr))
energy_arr = np.zeros(len(time_arr))

for i in range(len(time_arr)):
    dist_arr[i] = np.random.randint(-10, 10)
    speed_arr[i] = np.random.randint(-50, 50)
    energy_arr[i] = np.random.randint(-1000, 1000)


fig = plt.figure(figsize=(13,13))

plt.plot(time_arr, dist_arr, 'red', linestyle='-', label='dist (m)', linewidth=5)
plt.plot(time_arr, speed_arr, 'lime', linestyle='-', label='speed (m/s)', linewidth=5)
plt.plot(time_arr, energy_arr, 'black', linestyle='--', label='E_tot (J)', linewidth=5)

plt.xlabel('Time (s)', fontsize=25)
plt.ylabel('Various Params', fontsize=25)
plt.tick_params(axis='x', labelsize=20)
plt.tick_params(axis='y', labelsize=20)
plt.title('Various VS Time', fontsize = 32, y=1.008)
plt.legend(loc='best', fontsize=25)

plt.show()
Run Code Online (Sandbox Code Playgroud)

我尝试过对每个单独的线图使用 plt.ylim([0, 100]) 之类的东西,但这似乎没有成功。这里的任何帮助都会很棒,干杯。

编辑:

由于 ImportanceOfBeingErnest 改进的标准化技术以及已接受的答案,评论中的问题得到了解决。谢谢!

pla*_*360 5

您可以选择标准化数据,也可以有多个双 y 轴。见下文。

import numpy as np
import matplotlib.pyplot as plt

def norm(data):
    return (data)/(max(data)-min(data))

time_arr = np.arange(0, 25, 1)

dist_arr = np.zeros(len(time_arr))
speed_arr = np.zeros(len(time_arr))
energy_arr = np.zeros(len(time_arr))

for i in range(len(time_arr)):
    dist_arr[i] = np.random.randint(-10, 10)
    speed_arr[i] = np.random.randint(-50, 50)
    energy_arr[i] = np.random.randint(-1000, 1000)

# option 1
fig = plt.figure(figsize=(10,10))

plt.plot(time_arr, norm(dist_arr), 'red', linestyle='-', label='dist (m)', linewidth=5)
plt.plot(time_arr, norm(speed_arr), 'lime', linestyle='-', label='speed (m/s)', linewidth=5)
plt.plot(time_arr, norm(energy_arr), 'black', linestyle='--', label='E_tot (J)', linewidth=5)

plt.xlabel('Time (s)', fontsize=25)
plt.ylabel('Various Params', fontsize=25)
plt.tick_params(axis='x', labelsize=20)
plt.tick_params(axis='y', labelsize=20)
plt.title('Various VS Time', fontsize = 32, y=1.008)
plt.legend(loc='best', fontsize=25)

plt.show()

# option 2
fig, ax1 = plt.subplots(1,1,figsize=(12,9))

ax1.plot(time_arr, dist_arr, 'red', linestyle='-', label='dist (m)', linewidth=5)
ax1.set_xlabel('Time (s)', fontsize=25)
# Make the y-axis label, ticks and tick labels match the line color.
ax1.set_ylabel('Distance(m)', color='r',fontsize=25)
ax1.tick_params('y', colors='r', labelsize=20)

ax2 = ax1.twinx()
ax2.plot(time_arr, speed_arr, 'lime', linestyle='-', label='speed (m/s)', linewidth=5)
ax2.set_ylabel('Speed (m/s)', color='lime',fontsize=25)
ax2.tick_params('y', colors='lime', labelsize=20)

ax3 = ax1.twinx()
ax3.spines['right'].set_position(('axes', 1.25)) # move the right axis light bit to the right by 25 % of the axes
ax3.plot(time_arr, norm(energy_arr), 'black', linestyle='--', label='E_tot (J)', linewidth=5)
ax3.set_ylabel('E_tot (J)', color='black',fontsize=25)
ax3.tick_params('y', colors='black', labelsize=20)


fig.tight_layout()
plt.show()
Run Code Online (Sandbox Code Playgroud)

结果是

在此输入图像描述 在此输入图像描述