Matplotlib使用时间戳为索引时的奇怪输出

0 投票
1 回答
50 浏览
提问于 2025-04-14 17:40

给定以下的数据表

        open     close      high       low                        timestamp
0   17910.25  17902.75  17910.50  17901.75 2024-02-28 20:46:10.628867+00:00
1   17902.50  17901.75  17906.50  17900.50 2024-02-28 20:46:50.270189+00:00
2   17902.25  17904.50  17905.50  17901.25 2024-02-28 20:47:55.031114+00:00
3   17904.75  17907.25  17909.00  17904.50 2024-02-28 20:48:44.114794+00:00
4   17907.25  17910.25  17914.50  17906.75 2024-02-28 20:49:31.424009+00:00
5   17910.25  17912.50  17914.50  17906.00 2024-02-28 20:50:05.337921+00:00
6   17912.75  17909.00  17912.75  17906.50 2024-02-28 20:50:51.489718+00:00
7   17909.00  17908.50  17910.50  17905.50 2024-02-28 20:51:32.629298+00:00
8   17908.75  17899.75  17908.75  17899.25 2024-02-28 20:52:06.190489+00:00
9   17899.50  17900.50  17902.00  17897.50 2024-02-28 20:52:38.421629+00:00
10  17900.25  17900.75  17904.25  17898.75 2024-02-28 20:53:22.651748+00:00
11  17900.25  17898.00  17901.25  17895.50 2024-02-28 20:54:07.124026+00:00
12  17898.00  17895.75  17900.75  17892.00 2024-02-28 20:54:36.208693+00:00
13  17895.75  17897.25  17898.00  17893.75 2024-02-28 20:55:00.171311+00:00
14  17897.25  17902.00  17902.00  17895.75 2024-02-28 20:55:09.389301+00:00
15  17902.00  17905.50  17906.75  17901.00 2024-02-28 20:55:30.245781+00:00
16  17905.25  17905.25  17906.50  17900.75 2024-02-28 20:56:03.837146+00:00
17  17905.25  17909.75  17910.75  17904.75 2024-02-28 20:56:26.466095+00:00
18  17909.75  17916.50  17918.25  17908.50 2024-02-28 20:56:47.284619+00:00
19  17916.25  17911.50  17917.25  17911.50 2024-02-28 20:57:16.248157+00:00
20  17911.50  17913.75  17914.50  17909.25 2024-02-28 20:57:56.980558+00:00
21  17913.50  17916.00  17917.25  17910.00 2024-02-28 20:58:32.008823+00:00
22  17916.00  17916.25  17918.75  17913.50 2024-02-28 20:58:59.908276+00:00
23  17916.25  17915.00  17918.25  17915.00 2024-02-28 20:59:16.061040+00:00
24  17915.00  17911.75  17915.25  17910.00 2024-02-28 20:59:27.103036+00:00

我可以用下面的函数来画蜡烛图

def plotCandles(df):
    #df.set_index('timestamp', inplace=True)
    plt.figure() 

    # "up" dataframe will store the stock_prices 
    # when the closing stock price is greater 
    # than or equal to the opening stock prices 
    up = df[df.close >= df.open] 

    # "down" dataframe will store the stock_prices 
    # when the closing stock price is 
    # lesser than the opening stock prices 
    down = df[df.close < df.open] 

    # When the stock prices have increased, then it 
    # will be represented by green color candlestick 
    col1 = 'green'

    # When the stock prices have decreased, then it 
    # will be represented by red color candlestick 
    col2 = 'red'

    # Setting width of candlestick elements 
    width = .3
    width2 = .03

    # Plotting up prices of the stock 
    plt.bar(up.index, up.close-up.open, width, bottom=up.open, color=col1) 
    plt.bar(up.index, up.high-up.close, width2, bottom=up.close, color=col1) 
    plt.bar(up.index, up.low-up.open, width2, bottom=up.open, color=col1) 

    # Plotting down prices of the stock 
    plt.bar(down.index, down.close-down.open, width, bottom=down.open, color=col2) 
    plt.bar(down.index, down.high-down.open, width2, bottom=down.open, color=col2) 
    plt.bar(down.index, down.low-down.close, width2, bottom=down.close, color=col2) 

    # rotating the x-axis tick labels at 30degree 
    # towards right 
    plt.xticks(rotation=30, ha='right') 

    # show it
    plt.show() 

这样就会生成下面的图

这里有一些漂亮的蜡烛图

但是,如果我尝试通过下面的代码把索引设置为时间戳列

df.set_index('timestamp', inplace=True)

图形就变成了一种表现主义的东西

一些表现主义的图片

这是为什么呢?

1 个回答

暂无回答

撰写回答