Anaconda 中的折线图

Line Graphs in Anaconda

我正在编写一个 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 中执行此操作,bokeh 是 Anaconda 附带的一个库。我没有使用 Visual Studio(或 Visual Studio 代码——我不确定你指的是哪一个),而是使用了 jupyter notebook,这里的一些导入是特定于该环境的(以内联显示散景图)。您可能希望以不同方式设置日期格式。

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