繁体   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