簡體   English   中英

垂直堆疊的 883276440288 子圖中第二個 plot 中的數據在 Django 3.1 應用程序中未正確顯示

[英]The data in the 2nd plot in vertically stacked Plotly sub-plot not being displayed correctly in Django 3.1 app

我正在運行一個 Django 3.1 應用程序,使用 plotly 在 a.html 模板文件中顯示交互式圖表。

我正在嘗試使用子圖功能創建一個 plot。

plot 有 2 行和 1 列。 第一行正確顯示了第一個圖表,它是每日股票價格的燭台 plot。 plot 的第二行是股票價格每一天的成交量條形圖。

當我運行該應用程序時,我沒有收到任何服務器錯誤,並且 .html 頁面按預期加載。

然而,第二個 plot 應該是成交量的條形圖,但圖表是空白的。 plot 正確顯示 yaxis 和 xaxis 標題以及刻度的值,如 Volume 數據所預期的那樣。

如果我將 axis_rangeslider_visible 設置為 True,則該圖表會在第二個圖表中顯示范圍選擇器,其中應該是體積數據。 軸標簽仍然顯示預期值和標簽,就像繪制體積數據一樣。

以下代碼來自我在 Django 應用程序中的 views.py 文件。

from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from users.models import CustomUser
from .models import Chart

import pandas as pd
from datetime import datetime
from smart_open import smart_open
from django.conf import settings
import plotly.express as px
import plotly.graph_objs as go
from plotly.subplots import make_subplots


class ChartDetailView(LoginRequiredMixin, DetailView):
    model = Chart
    
    def get_queryset(self):
        return Chart.objects.filter(user=self.request.user)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        
        path = 's3://{}:{}@{}/{}'.format(settings.AWS_ACCESS_KEY_ID,
                                         settings.AWS_SECRET_ACCESS_KEY,
                                         settings.AWS_STORAGE_BUCKET_NAME,
                                         self.object.data_file.name)

        df_daily = pd.read_csv(smart_open(path), parse_dates=True)
        df_daily['Date'] = pd.to_datetime(df_daily['Date'])
        df_daily = df_daily.set_index('Date')

        fig_daily = make_subplots(rows=2, cols=1,
                                 row_heights=[0.8, 0.2],
                                 specs=[[{"type": "candlestick"}],
                                        [{"type": "bar"}]],
                                 shared_xaxes=True,
                                 vertical_spacing=0.02)

        fig_daily.add_trace(go.Candlestick(x=df_daily.index,
                                           open=df_daily['Open'],
                                           high=df_daily['High'],
                                           low=df_daily['Low'],
                                           close=df_daily['Close'],
                                           showlegend=False,
                                           ),
                            row=1, col=1)

        fig_daily.add_trace(go.Bar(x=df_daily.index,
                                   y=df_daily['Volume'],
                                   marker=dict(color="crimson"),
                                   showlegend=False),
                            row=2, col=1)

        # Update xaxis properties
        fig_daily.update_xaxes(title=None,row=1, col=1)
        fig_daily.update_xaxes(title="Date", row=2, col=1)

        # Update yaxis properties
        fig_daily.update_yaxes(title="$/share", row=1, col=1)
        fig_daily.update_yaxes(title="Volume", range=[0, df_daily['Volume'].max()], row=2, col=1)

        fig_daily.update_layout(title='Daily Stock Price and Volume',
                                xaxis_rangeslider_visible=False,
                                margin=dict(r=5, t=30, b=5, l=5),
                                height=650,
                                width=700)

        graph_daily = fig_daily.to_html()

        df_daily = df_daily[['Open', 'High', 'Low', 'Close', 'Volume']].to_html(classes='mystyle')

        context['df_daily'] = df_daily
        context['graph_daily'] = graph_daily

        return context

好吧,我仍然不明白潛在的問題是什么。 但是,我通過繪制較小的樣本量解決了我的迫切需求。 原始的 plot 有大約 5,500 個數據點。 當我將大小限制為小於 500 時,第二個子圖顯示正確。

那么plotly個子圖中可以使用多少個數據點是否有上限。 並且僅在第二個 plot 中。令人費解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM