简体   繁体   English

Plotly:如何仅在图形上显示今天的数据?

[英]Plotly: How to show only today's data on a figure?

I have made a graph using time axis on the X axis, however I want to show only today's data.我在 X 轴上使用时间轴制作了一个图表,但是我只想显示今天的数据。 (ie restrict the date to current date) (即限制日期为当前日期)

Here is the code I used to produce those graphs:这是我用来生成这些图表的代码:

fig = go.Figure()
fig = make_subplots(rows=2,cols=1,shared_xaxes=True,vertical_spacing=0.02)
fig.add_trace(go.Scatter(x=data['time'],y=data['x_last_recorded'],name='xyz',mode='lines+markers'),row=2,col=1)
fig.add_trace(go.Scatter(x=predict_data['time'],y=predict_data['x1_last_recorded'],name='x1yz',mode='lines'),row=2,col=1)

fig.update_layout(height=800,width=1500,title='first_graph',yaxis_title='Values')

This has got me a graph of how I want, but it is showing all the dates present in the dataframe.这给了我一张我想要的图表,但它显示了 dataframe 中存在的所有日期。 How do I fetch only the current date's data?如何仅获取当前日期的数据? Structure of time: dd-mm-yyyy hh:mm时间结构:dd-mm-yyyy hh:mm

We can solve your challenge by subsetting your dataframe using an approach such as df_current = df[df.index.date==df.index.date[-1]] , and then restyle your figure by letting different subsets be represented by different options in a dropdown menu.我们可以通过使用诸如df_current = df[df.index.date==df.index.date[-1]]类的方法对您的 dataframe 进行子集化来解决您的挑战,然后通过让不同的子集由不同的选项表示来重新设置您的图形在下拉菜单中。

Here's the resulting figure for the different subsets / selection options:这是不同子集/选择选项的结果图:

All dates所有日期

在此处输入图像描述

Current date当前的日期

在此处输入图像描述

Complete code:完整代码:

# imports
import plotly.graph_objects as go
import pandas as pd
import numpy as np

# sample data in the form of an hourlt
np.random.seed(1234)
tseries = pd.date_range("01.01.2020", "01.04.2020", freq="H")
data = np.random.randint(-10, 12, size=(len(tseries), 2))
df = pd.DataFrame(index=tseries, data=data)
df.drop(df.tail(1).index,inplace=True)
df.columns=list('AB')
df.iloc[0]=0
df=df.cumsum()

# subset method
df_current = df[df.index.date==df.index.date[-1]]

# plotly setup
fig = go.Figure()

# set up a trace for each column in a dataframe
for col in df.columns:
    fig.add_trace(go.Scatter(x=df.index, y =df[col], name=col))

# container for updatemenus and buttons
updatemenu = []
buttons = []

# button 1
buttons.append(dict(method='restyle',
                    label='All',
                    visible=True,
                    args=[{'y':[df[col].values for col in df.columns],
                           'x':[df.index],
                           'type':'scatter'}, ],))
# button 2
buttons.append(dict(method='restyle',
                    label='Current',
                    visible=True,
                    args=[{'y':[df_current[col].values for col in df_current.columns],
                           'x':[df_current.index],
                           'type':'scatter'}, ],))

# menu setup
your_menu = dict()
updatemenu.append(your_menu)

updatemenu.append(your_menu)
updatemenu[0]['buttons'] = buttons
updatemenu[0]['direction'] = 'down'
updatemenu[0]['showactive'] = True

# add dropdown menus to the figure
fig.update_layout(showlegend=False, updatemenus=updatemenu)
fig.show()

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

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