使用箱线图可视化股票数据

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

我想使用烛台图和箱线图可视化我的数据。我已经完成了烛台,但我被箱线图困住了。

我提供了代码:

def visualization(df, trading_days):
    dt_range = pd.date_range(start="2015-01-01", end="2015-03-01")
    df = df[df.index.isin(dt_range)]

    # Calculate the average values of 'trading_days' consecutive days
    df = df.rolling(window=trading_days, min_periods=1).mean()

    df = delete_gaps(df, trading_days)

    hover = HoverTool(
        tooltips=[
            ('Date', '@Date{%Y-%m-%d}'),  # Display the 'Date' value in 'yyyy-mm-dd' format
            ('Open', '@Open'),  # Display the 'Open' column value with formatting
            ('High', '@High'),  # Debugging: Print High value
            ('Low', '@Low'),  # Debugging: Print Low value
            ('Close', '@Close'),  # Debugging: Print Close value
            ('Volume', '@Volume')  # Debugging: Print Volume value
        ],
        formatters={'@Date': 'datetime'},
        mode='mouse'
    )

    source = ColumnDataSource(data=df)

    # Create ColumnDataSources for increasing and decreasing values
    inc_source = ColumnDataSource(data=df[df.Close > df.Open])
    dec_source = ColumnDataSource(data=df[df.Open > df.Close])

    w = 12 * 60 * 60 * 1000

    candle = figure(x_axis_type="datetime", width=800, height=500, title="Apple, March - 2020")

    candle.segment('Date', 'High', 'Date', 'Low', source=source, color="black")

    # Create vbar glyphs for increasing and decreasing values with different colors
    candle.vbar('Date', w, 'Open', 'Close', source=inc_source, fill_color="red", line_color="red",
             legend_label="Increasing")
    candle.vbar('Date', w, 'Open', 'Close', source=dec_source, fill_color="green", line_color="green",
             legend_label="Decreasing")

    candle.add_tools(hover)

    ## Boxplot Chart
    box = figure(x_axis_type="datetime", width=800, height=200)

    box.vbar('Date', width=w, top='Volume', source=source, fill_color="dodgerblue", alpha=0.8)

    box.add_tools(hover)

    box.xaxis.axis_label = "Date in March-2020"
    box.yaxis.axis_label = "Volume"
    candle.yaxis.axis_label = "Price ($)"

    show(column(candle, box))

df 的输出:

                 Open       High        Low      Close  Adj Close       Volume
Date                                                                          
2015-01-06  14.388444  14.532222  13.880667  14.237333  14.237333   81974000.0
2015-01-09  14.112889  14.190222  13.883333  13.960666  13.960666   55396000.0
2015-01-14  13.160000  13.495111  13.003555  13.314445  13.314445  109897500.0
2015-01-20  12.868000  12.985778  12.593111  12.819333  12.819333   66614500.0
2015-01-23  13.040889  13.453777  12.956444  13.321778  13.321778   58542500.0
2015-01-28  13.608000  13.844889  13.394889  13.597778  13.597778   45825500.0
2015-02-02  13.533333  13.897778  13.395555  13.772000  13.772000   53521500.0
2015-02-05  14.475334  14.829555  14.393556  14.620000  14.620000   58272500.0
2015-02-10  14.554000  14.707334  14.299778  14.469556  14.469556   60534000.0
2015-02-13  13.526222  13.862667  13.366000  13.765556  13.765556  158048500.0
2015-02-19  13.663778  13.873556  13.507778  13.789333  13.789333   59236500.0
2015-02-24  14.082889  14.290889  13.729778  13.968000  13.968000  105427500.0
2015-02-27  13.685334  13.928444  13.502222  13.650889  13.650889   71322500.0

我想可视化遵循以下要求的数据集: 顶部的须须就是高颜值, 晶须底部价值较低, 酒吧的顶部是开放的, 酒吧的底部很近, mid是中间价

python matplotlib visualization bokeh
1个回答
0
投票

有一个基本方法可以使用

matplotlib
来做到这一点:

import matplotlib.pyplot as plt

**
Your code
**

bp = plt.boxplot(df['Volume'])
plt.show()

您可以在 matplotlib

docs
中找到其他选项。

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