带有日期的x轴绘图:
现在,只需将下面的片段中的x=df.index
替换为x=df.index.strftime("%Y/%m/%d")
即可获得以下图形:
以字符串为x轴的图形:
代码:# imports
from plotly.subplots import make_subplots
import plotly.graph_objs as go
import pandas as pd
import numpy as np
# data
np.random.seed(123)
frame_rows = 50
n_plots = 1
frame_columns = ['V_'+str(e) for e in list(range(n_plots+1))]
df = pd.DataFrame(np.random.uniform(-10,10,size=(frame_rows, len(frame_columns))),
index=pd.date_range('1/1/2020', periods=frame_rows),
columns=frame_columns)
df=abs(df)
df.iloc[21:-2]=np.nan
df=df.dropna()
# show figure
fig = go.Figure()
fig.add_traces(go.Bar(#x=df.index,
x=df.index.strftime("%Y/%m/%d"),
y=df['V_0']))
fig.show()
如果有人在处理股票数据,以下是代码可隐藏非交易时间和周末的范围中断。
fig = go.Figure(data=[go.Candlestick(x=df['date'], open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'])])
fig.update_xaxes(
rangeslider_visible=True,
rangebreaks=[
# NOTE: Below values are bound (not single values), ie. hide x to y
dict(bounds=["sat", "mon"]), # hide weekends, eg. hide sat to before mon
dict(bounds=[16, 9.5], pattern="hour"), # hide hours outside of 9.30am-4pm
# dict(values=["2020-12-25", "2021-01-01"]) # hide holidays (Christmas and New Year's, etc)
]
)
fig.update_layout(
title='Stock Analysis',
yaxis_title=f'{symbol} Stock'
)
fig.show()
这是Plotly文档的链接。
dict(bounds=[11.5, 13], pattern="hour")
来排除11:30和13:00之间的空隙,则图形会变成空白。有任何想法吗? - HCSF