我正在编写一个 Python 程序来生成加密货币价格的折线图。 目标是能够在同一水平时间线上覆盖多个任意对,即 BTC/USD + ETH/BTC 或 BTC/USD + BCH/LTC + XRP/BTG,类似于 https://coinmarketcap.com/currencies/ethereum/
上面示例中的附加功能不是必需的。它不需要点击缩放技术、对数刻度按钮或花哨的工具提示,如果这些功能增加了实现难度。每条线单独的颜色会很有帮助。这是我必须使用的数据格式:
https://min-api.cryptocompare.com/data/histominute?fsym=BTC&tsym=USD&limit=60&aggregate=1&e=Coinbase
https://min-api.cryptocompare.com/data/histominute?fsym=ETH&tsym=BCH&limit=30&aggregate=1&e=CCCAGG
目前,我在 Visual Studio 上安装了 Anaconda 3.6,根据 https://docs.anaconda.com/anaconda/packages/py3.6_win-64.html应包含多个包来绘制此数据,而无需通过命令行手动安装第三方代码。然而,当我尝试导入其中任何一个(即 matplotlib、bokeh、seaborn)时,我收到一条“ModuleNotFoundError”消息,所以我不确定我的 Anaconda 是否正常运行。使用 Anaconda 绘制这些数据的最简单方法是什么?
最佳答案
VSCode 中的 python 解释器可能未使用您安装的 Anaconda。在 VSCode python shell 中,输入 import sys
,然后输入 sys.version
,它应该会告诉您正在使用的 python 版本。
这里有一些代码可以帮助您开始在 bokeh(Anaconda 附带的库)中执行此操作。我没有使用 Visual Studio(或 Visual Studio Code——我不确定您指的是哪一个),而是使用了 Jupyter Notebook,并且此处的一些导入特定于该环境(以显示内联 Bokeh 图)。您可能想要以不同的方式设置日期格式。
from bokeh.plotting import figure, output_file, show
from bokeh.io import output_notebook
import numpy as np
output_notebook()
import requests
import datetime
from math import pi
def format_date(utc_time):
time = datetime.datetime.fromtimestamp(int(utc_time))
return time
url1 = "https://min-api.cryptocompare.com/data/histominute?fsym=BTC&tsym=USD&limit=60&aggregate=1&e=Coinbase"
url2 = "https://min-api.cryptocompare.com/data/histominute?fsym=ETH&tsym=BCH&limit=30&aggregate=1&e=CCCAGG"
r1 = requests.get(url1)
r2 = requests.get(url2)
r1_source = r1.json()["Data"]
r2_source = r2.json()["Data"]
r1_data = [i["close"] for i in r1_source]
r1_time = [format_date(i["time"]) for i in r1_source]
# r2_data = [i["close"] for i in r2_source]
# r2_time = [i["time"] for i in r2_source]
p = figure(plot_width=800, plot_height=400)
p.line(r1_time,r1_data, line_width=2)
# p.line(r2_time, r2_data, line_width=2, line_color="red")
p.xaxis.major_label_orientation = pi/4
关于python - Anaconda 中的线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48387874/