不同y轴的图例表示方法
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
from matplotlib import rc
num_list1 = [152.8,122.23,101.9,87.31,76.4,68,61.15,55.6,52.41]
num_list2 = [23393.3,17956.2,14406,11938.7,10180.5,8899.45,7917.17,7178.32,6832.4]
for i in range(len(num_list2)):
num_list2[i] /= 1000
fig = plt.figure()
ax = fig.add_subplot(111)
x = range(4, 13)
ax.bar(x=x, height=num_list1, width=0.4, alpha=0.8, color='#00a6ac', label='time(s)')
ax2 = ax.twinx()
ax2.bar(x=[i + 0.4 for i in x], height=num_list2, width=0.4, color='#d71345', label='Energy Consumption(kJ)')
fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax.transAxes)
font = {'family': 'Arial', 'weight': 'normal', 'size': 20}
ax.set_xlabel("Maximal Speed(m/s)", font)
ax.set_ylabel('Time(s)', font)
x_locator = MultipleLocator(1) # 横轴间隔
ax.xaxis.set_major_locator(x_locator)
ax.spines['top'].set_linewidth(2) # 边框加粗
ax.spines['bottom'].set_linewidth(2)
ax.spines['left'].set_linewidth(2)
ax.spines['right'].set_linewidth(2)
font2 = {'family': 'Arial', 'weight': 'normal', 'size': 20, 'color':'#d71345'}
ax2.set_ylabel('Energy Consumption(kJ)', font2)
plt.tick_params(axis='y', colors='#d71345')
plt.show()