<jupyter_start><jupyter_text>设置离线模式<jupyter_code>cf.offline.go_offline()<jupyter_output><empty_output><jupyter_text>准备数据<jupyter_code>ohlc = cf.datagen.ohlc(n=50)
ohlc<jupyter_output><empty_output><jupyter_text>修改前图样周末存在较大间隙  图样<jupyter_code>ohlc.iplot(kind='candle')<jupyter_output><empty_output><jupyter_text>分析<jupyter_code>fig = ohlc.iplot(kind='candle',asFigure=True)
fig
x = fig['data'][0]['x']
type(x)
x.dtype<jupyter_output><empty_output><jupyter_text>模拟过程<jupyter_code>import plotly.graph_objs as go
import pandas as pd
import plotly<jupyter_output><empty_output><jupyter_text>针对主要逻辑<jupyter_code>kind = 'candlestick'
_d = dict(type=kind,
          open=ohlc['open'].values.tolist(),
          high=ohlc['high'].values.tolist(),
          low=ohlc['low'].values.tolist(),
          close=ohlc['close'].values.tolist())
if isinstance(ohlc.index, pd.core.indexes.datetimes.DatetimeIndex):
    _d['x'] = ohlc.index.astype('str')
# 尝试以obj表达，输出同样结果    
# _d['x'] = ohlc.index.strftime('%Y-%m-%d')
_d['yaxis'] = 'y2'
data = [_d]
fig = go.Figure(data=data)
plotly.offline.iplot(fig)<jupyter_output><empty_output><jupyter_text>`Candlestick`  省略`x`将使用自然序号<jupyter_code>trace = go.Candlestick(
    open=ohlc['open'].values.tolist(),
    high=ohlc['high'].values.tolist(),
    low=ohlc['low'].values.tolist(),
    close=ohlc['close'].values.tolist(),
)
data = [trace]
plotly.offline.iplot(data)<jupyter_output><empty_output><jupyter_text>隐藏滑块<jupyter_code>trace = go.Candlestick(
    open=ohlc['open'].values.tolist(),
    high=ohlc['high'].values.tolist(),
    low=ohlc['low'].values.tolist(),
    close=ohlc['close'].values.tolist(),
)

layout = go.Layout(
    xaxis = dict(
        rangeslider = dict(
            visible = False
        )
    )
)
data = [trace]

fig = go.Figure(data=data,layout=layout)
plotly.offline.iplot(fig)<jupyter_output><empty_output><jupyter_text>修改刻度<jupyter_code>import pandas as pd
import numpy as np
def get_tick(x, percentile=[10, 25, 50, 75, 90], fmt=r'%Y-%m-%d'):
    tickvals = []
    ticktext = []
    idx = np.percentile(range(len(x)), percentile).astype(int)
    # 取唯一值
    idx = pd.Series(idx).drop_duplicates().values
    for i in idx:
        tickvals.append(i)
        ticktext.append(pd.Timestamp(x[i]).strftime(fmt))
    return tickvals, ticktext
tickvals, ticktext = get_tick(ohlc.index.values)
trace = go.Candlestick(
    open=ohlc['open'].values.tolist(),
    high=ohlc['high'].values.tolist(),
    low=ohlc['low'].values.tolist(),
    close=ohlc['close'].values.tolist(),
    text=ohlc.index,  # 将日期添加到悬停文本
)

tickvals, ticktext = get_tick(ohlc.index.values)

layout = go.Layout(
    xaxis=go.layout.XAxis(
        tickmode='array',
        tickvals=tickvals,
        ticktext=ticktext,
        rangeslider=dict(
            visible=False
        )
    )
)
data = [trace]

fig = go.Figure(data=data, layout=layout)

plotly.offline.iplot(fig)
fig<jupyter_output><empty_output><jupyter_text>`ohlc`<jupyter_code>tickvals, ticktext = get_tick(ohlc.index.values)
# ticktext = ohlc.index.strftime(r'%Y-%m-%d')
trace = go.Ohlc(
    open=ohlc['open'].values.tolist(),
    high=ohlc['high'].values.tolist(),
    low=ohlc['low'].values.tolist(),
    close=ohlc['close'].values.tolist(),
    hovertext=ohlc.index.strftime(r'%Y-%m-%d'),  # 将日期添加到悬停文本
)

layout = go.Layout(
    xaxis=go.layout.XAxis(
        tickmode='array',
        tickvals=tickvals,
        ticktext=ticktext,
        rangeslider=dict(
            visible=False
        )
    )
)
data = [trace]

fig = go.Figure(data=data, layout=layout)

plotly.offline.iplot(fig)
data
layout['xaxis']<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17306099917195694, 'fraction_numerical': 0.0038642009384487995, 'mean_word_length': 4.524390243902439, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '264654', 'license': 'Apache-2.0', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1518, 'n_words': 157, 'path': 'plotly/memo.ipynb', 'repo_name': 'NeoBert/liudengfeng-zipline_doc', 'size': '3623'}