python 画子图+双坐标轴图

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)  # 所有标签旋转

在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值