技术博客
汇率走势可视化:用日线/分钟K线数据画图(Python + 前端)

汇率走势可视化:用日线/分钟K线数据画图(Python + 前端)

作者: 万维易源
2026-08-27
外汇数据查询汇率可视化matplotlibECharts
# 汇率走势可视化:用日线/分钟K线数据画图(Python + 前端) > 接口级 · 免费 · 返回 JSON · 适用:分析展示、教学演示 · 阅读时间:9 分钟 ## TL;DR - 日线(1683-2)和分钟 K(1683-3)都返回结构化 OHLC 数组,天然适合画图。 - Python 端用 `matplotlib` 画折线/蜡烛;前端用 `ECharts` 做交互式 K 线。 - 数据是延迟数据,图表上务必标注「仅供学习分析,非实时行情」。 ## Why:把数字变成图,决策才直观 拿到一串 `close` 价格,人眼很难看出趋势。画成折线或蜡烛图,支撑位、波动区间一目了然。本文给可直接跑的 Python 与前端两段代码。 ## What:数据准备 | 接入点 | 取数字段 | 横轴 | 纵轴 | |--------|---------|------|------| | 日线 1683-2 | `list`: `date`,`open`,`high`,`low`,`close` | `date` | 价格 | | 分钟K 1683-3 | `list`: `datetime`,`open`,`high`,`low`,`close` | `datetime` | 价格 | > 价格字段是字符串,画图前先 `float()`。 ## How ### Python:日线收盘价折线 ```python import requests, matplotlib.pyplot as plt def daily(code, begin, end, appkey): r = requests.post("https://route.showapi.com/1683-2", data={"appKey": appkey, "code": code, "begin": begin, "end": end}, timeout=10) return r.json()["showapi_res_body"]["list"] rows = daily("USDCNY", "20250120", "20250201", "YOUR_APPKEY") xs = [r["date"] for r in rows] ys = [float(r["close"]) for r in rows] plt.figure(figsize=(10, 4)) plt.plot(xs, ys, marker="o") plt.title("USDCNY 日线收盘价(延迟数据,仅供学习分析)") plt.xticks(rotation=45) plt.tight_layout() plt.show() ``` ### 前端:ECharts 蜡烛图(取数后渲染) ```html <div id="k" style="width:600px;height:400px"></div> <script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script> <script> async function load() { const resp = await fetch("https://route.showapi.com/1683-2", { method: "POST", headers: {"content-type": "application/x-www-form-urlencoded"}, body: new URLSearchParams({appKey:"YOUR_APPKEY", code:"USDCNY", begin:"20250120", end:"20250201"}) }); const rows = (await resp.json()).showapi_res_body.list; const candle = rows.map(r => [r.date, +r.open, +r.close, +r.low, +r.high]); const chart = echarts.init(document.getElementById("k")); chart.setOption({ xAxis: {type:"category"}, yAxis: {scale:true}, series: [{type:"candlestick", data: candle}] }); } load(); </script> ``` ## 进阶 / 边界 - **缺失日期**:节假日无日 K,折线会出现断点,绘制前按日期补空或断开处理。 - **延迟标注**:所有对外图表必须标注「延迟数据,仅供学习分析,不得用于对外展示」。 - **大批量**:分钟 K `limit` 最大 60,长区间需分段拉取拼接。 ## FAQ **Q1:能画实时行情图吗?** A:不能,数据是延迟的,仅适合学习分析展示。 **Q2:蜡烛图需要哪些字段?** A:ECharts candlestick 期望 `[日期, 开, 收, 低, 高]` 顺序,注意是「开收低高」不是「开高低收」。 **Q3:横轴日期太密怎么办?** A:用 `plt.xticks(rotation=45)` 或只显示间隔刻度。 **Q4:分钟 K 一次画多少根?** A:受 `limit`(≤60) 限制,更长需分段。 ## 下一步阅读 - [外汇数据查询:日线历史查询接入点实战](https://www.showapi.com/guides/forex-daily-history-1683) - [外汇数据查询:历史分钟K线查询接入点实战](https://www.showapi.com/guides/forex-minute-kline-1683) - [免费档位限制下:如何设计缓存策略节省调用次数](https://www.showapi.com/guides/forex-cache-cost-1683) - **本系列共 13 篇**:查看[外汇数据查询指南总目录](https://www.showapi.com/guides/forex-guides-1683)