Matplotlib 奇怪的输出,以时间戳为索引

问题描述 投票:0回答:1

给出以下数据框

        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)

人物变成了表现主义的东西

怎么来的?

python pandas matplotlib
1个回答
0
投票

条形的宽度以 x 轴的单位测量。您可以使用两个刻度之间的距离来指导条形宽度。由于您的 x 值不等距,并且不同宽度的条形图看起来不太好,因此您可以使用最近条形图的距离。

这是一个可重现的代码示例:

import matplotlib.pyplot as plt
import pandas as pd
from pandas import Timestamp

df = pd.DataFrame({'open': [17910.25, 17902.5, 17902.25, 17904.75, 17907.25, 17910.25, 17912.75, 17909.0, 17908.75, 17899.5, 17900.25, 17900.25, 17898.0, 17895.75, 17897.25, 17902.0, 17905.25, 17905.25, 17909.75, 17916.25, 17911.5, 17913.5, 17916.0, 17916.25, 17915.0],
                   'close': [17902.75, 17901.75, 17904.5, 17907.25, 17910.25, 17912.5, 17909.0, 17908.5, 17899.75, 17900.5, 17900.75, 17898.0, 17895.75, 17897.25, 17902.0, 17905.5, 17905.25, 17909.75, 17916.5, 17911.5, 17913.75, 17916.0, 17916.25, 17915.0, 17911.75],
                   'high': [17910.5, 17906.5, 17905.5, 17909.0, 17914.5, 17914.5, 17912.75, 17910.5, 17908.75, 17902.0, 17904.25, 17901.25, 17900.75, 17898.0, 17902.0, 17906.75, 17906.5, 17910.75, 17918.25, 17917.25, 17914.5, 17917.25, 17918.75, 17918.25, 17915.25],
                   'low': [17901.75, 17900.5, 17901.25, 17904.5, 17906.75, 17906.0, 17906.5, 17905.5, 17899.25, 17897.5, 17898.75, 17895.5, 17892.0, 17893.75, 17895.75, 17901.0, 17900.75, 17904.75, 17908.5, 17911.5, 17909.25, 17910.0, 17913.5, 17915.0, 17910.0],
                   'timestamp': [Timestamp('2024-02-28 20:46:10.628867+0000', tz='UTC'), Timestamp('2024-02-28 20:46:50.270189+0000', tz='UTC'), Timestamp('2024-02-28 20:47:55.031114+0000', tz='UTC'), Timestamp('2024-02-28 20:48:44.114794+0000', tz='UTC'), Timestamp('2024-02-28 20:49:31.424009+0000', tz='UTC'), Timestamp('2024-02-28 20:50:05.337921+0000', tz='UTC'), Timestamp('2024-02-28 20:50:51.489718+0000', tz='UTC'), Timestamp('2024-02-28 20:51:32.629298+0000', tz='UTC'), Timestamp('2024-02-28 20:52:06.190489+0000', tz='UTC'), Timestamp('2024-02-28 20:52:38.421629+0000', tz='UTC'), Timestamp('2024-02-28 20:53:22.651748+0000', tz='UTC'), Timestamp('2024-02-28 20:54:07.124026+0000', tz='UTC'), Timestamp('2024-02-28 20:54:36.208693+0000', tz='UTC'), Timestamp('2024-02-28 20:55:00.171311+0000', tz='UTC'), Timestamp('2024-02-28 20:55:09.389301+0000', tz='UTC'), Timestamp('2024-02-28 20:55:30.245781+0000', tz='UTC'), Timestamp('2024-02-28 20:56:03.837146+0000', tz='UTC'), Timestamp('2024-02-28 20:56:26.466095+0000', tz='UTC'), Timestamp('2024-02-28 20:56:47.284619+0000', tz='UTC'), Timestamp('2024-02-28 20:57:16.248157+0000', tz='UTC'), Timestamp('2024-02-28 20:57:56.980558+0000', tz='UTC'), Timestamp('2024-02-28 20:58:32.008823+0000', tz='UTC'), Timestamp('2024-02-28 20:58:59.908276+0000', tz='UTC'), Timestamp('2024-02-28 20:59:16.061040+0000', tz='UTC'), Timestamp('2024-02-28 20:59:27.103036+0000', tz='UTC')]})

df = df.set_index('timestamp')
min_dist = df.index.diff().min()
width = min_dist * 0.95
width2 = width * 0.1

plt.figure()

up = df[df.close >= df.open]
down = df[df.close < df.open]

col1 = 'green'
col2 = 'red'

# 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)

plt.xticks(rotation=30, ha='right')

plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.