简体   繁体   English

如何使用 matplotlib 或 plotly 放大 Python 中的图形?

[英]How to zoom in a graph in Python using matplotlib or plotly?

I want to zoom in to the red section of the graph.我想放大图表的红色部分。 I have the following code snippet.我有以下代码片段。 What changes do I need to make in my code either using matplotlib or plotly to achieve the desired results.我需要使用 matplotlib 或 plotly 在我的代码中进行哪些更改才能达到预期的结果。 Please prefer plotly library if you can.如果可以,请首选 plotly 库。

# Visualize decision tree predictions

predictions = treePrediction
valid = df[x.shape[0]:]
valid["Predictions"] = predictions
plt.figure(figsize=(12, 7))
plt.title("Apple's Stock Price Prediction Model(Decision Tree Regressor Model)")
plt.xlabel("Days")
plt.ylabel("Close Price USD ($)")
plt.plot(df["Mean"])
plt.plot(valid[["Mean", "Predictions"]])
plt.legend(["Original", "Valid", "Predictions"])
plt.show()

在此处输入图像描述

Referring to the published node book, I have reproduced the graph in question.参考已发布的节点书,我复制了有问题的图表。 Since the nodebook was using locale CSV data, I retrieved the stock price data from yfinance and replaced the closing price with df['Mean'] .由于 nodebook 使用的是区域设置 CSV 数据,因此我从yfinance检索了股票价格数据并将收盘价替换为df['Mean'] Plotly, a graph_object was used to add each graph. Plotly,一个graph_object用于添加每个图。 I have also added a button to select the period for zooming.我还在 select 中添加了一个按钮,用于缩放。 See this page for details .有关详细信息,请参阅此页面。 We are adding 3 months as a period selection.我们将添加 3 个月作为期间选择。

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(mode='lines', x=df.index, y=df['Mean'], line_color='blue', name='Original'))
fig.add_trace(go.Scatter(mode='lines', x=valid.index, y=valid['Mean'], line_color='orange', name='Valid'))
fig.add_trace(go.Scatter(mode='lines', x=valid.index, y=valid['Predictions'], line_color='green',name='Predictions'))

fig.update_layout(
    autosize=True,
    height=600,
    title="Apple's Stock Price Prediction Model(Decision Tree Regressor Model)",
    xaxis_title="Days",
    yaxis_title="Close Price USD ($)",
    template='plotly_white'
)

# Add range slider
fig.update_layout(
    xaxis=dict(
        rangeselector=dict(
            buttons=list([
                dict(count=1,
                     label="1m",
                     step="month",
                     stepmode="backward"),
                dict(count=3,
                     label="3m",
                     step="month",
                     stepmode="backward"),              
                dict(count=6,
                     label="6m",
                     step="month",
                     stepmode="backward"),
                dict(count=1,
                     label="YTD",
                     step="year",
                     stepmode="todate"),
                dict(count=1,
                     label="1y",
                     step="year",
                     stepmode="backward"),
                dict(step="all")
            ])
        ),
        rangeslider=dict(
            visible=True
        ),
        type="date"
    )
)

fig.show()

在此处输入图像描述

Select the most recent month using the Select Period button. Select 最近一个月使用 Select 期间按钮。

在此处输入图像描述

The range slider at the bottom of the graph can be used to select any range.图表底部的范围 slider 可用于 select 的任何范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM