weixin_44624165 2022-04-20 09:29 采纳率: 94.1%
浏览 136
已结题

python_matplotlib 同时绘制面积图和折线图(即一个坐标轴下两种图)

问题遇到的现象和发生背景

我想实现在面积图上叠加折线图,生成类似这样的图

img

问题相关代码,请勿粘贴截图
x=range(1,29)
y1=meanminus
y2=meanplus
y3=meanlist
plt.fill_between(x,y1,y2)#面积图
plt.show()

plt.plot(x,y3)#折线图
plt.show()

meanlist= [0.240623606, 0.141448868, 0.141708252, 0.1134854405, 0.136074166, 0.148731597, 0.1127166275, 0.0932453725, 0.0906676125, 0.08559815900000001, 0.117364884, 0.221391108, 0.2649745125, 0.20113137749999999, 0.1462845265, 0.0927852045, 0.079287448, 0.08901221000000001, 0.078628505, 0.11750408000000001, 0.13092000250000002, 0.073606722, 0.0513180355, 0.084963851, 0.077851754, 0.0321400055, 0.0037168715, 0.0]
meanplus= [0.33521952090822227, 0.2360447829082223, 0.2363041669082223, 0.2080813554082223, 0.2306700809082223, 0.2433275119082223, 0.2073125424082223, 0.1878412874082223, 0.1852635274082223, 0.1801940739082223, 0.2119607989082223, 0.31598702290822234, 0.3595704274082223, 0.29572729240822226, 0.24088044140822232, 0.1873811194082223, 0.17388336290822232, 0.18360812490822231, 0.1732244199082223, 0.21209999490822232, 0.22551591740822233, 0.1682026369082223, 0.1459139504082223, 0.1795597659082223, 0.1724476689082223, 0.1267359204082223, 0.0983127864082223, 0.0945959149082223] 
meanminus= [0.14602769109177768, 0.0468529530917777, 0.0471123370917777, 0.0188895255917777, 0.04147825109177769, 0.05413568209177769, 0.018120712591777693, -0.0013505424082222994, -0.003928302408222312, -0.008997755908222299, 0.022768969091777697, 0.1267951930917777, 0.17037859759177768, 0.10653546259177768, 0.051688611591777706, -0.0018107104082223102, -0.01530846690822231, -0.005583704908222298, -0.015967409908222305, 0.022908165091777705, 0.036324087591777715, -0.020989192908222307, -0.04327787940822231, -0.0096320639082223, -0.01674416090822231, -0.06245590940822231, -0.09087904340822231, -0.0945959149082223]


运行结果及报错内容

上面代码运行出来是两个单独的图,不在一个坐标轴下,如何解决呢?

img


img

我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • pp 2022-04-20 10:13
    关注

    你有没有试试把上面的plt.show()去掉试一下

    img

    
    ```python
    import matplotlib.pyplot as plt
    
    meanlist = [0.240623606, 0.141448868, 0.141708252, 0.1134854405, 0.136074166, 0.148731597, 0.1127166275, 0.0932453725,
                0.0906676125, 0.08559815900000001, 0.117364884, 0.221391108, 0.2649745125, 0.20113137749999999,
                0.1462845265, 0.0927852045, 0.079287448, 0.08901221000000001, 0.078628505, 0.11750408000000001,
                0.13092000250000002, 0.073606722, 0.0513180355, 0.084963851, 0.077851754, 0.0321400055, 0.0037168715, 0.0]
    meanplus = [0.33521952090822227, 0.2360447829082223, 0.2363041669082223, 0.2080813554082223, 0.2306700809082223,
                0.2433275119082223, 0.2073125424082223, 0.1878412874082223, 0.1852635274082223, 0.1801940739082223,
                0.2119607989082223, 0.31598702290822234, 0.3595704274082223, 0.29572729240822226, 0.24088044140822232,
                0.1873811194082223, 0.17388336290822232, 0.18360812490822231, 0.1732244199082223, 0.21209999490822232,
                0.22551591740822233, 0.1682026369082223, 0.1459139504082223, 0.1795597659082223, 0.1724476689082223,
                0.1267359204082223, 0.0983127864082223, 0.0945959149082223]
    meanminus = [0.14602769109177768, 0.0468529530917777, 0.0471123370917777, 0.0188895255917777, 0.04147825109177769,
                 0.05413568209177769, 0.018120712591777693, -0.0013505424082222994, -0.003928302408222312,
                 -0.008997755908222299, 0.022768969091777697, 0.1267951930917777, 0.17037859759177768, 0.10653546259177768,
                 0.051688611591777706, -0.0018107104082223102, -0.01530846690822231, -0.005583704908222298,
                 -0.015967409908222305, 0.022908165091777705, 0.036324087591777715, -0.020989192908222307,
                 -0.04327787940822231, -0.0096320639082223, -0.01674416090822231, -0.06245590940822231,
                 -0.09087904340822231, -0.0945959149082223]
    # fig, ax = plt.subplots()
    
    x = range(1, 29)
    y1 = meanminus
    y2 = meanplus
    y3 = meanlist
    plt.fill_between(x, y1, y2)  # 面积图
    
    plt.plot(x, y3, c='r')  # 折线图
    plt.show()
    
    

    ```

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月28日
  • 已采纳回答 4月20日
  • 修改了问题 4月20日
  • 创建了问题 4月20日

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型