Python, Matplotlib: Normalising multiple plots to fit the same arbitrary axis limits -
i trying create figure containing several plots, , normalising them features can distinguished. i'm having trouble wording i'm trying do, example code below should clarify.
example plot generated below code.
the code creates figure 3 lines on it. black line's data varies between -1000 , 1000, scale adjusted accordingly. means variation in green data, , moreso in red, hard see. ideally enlarge green , red data variation clearer - without multiplying constant value.
example of result i'm hoping - different lines have different orders of magnitude have been fit arbitrary y-axis scale shape can demonstrated.
import numpy np import matplotlib.pyplot 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)) 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()
i've tried playing around things plt.ylim([0, 100]) each individual line plot, didn't seem work out. assistance here fantastic, cheers.
edit:
problem solved in comments importanceofbeingernest's improved normalisation technique, combined accepted answer. thanks!
you have option of normalizing data , have multiple twin y axes. see below.
import numpy np import matplotlib.pyplot 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)) 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 y-axis label, ticks , tick labels match 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 right axis light bit right 25 % of 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()
results in
Comments
Post a Comment