亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Plotly:如何在圖形上只顯示今天的數據?

Plotly:如何在圖形上只顯示今天的數據?

牧羊人nacy 2023-05-09 09:55:34
我在 X 軸上使用時間軸制作了一個圖表,但我只想顯示今天的數據。(即限制日期為當前日期)這是我用來生成這些圖表的代碼: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')這讓我得到了一張我想要的圖表,但它顯示了數據框中存在的所有日期。如何只獲取當前日期的數據?時間結構:dd-mm-yyyy hh:mm
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

我們可以通過使用諸如 之類的方法對您的數據框進行子集化來解決您的挑戰df_current = df[df.index.date==df.index.date[-1]],然后通過讓不同的子集由下拉菜單中的不同選項表示來重新設計您的圖形。

這是不同子集/選擇選項的結果圖:

所有日期

http://img1.sycdn.imooc.com//6459a83300012a8b06520345.jpg

當前日期

http://img1.sycdn.imooc.com//6459a83f000117b506510328.jpg

完整代碼:

# 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()




查看完整回答
反對 回復 2023-05-09
  • 1 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號