Issue
I am trying to plot the Bitcoin historical data on a Matplotlib graph using the this (2010-2024 CSV file) dataset.
When I graph it using Matplotlib, it gives the attached graph.
I have tried the same exact code with a different dataset (using the Solana crypto price dataset, it has the same exact CSV keys, just different values), and its graph is attached.
Here is my code:
import matplotlib.pyplot as mp
import pandas as pd
import numpy as np
import sklearn as sk
import random
from sklearn.linear_model import Perceptron
csv = pd.read_csv("./bitcoin-historical-trading-data.csv")
model = Perceptron()
csv['Date'] = pd.to_datetime(csv['Date'])
csv['Date'] = (csv['Date'].astype('int64') / 10**9).astype('int64') * 1000
csv = csv[["Date", "Price"]]
date = np.array(csv["Date"])
price = np.array(csv["Price"])
mp.plot(date, price)
mp.show()
Please let me know if you have any idea why this is happening.
Solution
When you see a Matplotlib plot with that many ticks, the first thing you should consider is that your data might not be numerical, see the Matplotlib FAQ.
To verify, print out csv.dtypes
and I'm guessing the "Price"
column will be of object
type. Looking at the links you provided, the Bitcoin data is in the thousands while the Solana is not. Without an account I cannot download the data, but if the saved data has those commas, Pandas will read them as strings (and make them object
s). Remove those commas and cast the data to a float
.
csv["Price"] = csv["Price"].str.replace(",", "").astype(float)
Answered By - jared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.