You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python绘制降雨量与水流量镜像组合折线图

要实现上下拆分的镜像组合图表,我们可以通过matplotlib创建共享x轴的上下两个子图,将下半部分子图的y轴反转即可达到倒置效果,以下是完整可运行的代码:

import pandas as pd
import random
import matplotlib.pyplot as plt

# 生成样本数据
date = ['2009/6/12 2:00', '2009/6/12 3:00', '2009/6/12 4:00', '2009/6/12 5:00', '2009/6/12 6:00', '2009/6/12 7:00', '2009/6/12 8:00', '2009/6/12 9:00', '2009/6/12 10:00', '2009/6/12 11:00', '2009/6/12 12:00', '2009/6/12 13:00', '2009/6/12 14:00', '2009/6/12 15:00', '2009/6/12 16:00', '2009/6/12 17:00', '2009/6/12 18:00', '2009/6/12 19:00', '2009/6/12 20:00', '2009/6/12 21:00', '2009/6/12 22:00', '2009/6/12 23:00',
    '2009/6/13 0:00', '2009/6/13 1:00', '2009/6/13 2:00', '2009/6/13 3:00', '2009/6/13 4:00', '2009/6/13 5:00', '2009/6/13 6:00', '2009/6/13 7:00', '2009/6/13 8:00', '2009/6/13 9:00', '2009/6/13 10:00', '2009/6/13 11:00', '2009/6/13 12:00', '2009/6/13 13:00', '2009/6/13 14:00', '2009/6/13 15:00', '2009/6/13 16:00', '2009/6/13 17:00', '2009/6/13 18:00', '2009/6/13 19:00', '2009/6/13 20:00', '2009/6/13 21:00', '2009/6/13 22:00', '2009/6/13 23:00',
    '2009/6/14 0:00', '2009/6/14 1:00', '2009/6/14 2:00', '2009/6/14 3:00', '2009/6/14 4:00', '2009/6/14 5:00', '2009/6/14 6:00', '2009/6/14 7:00', '2009/6/14 8:00', '2009/6/14 9:00', '2009/6/14 10:00', '2009/6/14 11:00', '2009/6/14 12:00', '2009/6/14 13:00', '2009/6/14 14:00', '2009/6/14 15:00', '2009/6/14 16:00', '2009/6/14 17:00', '2009/6/14 18:00', '2009/6/14 19:00', '2009/6/14 20:00', '2009/6/14 21:00', '2009/6/14 22:00', '2009/6/14 23:00',
    '2009/6/15 0:00', '2009/6/15 1:00', '2009/6/15 2:00']

count1 = [random.randrange(1, 50, 1) for i in range(len(date))]
count2 = [random.randrange(50, 100, 1) for i in range(len(date))]

data = pd.DataFrame()
data["Date"] = date
data["Count1"] = count1
data["Count2"] = count2
data["Date"] = pd.to_datetime(data["Date"], format="%Y/%m/%d %H:%M")

# 绘图核心代码
# 创建2行1列的子图,共享x轴避免重复显示
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True, figsize=(12, 7), dpi=100)

# 上半部分绘制水流量(Count2模拟水流量)
ax1.plot(data["Date"], data["Count2"], color='tab:blue', label='水流量')
ax1.set_ylabel('水流量', fontsize=12)
ax1.tick_params(axis='y', labelsize=12, alpha=.7)
# 隐藏多余边框
ax1.spines["top"].set_alpha(0)
ax1.spines["right"].set_alpha(0)
ax1.spines["left"].set_alpha(0)
ax1.grid(axis='y', alpha=.3)
ax1.legend(loc='upper left')

# 下半部分绘制降雨量(Count1模拟降雨量,如需柱状展示可把plot改为bar)
ax2.plot(data["Date"], data["Count1"], color='tab:green', label='降雨量')
# 关键:反转y轴实现倒置效果
ax2.invert_yaxis()
ax2.set_ylabel('降雨量', fontsize=12)
ax2.tick_params(axis='both', labelsize=12, alpha=.7)
# 隐藏多余边框
ax2.spines["bottom"].set_alpha(0)
ax2.spines["right"].set_alpha(0)
ax2.spines["left"].set_alpha(0)
ax2.grid(axis='y', alpha=.3)
ax2.legend(loc='lower left')

# 设置全局标题
fig.suptitle("降雨量与水流量组合图", fontsize=14, y=0.95)
# 调整子图间距避免重叠
plt.tight_layout()
plt.show()

如果需要更贴合常规的降雨量展示形式,只需要把下半部分的ax2.plot改为ax2.bar即可生成柱状的倒置降雨量图表。

内容的提问来源于stack exchange,提问作者Ailurophile

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 05:39:04