python 画子图+双坐标轴图
# 双坐标轴和单坐标轴
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False
# 子图中包含双坐标轴
fig, axs = plt.subplots(1, 2, figsize=[20, 5])
fig.suptitle('对比趋势图', fontsize=20) # 设置整体标题
x_xticks = df_fin['date'].values
y1 = df_fin['money'].values
y2 = df_fin['cnt'].values
axs[0].plot(x_xticks, y1, color='r', label='money')
axs[0].xaxis.set_tick_params(labelrotation=45) # x轴旋转45
axs[0].set_title('双坐标轴趋势', fontsize=16) # 设置子图标题及大小
axs[0].set_xlabel('日期') # 设置x轴标签
axs[0].set_ylabel('金额/元') # 设置y轴标钱
axs_0_1 = axs[0].twinx() # 添加双坐标轴
axs_0_1.plot(x_xticks, y2, color='y', label='cnt')
axs[0].legend() # 添加图例
axs_0_1.legend()
axs_0_1.set_ylabel('个数/个')
axs[1].plot(x_xticks, y1, color='r', label='money')
axs[1].plot(x_xticks, y2, color='y', label='cnt')
axs[1].set_title('单坐标轴趋势')
axs[1].set_xlabel('日期')
# fig.legend()
axs[1].legend()
axs[1].tick_params(labelrotation=30) # 所有标签旋转