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

I am running a Django 3.1 app using plotly to display an interactive chart in a.html template file.我正在运行一个 Django 3.1 应用程序,使用 plotly 在 a.html 模板文件中显示交互式图表。

I am trying to create a plot using the subplots functionality.我正在尝试使用子图功能创建一个 plot。

The plot has 2 rows and 1 column. plot 有 2 行和 1 列。 The first row correctly displays the first chart which is a Candlestick plot of daily stock prices.第一行正确显示了第一个图表,它是每日股票价格的烛台 plot。 The second row of the plot was to be a bar chart of the volume for each of the days of the stock price. plot 的第二行是股票价格每一天的成交量条形图。

When I run the app, I receive no server errors and the.html page loads as expected.当我运行该应用程序时,我没有收到任何服务器错误,并且 .html 页面按预期加载。

However, the second plot should be a bar chart of the volume but the chart is blank.然而,第二个 plot 应该是成交量的条形图,但图表是空白的。 The plot correctly displays the yaxis and xaxis title and values for the tick as expected for the Volume data. plot 正确显示 yaxis 和 xaxis 标题以及刻度的值,如 Volume 数据所预期的那样。

If I set the axis_rangeslider_visible to True, then the chart displays the rangeselector in the 2nd chart where the volume data should be.如果我将 axis_rangeslider_visible 设置为 True,则该图表会在第二个图表中显示范围选择器,其中应该是体积数据。 The axis labels still display the expected values and labels as if the volume data is plotted.轴标签仍然显示预期值和标签,就像绘制体积数据一样。

The following code is from my views.py file within the Django app.以下代码来自我在 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

Well I still don't understand what the underlying problem is.好吧,我仍然不明白潜在的问题是什么。 However I have resolved my immediate need by plotting a smaller sample size.但是,我通过绘制较小的样本量解决了我的迫切需求。 The original plot had about 5,500 data points.原始的 plot 有大约 5,500 个数据点。 When I restricted the size to less than 500, the second subplot displayed correctly.当我将大小限制为小于 500 时,第二个子图显示正确。

So is there an upper limit on how many data points can be used in plotly sub plots.那么plotly个子图中可以使用多少个数据点是否有上限。 And only in the second plot. Puzzling.并且仅在第二个 plot 中。令人费解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM