将存储箱大小绘制为周

2024-04-28 04:28:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用plotly绘制包含日期数据的直方图。我想用与周相对应的垃圾箱大小来绘制它,但这似乎不起作用。我搜索了有关它的文档,但什么也没找到

这是我的密码。我试过(第5行):“D7”和“W1”。这是行不通的(plotly似乎无法识别该参数,并将其设置为每天一个bin)。奇怪的是“M1”,“M3”等等。。。似乎有效

fig = go.Figure(data=[go.Histogram(x=df.col, 
                                   xbins=dict(
                                       start='2018-01-01',
                                       end='2018-12-31',
                                       size='D7'), 
                                   autobinx=False)])
fig.update_layout(
    title=go.layout.Title(
        text="title",
        xref="paper",
        x=0.5
    ),
    xaxis_title_text='xaxis title',
    yaxis_title_text='yaxis title'
)


fig.show()

有人知道关于这个问题的信息吗? 谢谢


Tags: 数据text文档go密码titlefig绘制
2条回答

似乎重新采样的数据源和条形图才是您真正想要的:

绘图:

enter image description here

这里,基于每日观察的源数据DatetimeIndex(['2020-01-01', '2020-01-02', ... , '2020-07-18'],已被重新采样,以显示特定股票价格的每周总和

代码:

# Imports
import pandas as pd
#import matplotlib.pyplot as plt
import numpy as np
import plotly.graph_objects as go
#from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

# data, random sample to illustrate stocks
np.random.seed(12345)
rows = 200
x = pd.Series(np.random.randn(rows),index=pd.date_range('1/1/2020', periods=rows)).cumsum()
y = pd.Series(x-np.random.randn(rows)*5,index=pd.date_range('1/1/2020', periods=rows))
df = pd.concat([y,x], axis = 1)
df.columns = ['StockA', 'StockB']

# resample daily data to weekly sums
df2=df.reset_index()
df3=df2.resample('W-Mon', on='index').mean()

# build and show plotly plot
fig = go.Figure([go.Bar(x=df3.index, y=df3['StockA'])])
fig.show()

让我知道这对你是如何起作用的

默认情况下,xbins.size以毫秒为单位指定。要获取每周垃圾桶,请将xbins.size设置为604800000(7天,每个垃圾桶86400000毫秒)

Plotly提供了xM格式来获取月度仓位,因为此用例需要在后台进行更复杂的计算,因为月度仓位没有统一的大小

相关问题 更多 >