Bokeh,如何删除没有相应值的日期时间?

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

希望你做得很好,我想渲染一个不存在长线的图表,这些线对应于没有相应对的值。

这是生成我的图表的函数,希望我不会吓到你:

def get(self, request):
        user=request.user
        historical_me=FinancialInstrument.objects.all()
        form = self.form_class(historical_me=historical_me)
        
        retrive=HistoricalData.objects.all()
        get_symbol=retrive.filter(instrument__symbol="BTUSD").order_by('datetime')
        fields=get_symbol.values('datetime','opening_price','closing_price','min_price','max_price','volume')
        df=pd.DataFrame.from_records(fields)
        
        x_values = [key.datetime for key in get_symbol]
        y_values = [float(key.opening_price) for key in get_symbol]
        
        source = ColumnDataSource(data=dict(dater=x_values,pricer=y_values))
        plot = figure(title="New title", width=780, height=400,
                      x_axis_label="Date", y_axis_label="Max Price", x_axis_type="datetime")
        plot.line(x='dater',y='pricer', source=source,line_width=3)
        
        unique_dates = HistoricalData.objects.order_by('datetime').values_list('datetime', flat=True).distinct()

        plot.line(x='datetime', y='opening_price', source=source, line_width=3)

        plot.toolbar.autohide = True

        script, div = components(plot)
        if user.is_authenticated:
            return render(
                request,
                self.template_name,
                {
                    'form':form,
                    'days_of_month': [i for i in range(1,32)],
                    'script':script,
                    'div':div,
                })

        return redirect('home')

这是它生成的图表,我需要一种方法来抑制那些长线,它们对应于没有相应值的日期。

django bokeh
1个回答
0
投票

解决方案是将

NaN
添加到
DataFrame
中没有有效值的所有日期,因为
line
渲染器会在给定数据点之间进行插值。如果存在
NaN
值,则不会绘制任何内容。在您的情况下,一种选择是使用 pandas
resample()
函数添加一个简单的频率。

请参阅下面的最小示例:

import pandas as pd
from bokeh.plotting import figure, show, output_notebook
output_notebook()

data = {
    'datetime': ['2024-04-15', '2024-04-16', '2024-04-19', '2024-04-20'],
    'opening_price': [1,  1, -1, -1],
}
df = pd.DataFrame(data)
df['datetime'] = pd.to_datetime(df['datetime'])
df.set_index('datetime', inplace=True)

# the one line which adds a frequency and to all not know dates the value `NaN`
df = df.resample('1D').mean()

p = figure(x_axis_type='datetime', x_axis_label='date', y_axis_label='change')
p.line(x='datetime', y='opening_price', source=df, line_width=3)
show(p)
无需重新采样 重新采样
no dates with NaN values dates with NaN values added

如果我们比较两个打印的 DataFrame,您可以看到发生了什么变化。

这是没有

resample
调用的 DataFrame。

            opening_price
datetime                 
2024-04-15              1
2024-04-16              1
2024-04-19             -1
2024-04-20             -1

这是改变的。

            opening_price
datetime                 
2024-04-15            1.0
2024-04-16            1.0
2024-04-17            NaN
2024-04-18            NaN
2024-04-19           -1.0
2024-04-20           -1.0

请记住,

line
调用需要至少两个连续的数据点才能绘制一条线。使用
line
将不可见单个点。
circle
渲染器具有不同的行为。

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