在子图中绘制更改xaxis的域

时间:2020-03-22 05:34:48

标签: python plotly-python

我使用下面的代码绘制了两个子图。我想将x轴域/范围更改为与第一个子图相同的范围。换句话说,第一个子图的x值范围是2004年到2010年,而第二个轴的图是从2010年开始。我希望第二个子图也从2004年开始,然后一直到2019年。第二个子图的第一部分当然将没有数据(2004年至2010年),然后第二部分将具有相同的抛物线外观曲线(从2010年开始) )。有关如何执行此操作的任何想法? 。我在图上看到有更改域的选项,但不确定如何为子图实现这一点。

Fig = make_subplots(rows = 2, cols = 1,subplot_titles = ['ndvi TS','anomaly'])
Fig.add_trace(go.Scatter(
    x = pd.to_datetime(df['ts'],format = '%d/%m/%Y'),
    y = df['fitted.values'],
    mode = 'lines',
    line = dict(
        color = 'steelblue',
        width = 2
    ),
    name = 'fitted line'
),
row = 1, col = 1
)

Fig.add_trace(go.Scatter(
    x = newdf['date'],
    y = newdf['cumulativeDelta'],
    mode = 'markers',
    marker = dict(
        color = 'lightcoral',
        size = 3,
        symbol = 'circle',
    ),
    name = 'Anomaly'
),
row = 2, col = 1
)

Fig.update_layout(
    width = 800,
    height = 600,
)

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该为此使用update_xaxes

fig.update_xaxes(range=["2004-01-21", "2020-03-23"])