我開始學習資料科學,我被教導可以matplotlib.pyplot用來繪制我的資料。以下是這些的一些預覽(1 小時時間范圍內的 OHLC 價格):

而且這里是SOLUSDT_close.head().to_dict()用于除錯目的的輸出:
{'Close Price': {'2021-06-29 00:59:59.999': 33.65642566,
'2021-06-29 01:59:59.999': 33.309,
'2021-06-29 02:59:59.999': 32.76168975,
'2021-06-29 03:59:59.999': 33.118,
'2021-06-29 04:59:59.999': 33.267}}
所以,我只對繪制Close Price反對的感興趣End Date,我寫了下面的代碼來做到這一點:
import pandas as pd
import matplotlib.pyplot as plt
# Import the csv file without index
SOLUSDT = pd.read_csv('C:/Users/ResetStoreX/Downloads/Binance futures data/SolUSDT-Mark_Prices_Klines_1h_Timeframe/zip/SOLUSDT-1h-June-29-2021-February-13-2022.csv', index_col=0)
# Create a new df that only contains the date and close price from the previous imported df
SOLUSDT_close = SOLUSDT[['End Date','Close Price']]
# Set the 'Date' column as the actual index
SOLUSDT_close.set_index('End Date', inplace=True)
# set the breadth and length of the plot as a good mix of values
plt.figure(figsize=(14,5))
# set a grid background to the plot
plt.grid(True)
# set the color of the trend as blue
plt.plot(SOLUSDT_close, 'b')
# give a title to the plot
plt.title('SOLUSDT close price from June 29 2021 to February 13 2022')
# give a label to the x axis
plt.xlabel('Date')
# gove a label to the y axis
plt.ylabel('Close Price')
# plot it
plt.plot(SOLUSDT_close)
然而,我最終得到了一個令人失望的輸出:

可以看出,網格線與灰色背景合并,并且在 x 軸上也沒有顯示一些預期的結束日期值。
如果我決定洗掉該plt.grid(True)陳述句并再次運行代碼,它最終會繪制以下圖表:

我想要實作的是從
請問我可以在這里得到一些幫助嗎?
uj5u.com熱心網友回復:
這里唯一的問題是你有太多的日期,每個日期都在 x 軸上繪制它的線。一種方法是axis在
另一種方法是只選擇您想放在 x 軸上的特定時間(日期) - 正如您現在所看到的,它們太多了。此外,繪圖的第二行不是必需的。這將是一個非常快速和骯臟的例子,但你明白了:
import pandas as pd
import matplotlib.pyplot as plt
df= pd.read_csv('./sol-usd-max.csv', usecols=['snapped_at','price'])
df.set_index('snapped_at', inplace=True)
# set the breadth and length of the plot as a good mix of values
plt.figure(figsize=(14,5))
# set a grid background to the plot
plt.grid(True, axis='y')
plt.grid(True, axis='x', which='major')
# set the color of the trend as blue
plt.plot(df[::10], 'b')
# give a title to the plot
plt.title('SOLUSDT close price from June 29 2021 to February 13 2022')
# give a label to the x-axis
plt.xlabel('Date')
# give a label to the y axis
plt.ylabel('Close Price')

更新
至于錯誤的日期時間格式,將其轉換為日期時間(通過將其加載為日期時間或使用轉換df["snapped_at"] = pd.to_datetime(df["snapped_at"]))也可以解決問題:
import pandas as pd
import matplotlib.pyplot as plt
df= pd.read_csv('./sol-usd-max.csv', usecols=['snapped_at','price'], parse_dates=["snapped_at"])
# df.set_index('snapped_at', inplace=True)
# set the breadth and length of the plot as a good mix of values
plt.figure(figsize=(14,5))
# set a grid background to the plot
plt.grid(True)
# set the color of the trend as blue
plt.plot(df["snapped_at"], df["price"], c='b')
# give a title to the plot
plt.title('SOLUSDT close price from June 29 2021 to February 13 2022')
# give a label to the x axis
plt.xlabel('Date')
# give a label to the y axis
plt.ylabel('Close Price')
但這不是問題的主要關注點。我的意思是,如果 dtypes 是正確的,它就不會發生,但它不會直接導致背景變灰。

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/478262.html
標籤:Python 熊猫 matplotlib
