Plotly - Cours du Bitcoin

Mis à jour le 24/03/2023

Un petit script Python pour illustrer comment afficher avec Plotly le cours du Bitcoin récupéré avec Pandas.

Le site Blockchain.com propose un diagramme avec le cours moyen du Bitcoin sur 1 jour, 7 jours ou 30 jours sur sa page Charts - market-price. Les données affichées peuvent être récupérées au format CSV via une API.

Le code Python qui suit montre comment facilement récupérer avec Pandas et afficher avec Plotly le cours du bitcoin.

Code source

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

url = "https://api.blockchain.info/charts/market-price?format=csv"

df = pd.read_csv(url)
df.columns = ["date", "price"]
df["date"] = pd.to_datetime(df["date"])

fig = px.line(
    df,
    x="date",
    y="price",
    title="<span style='font-weight: bold; font-size: 18px;'>Bitcoin Market Price (USD)</b>"
    + "<br>The average USD market price across major bitcoin exchanges."
    + "<br>Source: <a href='" + url + "'>" + url + "<a>",
    markers=False,
    labels={"price": "<b>USD</b>", "date": "<b>Date</b>"},
)

fig.update_layout(title_x=0.5)

fig.show()
fig.write_html("plotly-bitcoin.html")

Diagramme généré